如何仅在第一次重新加载页面

时间:2016-02-12 19:06:09

标签: javascript php jquery html

首次加载页面时,我需要立即重新加载或刷新Page(index.php)。因为google.com将网址提供给我的网页,其中没有更多数据,例如index.php?id = 10。所以,我需要在第一次将url恢复为index.php。我需要以简单的方式解决问题。请帮忙吗?

2 个答案:

答案 0 :(得分:1)

我建议您使用$_SESSION全局数组,因为它允许您轻松地将信息从一个页面传递到另一个页面(或同一个页面,如本例中)。但请确保在您使用它的每个页面上初始化会话。

代码应该是这样的:

session_start();    //Important! Without this, $_SESSION doesn't work

//reload_index is a variable I'm using in the array, nothing restricted; you can use whichever name you like
if (!isset($_SESSION['reload_index']) || ($_SESSION['reload_index'] == 'yes'))
{
    $_SESSION['reload_index'] = 'no';
    header("Location: index.php");    //Or whatever page you want to go; you can add parameters as well, like index.php?id=10
}

//...Rest of the page

我希望这可以帮助您解决问题。最好的问候。

答案 1 :(得分:0)

检查会话中是否设置了标志。如果没有,请设置并重新加载页面。 简单的伪代码示例:

session_start();

if (!isset($_SESSION['redirect_flag'])) {
  $_SESSION['redirect_flag'] = true;
  header("Refresh:0; url=index.php");
}
相关问题