Solved

php conditionally auto refresh

Posted on 2011-09-18
6
486 Views
Last Modified: 2012-05-12
Is there a way to have a php page/url that you have opened in your browser to reload/refresh automatically only when a certain condition has ocured, f. ex. when a new version of a file has been uploaded to the webserver?
0
Comment
Question by:daghoff
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36556891
in short: no.
but, you could run some timed ajax script to check if there is a new version (so no full page reload for that), and if there is, then indeed request to page to reload itself.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 167 total points
ID: 36556910
<script language="javascript" type="text/javascript">
var current_version = <?PHP echo $current_version ?>;
setInterval(function() {
     if(window.XMLHttpRequest) var xmlhttp=new XMLHttpRequest();else var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
     xmlhttp.open("GET","what_is_last_version.php",false);
     xmlhttp.send();
     if( parseInt(xmlhttp.responseText) > current_version ) location.reload();
}, 30000); // check every 30s
</script>

what_is_last_version.php is a php code returning (echo $xxx) the version number (or a datetime) of the file
0
 
LVL 13

Assisted Solution

by:Hugh McCurdy
Hugh McCurdy earned 167 total points
ID: 36557101
leakim971's answer looks pretty good (with the caveat that I didn't study it).  angelIII's answer looks correct but it occurs to me that you might not know about Ajax.

Mostly I'm posting to get e-mail updates in case you don't yet have a solution.

One question I have is if you truly want to reload the entire page or just part of it?

In case you don't know about Ajax, a good resource is here
http://www.w3schools.com/ajax/default.asp
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Assisted Solution

by:boon86
boon86 earned 166 total points
ID: 36558163
here is example of auto refresh:

File xmlhttp.js
var xmlhttp = false ;


if (!xmlhttp && typeof XMLHttpRequest != ‘undefined’)
{
try {
xmlhttp = new XMLHttpRequest ();
}
catch (e) {
xmlhttp = false}
}


function myXMLHttpRequest ()
{
var xmlhttplocal;
try {
xmlhttplocal = new ActiveXObject (“Msxml2.XMLHTTP”)}
catch (e) {
try {
xmlhttplocal = new ActiveXObject (“Microsoft.XMLHTTP”)}
catch (E) {
xmlhttplocal = false;
}
}

if (!xmlhttplocal && typeof XMLHttpRequest != ‘undefined’) {
try {
var xmlhttplocal = new XMLHttpRequest ();
}
catch (e) {
var xmlhttplocal = false;
}
}
return (xmlhttplocal);
}

var mnmxmlhttp = Array ();
var mnmString = Array ();
var mnmPrevColor = Array ();
var responsestring = Array ();
var myxmlhttp = Array ();
var responseString = new String;

var i=0;
var ii = 0;
function ajax_update()
{
url = “ajax_php.php”;
target2 = document.getElementById (‘content’);
ii = i++;

var content = “i=” + ii ;

mnmxmlhttp = new myXMLHttpRequest ();
if (mnmxmlhttp) {
mnmxmlhttp.open (“POST”, url, true);
mnmxmlhttp.setRequestHeader (‘Content-Type’,
‘application/x-www-form-urlencoded’);

mnmxmlhttp.send (content);
errormatch = new RegExp (“^ERROR:”);

target2 = document.getElementById (‘content’);

mnmxmlhttp.onreadystatechange = function () {
if (mnmxmlhttp.readyState == 4) {
mnmString = mnmxmlhttp.responseText;

if (mnmString.match (errormatch)) {
mnmString = mnmString.substring (6, mnmString.length);

target = document.getElementById (‘content’);
target2.innerHTML = mnmString;

} else {
target = document.getElementById (‘content’);
target2.innerHTML = mnmString;

}
}
}
}
setTimeout(‘ajax_update()’, 20000);
}

Open in new window


File ajax_php.php
<?PHP
echo “<PRE>”;
print_r($_POST);
echo time();
?>

Open in new window


File test.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP + Ajax auto refresh DIV</title>
<script language="JavaScript" type="text/javascript" src="xmlhttp.js"></script>
</head>
<body onload="ajax_update();">
<divid="content">Loading....</div>
</body>
</html>

Open in new window



you can download example here: http://www.dayrecipe.com/wp-content/uploads/2008/04/testajax.zip
0
 

Author Comment

by:daghoff
ID: 36560447
It is correct that I don't know javascript and ajax. But I do asume I will need to use that to achive what I want. php and HTML can't do this alone.

Let me give a more detailed description of what I need:

I have a PC application that when used may upload some files to a web server. These files is public and some peopels may want to see if the info in the files are updated since last they visited the website.

I have currently implemented a php refresh statement that reloads the web page (php generated html) with the info from the uploded file every 5 seconds for as long a user chooses to keep the website open, but I belive that is not good design.

What I want is a 100% serverbased looping logic that can test the creation date on the uploaded file and when it detects there is a new file (and only then) it refresesh the users web page with the new data. The testloop is started when the user opens that webpage and must be active for as long as the user chooses to keep the web page activ/open.

Whether to reload the hole page or only parts of it looks irelevant to me.

Thoose of you who have ansvered, please feel free to give som more comments.
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36561308
I think it will become easier in the future but I think, for now, you assume correctly.

Do you know PHP fairly well?  If so, I doubt this will be too difficult for you.  I know PHP fairly well and I barely know any Javascript and I got AJAX working.  However, in my case, I didn't reload the page.  I sent new information to just part of the page.  This isn't exactly what I did but think of the problem of sending new stock quotes to someone viewing an otherwise static page.  I did something like that.

I suggest you read the beginning of the AJAX section at w3schools to get started.
http://www.w3schools.com/ajax/default.asp

Once you've done that, look at the code posted by leakim and boon to see if they make any more sense to you.  Probably not.  Then look at the Javascript section of W3 schools (but don't worry if you don't understanding everything).  

Then give it a try.  It probably won't work the first time.  If you get stuck, come back here for help.

That's my suggestion.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now