Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 自动刷新页面_Php_Javascript_Jquery_Html - Fatal编程技术网

Php 自动刷新页面

Php 自动刷新页面,php,javascript,jquery,html,Php,Javascript,Jquery,Html,我将创建一个JQuery幻灯片,但需要每5分钟刷新一次页面以更改内容 我知道我可以通过一些Javascript来实现这一点,但这可以在客户端进行更改,以避免页面刷新。服务器是否有办法使页面超时并强制刷新?您无法强制从服务器重新加载页面 在JavaScript中执行此操作的方法很简单: setTimeout(function() { window.location.reload(); }, 5000); 虽然这可以在客户端被覆盖,但是reguler用户不会在你的脚本上乱搞 作为旁注:有

我将创建一个JQuery幻灯片,但需要每5分钟刷新一次页面以更改内容


我知道我可以通过一些Javascript来实现这一点,但这可以在客户端进行更改,以避免页面刷新。服务器是否有办法使页面超时并强制刷新?

您无法强制从服务器重新加载页面

在JavaScript中执行此操作的方法很简单:

setTimeout(function() {
    window.location.reload();
}, 5000);
虽然这可以在客户端被覆盖,但是reguler用户不会在你的脚本上乱搞



作为旁注:有什么原因让你如此坚持重新加载页面吗?

除了重新加载javascript之外,你还可以发送一个刷新标题:

header("Refresh: 300;url='http://thepage.com/example'");

不管javascript如何,浏览器都会在300秒后重定向。虽然可以在浏览器配置中禁用它,但通常不会禁用它。

元刷新将非常简单:

<meta http-equiv="refresh" content="300">


300表示它将每300秒或5分钟刷新一次。通过这种方式,您不使用JavaScript,因此用户无法关闭它。

另一种重新加载的方法是使用
窗口。位置

    setTimeout(function(){
        window.location.href = "YOUR_PAGE_URL";
    }, 5000);

JavaScript方法

var timer = null;

function auto_reload()
{
  window.location = 'http://domain.com/page.php';
}

<body onload="timer = setTimeout('auto_reload()',10000);">
The following refreshes the page every 30 seconds.

<head>
<meta http-equiv="refresh" content="30" />
</head>
var定时器=null;
函数auto_reload()
{
window.location=http://domain.com/page.php';
}

元标记方法

var timer = null;

function auto_reload()
{
  window.location = 'http://domain.com/page.php';
}

<body onload="timer = setTimeout('auto_reload()',10000);">
The following refreshes the page every 30 seconds.

<head>
<meta http-equiv="refresh" content="30" />
</head>
以下命令每30秒刷新一次页面。
使用PHP 我将使用一个php函数,它可以在任何页面上调用,而无需设置url

    // write the function
    function refresh( $time ){
        $current_url = $_SERVER[ 'REQUEST_URI' ];
        return header( "Refresh: " . $time . "; URL=$current_url" );
    }

    // call the function in the appropriate place
    refresh( 4 );   
    // this refreshes page after 4 seconds

目前的两个答案都是正确的:A)你不能从服务器上强制这样做,B)你可以用一个标题轻松地做到这一点。所以你担心你的客户侵入你的页面的javascript并改变行为?这是html和web的本质。你不能控制客户端。好吧,但是我很抱歉-这是客户端的机器,所以你不能强迫任何事情。按下Esc键可以轻松避免出现所提到的
刷新
标题。也许你的一小部分用户会修改javascript,这样赤字就不会那么严重了,是吗?不,不会那么严重。我只是想知道是否还有其他的选择/解决方法这正是Esailija的答案。我更喜欢真正的标题,它在源代码中看不到;POf当然PHP也可以很好地工作,是的,您可能不想不在源代码中包含它。我看不出有任何理由使用匿名函数。