Solved

php conditionally auto refresh

Posted on 2011-09-18
6
516 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 143

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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 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