I Was wondering if it was possible to refresh a php webpage when a files content has changed?
I have some code but it doesn't refresh on a file change it refreshes every second. How can I do it when the file changes?
Here is the code:
<html>
<head>
<title>PHP Page</title>
<meta http-equiv="refresh" content="1">
</head>
<body>
<?php
$filename = "data.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
echo "<pre>$contents</pre>";
fclose($handle);
?>
</body>
you can try this
header( "Refresh:5; url=http://www.example.com/page2.php", true, 303);
You can fire an ajax request on every 5 seconds which will check either file is changed or not.
If file is changed the reload the page with javascript using
location.reload();
Related
When I try to open js.php, it gives an error in xampp while in 000webhost it doesn't . I don't understand why.
so here is the code.
js.php
<?php
header('Content: text/javascript');
if($_SESSION['myjskey'] != hash('md5','examplekey')){
die('No Auth');
}
$_SESSION['JSSESSID'] = 'change';//so that no one can access directly
?>
//secret js
alert('javascript done.');
//and some more js
index.php
<?php
session_start();
$_SESSION['myjskey'] = hash('md5','examplekey');
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP</title>
</head>
<body>
this is a test.
<script type="text/javascript"><?php include('js.php'); ?></script>
</body>
</html>
Website js.php
Xampp Js.php
Edit 1: I cannot do session_start(); in js.php as it will give an error index.php
(session already started.) Removing the header didn't work.
<?php
//js
if($_SESSION['myjskey'] != hash('md5','examplekey')){
die('No Auth');
}
$_SESSION['JSSESSID'] = 'change';//so that no one can access directly
?>
//secret js
alert('javascript done.');
//and some more js
make this change no need to use
header('Content: text/javascript');
I want to parsing data from homepage on this url. As you can see this url is HTML file and I read below:
// Create a DOM object from a HTML file
$html = file_get_html('test.htm');
so I just type a code below
include "simple_html_dom.php";
$html = file_get_html('eecs.kookmin.ac.kr/site/computer/notice.htm');
echo $html->plaintext;
The error message is:
Error message Warning: file_get_contents(eecs.kookmin.ac.kr/site/computer/notice.htm): failed to open stream: No such file or directory in C:\Bitnami\wampstack-5.6.27-0\apache2\htdocs\simple_html_dom.php on line 76
what should I do?
You can get the HTML code using the Snoopy Class (https://sourceforge.net/projects/snoopy). Next code displays the HTML code inside of a <textarea> tag, then it displays the page itself, copy-paste next code in a PHP file and open it in your browser:
<!DOCTYPE html>
<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=euc-kr">
<META HTTP-EQUIV="Content-language" CONTENT="ko">
</head>
<body>
<?php
require("Snoopy.class.php"); // ◄■■ GET SNOOPY FROM https://sourceforge.net/projects/snoopy
$snoopy = new Snoopy;
$snoopy->fetch("http://eecs.kookmin.ac.kr/site/computer/notice.htm");
$html = mb_convert_encoding( $snoopy->results, "UTF-8", "EUC-KR" ); // ◄■■ GET HTML CODE.
echo "<textarea rows='25' cols='80'>$html</textarea>"; // ◄■■ DISPLAY THE HTML.
echo $html; // ◄■■ DISPLAY THE WEBPAGE.
?>
</body>
</html>
The Snoopy Class is only one file, make sure the file is in the same directory your PHP file is.
I currently have a php file that I'm using as a template but I need it to read in data from another php file that I'm using for the page content. I'm doing it this way to save on code and time, however it doesn't appear to be working. I have done a test with shorter amounts of code but it still isn't working. they are both .php files.
Code -
<html>
<head>
<title></title>
</head>
<body>
<?php
$temp = 'test-2.php';
$file = fopen($temp, 'r');
$cont = fread($file, filesize($temp));
print $cont;
fclose($file);
?>
</body>
test-2.php
<?php
echo 'hello world';
?>
just use <?php include('test-2.php'); ?> surely that'll do what you want?
Ok, what I am trying to do is make a javascript loop of images, but first I have to get a list of the images. In javascript there is no way to directly grab this text file... http://www.ssd.noaa.gov/goes/east/tatl/txtfiles/ft_names.txt but it can be done eaisly in php, I am currently gettung the txt file using php, but the javascript cannot read the variable. How can I make javascript be able to read this variable. Here is what I have...
<?php
$file = "http://www.ssd.noaa.gov/goes/east/tatl/txtfiles/ft_names.txt"; //Path to your *.txt file
$contents = file($file);
$string = implode($contents);
echo $string;
?>
<script type="text/javascript">
function prnt() {
var whatever = "<?= $string ?>";
alert(whatever);
}
</script>
You can use echo or print to write to the page in PHP.
var whatever = "<?php echo $string; ?>";
Although, if the file has line breaks in it, you will need to remove those.
Make it a bit more interesting: go ahead and split the fields and use JSON encoding. It should read directly in javascript without needing to call JSON.parse() on the client.
<?php
$lines = file_get_contents('http://...');
$lines = explode("\n",trim($lines));
foreach ($lines as &$line) {
$line = preg_split('/,? /',$line);
}
$js = json_encode($lines);
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
var dar = <?php echo $js; ?>;
</script>
</body>
</html>
You should also consider using a local proxy to cache the results of that file if you plan to run this frequently and especially if you are going to serve it up on a public web server somewhere. Store the file locally as "noaa_data.txt" and have a second script on a cron job (12 hours or something):
<?php
file_put_contents("/var/www/noaa_data.txt",file_get_contents("http://www.ssd.noaa.gov/goes/east/tatl/txtfiles/ft_names.txt"));
?>
I am using ob_start()/ob_flush() to, hopefully, give me some progress during a long import operation.
Here is a simple outline of what I'm doing:
<?php
ob_start ();
echo "Connecting to download Inventory file.<br>";
$conn = ftp_connect($ftp_site) or die("Could not connect");
echo "Logging into site download Inventory file.<br>";
ftp_login($conn,$ftp_username,$ftp_password) or die("Bad login credentials for ". $ftp_site);
echo "Changing directory on download Inventory file.<br>";
ftp_chdir($conn,"INV") or die("could not change directory to INV");
// connection, local, remote, type, resume
$localname = "INV"."_".date("m")."_".date('d').".csv";
echo "Downloading Inventory file to:".$localname."<br>";
ob_flush();
flush();
sleep(5);
if (ftp_get($conn,$localname,"INV.csv",FTP_ASCII))
{
echo "New Inventory File Downloaded<br>";
$datapath = $localname;
ftp_close($conn);
} else {
ftp_close($conn);
die("There was a problem downloading the Inventory file.");
}
ob_flush();
flush();
sleep(5);
$csvfile = fopen($datapath, "r"); // open csv file
$x = 1;
// skip the header line
$line = fgetcsv($csvfile);
$y = (feof($csvfile) ? 2 : 5);
while ((!$debug) ? (!feof($csvfile)) : $x <= $y) {
$x++;
$line = fgetcsv($csvfile);
// do a lot of import stuff here with $line
ob_flush();
flush();
sleep(1);
}
fclose($csvfile); // important: close the file
ob_end_clean();
However, nothing is being output to the screen at all.
I know the data file is getting downloaded because I watch the directory where it is being placed.
I also know that the import is happening, meaning that it is in the while loop, because I can monitor the DB and records are being inserted.
Any ideas as to why I am not getting output to the screen?
You also need to check the PHP settings
some installs default to 4096, some default to off
output_buffering = Off
output_buffering = 4096
agreed with George but do check the above settings
Make sure that your output buffering doesn't start automatically. Run:
print ob_get_level ();
before ob_start (); if will will see something else then 0 you've got the answer.
Hey man I was also got stuck in this problem
and finally got the correct solution
here it is for you
you have to add content type for your page
you can do that by two ways
1. using html tag
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Ex.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Wp Migration</title>
</head>
<body>
<?php
for($i=0;$i<70;$i++)
{
echo 'printing...<br>';
ob_flush();
flush();
sleep(3);
}
?>
</body>
</html>
using php header function
<?php header( 'Content-type: text/html; charset=utf-8' ); ?>
Ex.
<?php
header( 'Content-type: text/html; charset=utf-8' );
for($i=0;$i<70;$i++)
{
echo 'printing...<br>';
ob_flush();
flush();
sleep(3);
}
?>
All the best
Ob_end_clean() discards the contents of the current output buffer and turns off the buffering.
You should use ob_end_flush() instead.
Add this line
header("X-Accel-Buffering: no");
worked for me.
You can edit it with the .htaccess file
To disable output buffering, modify the line as follows:
php_value output_buffering Off
php_value output_buffering 4096
worked for me. Thank you!
Check this site: Click Here
It's possible that your webserver is doing its own buffering. Probably with something like mod_gzip.
Here is some very simple test code:
<?php
echo 'starting...<br/>';
for($i = 0; $i < 5; $i++) {
print "$i<br/>";
flush();
sleep(2);
}
print 'DONE!<br/>';
If it takes 10 seconds for that page to load, rather than seeing a new line every 2 seconds, then it means that it is being cached by your webserver. For what you are trying to do, there is no need to use ob_start and ob_flush. Just call flush whenever you want to force the content to the browser. However, like I mentioned, if the webserver is waiting for the content to complete before sending, then that won't do anything for you.
Edit: Another possibility is that you're viewing the page from behind a corporate or ISP proxy/firewall that waits for the whole page before serving it (so that it can scan it to see if it looks like pornography, for example).