开发者

How do I frequently refresh a specific web page using PHP?

开发者 https://www.devze.com 2022-12-31 01:45 出处:网络
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 re

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>

Do开发者_运维百科es anyone have any suggestions for how to do this please?


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.


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消