views:

163

answers:

2

I need a PHP script running on my server to frequently request a web page.

I thought of using the PHP header function combined with a meta refresh tag, but that won't work because the header will redirect to the URL immediately, and the meta refresh will never execute.

<?php
header('Location: http://www.example.com/');
?>
<html>
 <META HTTP-EQUIV=Refresh CONTENT="60">
</html>

Does anyone have any suggestions for how to do this please?

+1  A: 

If I understand your question correctly, you want to automatically refresh a page each time after a certain timeout.

The header generated by the php code is intended to directly redirect to some URL, so take it out because you will not be able to set a time out that way. It is the client (that is, the webbrowser that views the page) that must reload the page after some timeout, not your webserver. Server side scripting (such as PHP) will not help you therefore.

The client can be instructed to reload the page using that <META ...> tag, or with some javascript:

<script type='text/javascript'>
setTimeout(function () {
  window.location.reload(true);
}, 60000); // reload after 60 seconds
</script>
catchmeifyoutry
why the downvote?
catchmeifyoutry
Someone's downvoted all the answers on this question as well as the question itself...
Josh
yeah, I have the same feeling... just some troll. How about we upvote each other again ;)
catchmeifyoutry
@catchmeifyoutry: +1: Sure, why not? :-)
Josh
@Josh: Aight, i've (already) upvoted you too. Nice teamwork :p
catchmeifyoutry
@catchmeifyoutry: United we will defeat the [serial downvoters](http://meta.stackoverflow.com/questions/50304/serial-downvote-record)! =D
Josh
+2  A: 

If you want to do this using PHP alone, you'll need to change your solution a bit. Instead of sending an HTTP Location: header, redirecting the user away from the page, you'll want to load the remote contents into a variable yourself using file_get_contents. Then you can rewrite all the URLs and inject your refresh tag into the HTML, and output that.

A far easier solution would be to make an iframe and set that up to be refreshed using JavaScript. If you like I could provide a code sample, just ask.

Josh