Question

I have a text file storing one string. I anticipate that the text file will be changing frequently, so in order to keep my page up to date, I would like to use PHP (preferably) to fetch data from the text file every 20 seconds so I can explode it into an array and use the contents accordingly. The variables would also need to update every 20s.

So: on page load, the contents are fetched and displayed. But the contents of the text file may be changed thus making the page outdated while a user may already have it open.

I tried META Refresh, but the whole page refreshes in the middle of browsing and interrupts the user.

Sorry for the confusing description, it's hard to explain. :)

I've searched the web for ages and not found an answer to my question. Please remember I am using a text file and not MySQL, since I'm only storing one string.

Thanks in advance.

Était-ce utile?

La solution

If you want to stay with PHP, I'm afraid a refreshing HTML Meta is the solution :

<meta http-equiv="refresh" content="10; url=http://example.com/" />

Refresh the page every X seconds, so that the file gets reloaded. Another way could be the use of frames, however I cannot seriously recommand it to you.

However, you can load a content without reloading the whole page, using Ajax. It allows you to perform a HTTP request to the server (using a Javascript code) and place its result on the current page, using Javascript as well. You could create a PHP script "my_string_parsed.php", which reads the file, and then parses/prints its content. Then, you could call this script through an Ajax request to http://yoursite.com/my_string_parsed.php, and place its result in a specified HTML tag on your page.

W3Schools.com provides an Ajax tutorial here : http://www.w3schools.com/ajax/

A warning concerning Ajax though : an Ajax content loading must never replace the typical HTTP behavior your browser and the server have. If the string in your file is the only content on your page, then the best solution would be the refreshing meta. Ajax should only be used to refresh parts of a page. Never the whole thing.

Autres conseils

Why not using a database instead of a file. You could also use jQuery to update your page smoothly.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top