PDA

View Full Version : Any PHP programers here?



SchoonerRat
08-20-2009, 01:48 PM
I have a web page that is updated at intervals of between 2 minutes and 20 minutes. Currently, I have the clients browser refresh the page every 2 minutes. This almost does what I want it to but there are some problems.

I would like the clients browser to check the server every 5 seconds or so, and refresh the page only if it has been modified. I know enough Javascript to know that Javascript won't do it. A couple of hours with Google has taught me enough PHP to believe that it is the way to go but not enough to actually program it.

Previous experience with the WBF has taught me that there is a major brain trust here. Can anybody come up with a few lines of code to handle this?

I have faith!

George Roberts
08-20-2009, 02:02 PM
Java(script) can do it. You need a time stamp for page. The Java(script) can sleep for 5 seconds and then depending on the time stamps reload the page.

There are lots of ways to do it.

SchoonerRat
08-20-2009, 02:16 PM
I need to be able to read the last modified date from the file on the server. What is the Javascript command to do that. The only one I have found can only read the last modified date of the html that is already loaded in the clients browser.

I think that what you are describing is basicaly what I'm already doing

TimH
08-20-2009, 02:20 PM
PHP runs server-side. Javascript client-side.

Michael s/v Sannyasin
08-20-2009, 02:30 PM
If you decided to use PHP, it would only be part of the solution. You have to explicitly call PHP to run the script, then it renders the output. If your webpage was generated by a PHP script, then that script would only run once.

If you wanted to invoke a PHP script multiple times, then you'd have to set the meta-tag to refresh every few seconds, but that would likely be re-drawing the entire page each time. (you're probably already doing this)

<meta http-equiv="refresh" content="10;">

Most modern websites use Ajax if they want to dynamically reload parts of the page. You can find an example of that here:

http://ajaxpatterns.org/Periodic_Refresh

There was also something that looked promising here:

http://stackoverflow.com/questions/204886/can-i-use-http-header-to-check-if-a-dynamic-page-has-been-changed

But, I don't have a clear-cut sample of code, doing exactly what you want.

SchoonerRat
08-20-2009, 02:31 PM
PHP runs server-side. Javascript client-side.
Thanks, Tim

That's exactly why I tried to find a Javascript solution first. I'm not sure the server I'm on now allows PHP and I'd rather not move. If I can find a Javascript solution I'll be a happy camper!

SchoonerRat
08-20-2009, 02:38 PM
If you decided to use PHP, it would only be part of the solution. You have to explicitly call PHP to run the script, then it renders the output. If your webpage was generated by a PHP script, then that script would only run once.

If you wanted to invoke a PHP script multiple times, then you'd have to set the meta-tag to refresh every few seconds, but that would likely be re-drawing the entire page each time. (you're probably already doing this)

<meta http-equiv="refresh" content="10;">

Most modern websites use Ajax if they want to dynamically reload parts of the page. You can find an example of that here:

http://ajaxpatterns.org/Periodic_Refresh

There was also something that looked promising here:

http://stackoverflow.com/questions/204886/can-i-use-http-header-to-check-if-a-dynamic-page-has-been-changed

But, I don't have a clear-cut sample of code, doing exactly what you want.
Both of these sites turned up in my Google searches. At the time I didn't think they applied. I'm looking to reload the entire html page not just part of it. If the PHP is in that page, shouldn't it just start searching the server once again for a new last modified date.

I'm getting the idea that the first thing I need to do is to make the html page into a php page.

Michael s/v Sannyasin
08-20-2009, 02:50 PM
Tim's point is the point that I was trying to make. When you first navigate to a page or when clicking the reload button for a page, whether the page is a .PHP file or a .HTML file with embedded PHP script in it, what happens is that on the server, the server runs the PHP script, and the PHP may either generate the HTML to send to the client, or modify what appears in the middle of the HTML that then gets sent to the client.

Once it is on the client, it's done.

If you want to have automatic refresh, then you either set a meta-tag which will refresh the whole page whether there are changes or not.

Or, you might be able to tell it through Javascript to call a Javascript function every few seconds. That javascript function could then request the page from the server again, but then look at the header, if the date had changed then it could refresh the DIV area in question... the second link looked like it was close to this type of solution, but I didn't spend enough time to fully grok what they were doing.

The ajax solution reads as if it is nearly what you want, but that would require that your webpage and webserver support ajax.

SchoonerRat
08-20-2009, 03:08 PM
Right now I'm doing an auto refresh every 2 minutes with a meta tag. The problems are.

1. This often leaves the client with old info for as long as 2 minutes.

2. I could shorten that 2 minutes but if the client attempts to reload while the new file is uploading then the page goes blank. Refreshing more often increases the likelyhood of this happening.

I can't seem to wrap my head around the problem of not being able to refresh without a meta tag. If my PHP loads a page, can't that page contain the code to once again check it's modified time on the server and reload again if necessary?

The page in question loads into a frame and the entire page is replaced. Is there no way I can do this without using a meta tag to refresh at regular intervals.

I'm not yet totally eliminating an Ajax solution, but my minimal PHP experience absolutely dwarfs my Ajax experience.

Kaa
08-20-2009, 03:49 PM
I can't seem to wrap my head around the problem of not being able to refresh without a meta tag. If my PHP loads a page, can't that page contain the code to once again check it's modified time on the server and reload again if necessary?

Well, your server-side code could generate a very small (a few digits) separate web page that the client will check. This very small web page could be used to signal that the contents of the main page have changed (the client stores the current value, the server changes the value if the main web page got updated, the client checks the value and if it's different from its own stored one, reloads the main page), as well as serve as a semaphor for the page updating (if the client sees a specific value, a flag, on that web page, it will NOT attempt to reload the main web page until the flag clears).

Kaa

George Roberts
08-20-2009, 05:26 PM
Well, your server-side code could generate a very small (a few digits) separate web page that the client will check. This very small web page could be used to signal that the contents of the main page have changed (the client stores the current value, the server changes the value if the main web page got updated, the client checks the value and if it's different from its own stored one, reloads the main page), as well as serve as a semaphor for the page updating (if the client sees a specific value, a flag, on that web page, it will NOT attempt to reload the main web page until the flag clears).

Kaa

That is the same as my time stamp idea. As I have said their are so many ways to solve this problem. I think I used to save the file time stamp in a comment in the file or in a data base. Then just read a small portion of the file.

But I don't write this stuff any more so I don't keep up the latest "tricks."

Michael s/v Sannyasin
08-22-2009, 08:46 AM
PHP can only be used on the server side, what you need is code on the client side to wake up every few seconds and refresh if necessary. I don't know javascript well enough to comment on what it could do, but I do know that this is precisely what they use Ajax for. Ajax can wrap segments of your HTML (such as a DIV tag), and it handles the automatic refresh of the data, only for that DIV.

Tools such as Visual Studio make Ajax pretty easy, I've walked through little demos that only take about 5-10 minutes, but requires you to have Visual Studio on your Dev computer.

Bruce Hooke
08-22-2009, 02:33 PM
If my PHP loads a page, can't that page contain the code to once again check it's modified time on the server and reload again if necessary?

Others have already tried to explain this in other ways, but I'll try again. Once a PHP page is delivered to the web browser it is no longer a PHP page and cannot contain any PHP code because the web browser would not know what to do with PHP code. PHP code runs on the server and generates output that does not contain PHP. So, if you want something to happen on the client side you cannot do it with PHP. You can use PHP to create JavaScript code that runs on the client side but of course this only makes sense if the JavaScript code for some reason needs to be dynamically generated.

SchoonerRat
08-23-2009, 06:48 PM
Thanks all!

I've gone back to hashing this out in Javascript and I almost have what I need. I'm sure if I just tweak the code a little bit...I can fix it so it don't work at all.:D

Thanks again for the help.