0

Here is the scenario: I have a page that is logging data to MYSQL. I have another page that reads that data and allows it to be viewed. When a new piece of data is logged I would like to have the first script check and see if the viewing page is open in the browser, and if so append the newest data to the end of the view. Also - could anyone point to some info giving an overview of how PHP and the browser interact? I think I have the concept of the DOM down for javascript...but as far as PHP it just appears that once the page is sent, that's it...

3
  • 1
    php and the browser newer interact. Php runs in the server, and the browser in the client... Commented Jan 10, 2012 at 17:26
  • 1
    You're correct, PHP can't "check if a page is being viewed". PHP scripts are executed on demand, so you should rework your understanding of PHP. It's not like javascript with events and the like, because all of the code is executed on the server, rather than on the client's machine. The client requests a URL to a PHP script, the script is run, and it sends the output to the client. Commented Jan 10, 2012 at 17:27
  • OK, Thanks - I have done some nasty stuff generating javascript on the server side to change the page after checking something in the database...but that was after redirecting to the url of the new script which I can't do in this case. I will explore some of the suggestions below and see where it takes me. Commented Jan 10, 2012 at 20:58

3 Answers 3

1

You're correct in that once the PHP is sent, that's it.

There is no way to send data to a PHP page once the page is loaded. There is another slightly nastier method, but the easiest way of doing this is going to be polling the page via Ajax.

So, have a script that every 20 seconds, sends a message to another PHP script that contains the timestamp of the last MySQL log you received, then get the script to return all the data that has been set by that time.

I'm unsure how new you are to JavaScript, but the easiest way of doing that is probably using JQuery's $.ajax and encoding the new MySQL records as JSON.

Sign up to request clarification or add additional context in comments.

Comments

0

No this isn't possible as you describe. The viewing page will have to poll the server for changes, either by periodically reloading itself, or by javascript / AJAX.

You are right that once the page is sent by PHP it can have no further influence. In fact the PHP execution thread on the server is killed as soon as output is complete, so the thing that generated the page no longer even exists.

Comments

0

To expand on Dolondro's suggestion, rather than periodically polling the server for updates, you could use Server-Sent-Events (newly supported in modern browsers).

With these, you basically just send 1 ajax request to the server, and the connection is held open. Then, the server can send updates whenever it wants. When the browser receives an event, it can add the data to the screen. Even still, the connection is held open, and the server can send additional events/updates as they occur.

W3C page: http://dev.w3.org/html5/eventsource/

Wikipedia: http://en.wikipedia.org/wiki/Server-sent_events

More Info: https://www.google.com/search?ix=hcb&sourceid=chrome&ie=UTF-8&q=server+sent+events

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.