Tran Ngu Dang
Tran Ngu Dang

Reputation: 2630

PHP -JavaScript : Monitor changes of a file

I'm still new to PHP/Javascript.I've made a simple Clock program in Javascript successfully.

Now i have a file ( and the file will change its content automatically). For example, maybe st like this: "12h15: eat breakfast"

And at 13.00, the file's content will change to : "13h:go to the university"

And i wanna make a php( or javascript) code and show up that content every time the file changes.

Upvotes: 0

Views: 775

Answers (1)

Oliver A.
Oliver A.

Reputation: 2900

You need:

  1. A PHP script that serves the initial page

  2. A PHP script that serves the file content as string

  3. A javascript on your page that calls the second PHP script via ajax and updates your html document if the content has changed

If you want to do this for long texts you should first check if the content has really changed before you reload it.

You can store the timestamp of the last request in $_SESSION and compare it with the results of the filetime function.

If the content changed return a json encoded object with the property changed=true and the property newtext= in your seond script. If nothing changed just return a json object with changed=false.

http://php.net/manual/en/function.filemtime.php

Upvotes: 2

Related Questions