PHP: "The website has too many redirects"当使用 php session 时

标签 php session timeout

在我的页面中使用此代码时遇到问题:

带有过期 session 的代码

<?php 
session_start();
if(!isset($_SESSION['clientmacs']) ) { 
    header('Location: index.php');
} else {
    if(time() - $_SESSION['timeLogin'] > 1800) {
        header('Location: include/logout.php');
    }
    $userclient = $_SESSION['clientmacs'];
?>
<html>
    HTML CODE
</html>
<?php
}
?>

但是,如果我使用此代码,问题就会消失,并且页面可以正常工作:

没有过期 session 的代码
<?php 
session_start();
if(!isset($_SESSION['clientmacs'])) { 
    header('Location: index.php');
} else {
    $userclient = $_SESSION['client'];;
?>
<html>
    HTML CODE
</html>
<?php
}
?>

Google Chrome 中的错误:
This webpage has a redirect loop

Http://localhost/mac/index.php The website has too many redirects. The incidence may be
resolved by deleting the cookies from this site or allowing third party cookies. If
that fails, the incidence may be related to a bug in the server configuration, not the
computer.

最佳答案

您需要在执行重定向时重置 $_SESSION 超时值($_SESSION['timeLogin']),否则当客户端从重定向返回时, session 中的值是相同的并且将再次被重定向。

您可以通过以下方式解决它:

if(!isset($_SESSION['clientmacs']) ) {
    $_SESSION['clientmacs'] = ""; // add this line if not added somewhere else
    header('Location: index.php');
}


if(time() - $_SESSION['timeLogin'] > 1800) {
    $_SESSION['timeLogin'] = time(); // add this line
    header('Location: include/logout.php');
}

也许(取决于您的逻辑)最好清除整个 session ,并在您执行重定向时通过正常流程( session_destroy() )重新配置它。

关于PHP: "The website has too many redirects"当使用 php session 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11441650/

相关文章:

session_regenerate_id() : Cannot regenerate session id - session is not active

c# - 访问串行端口时信号量超时?

time - 为什么这个 Golang 代码不能在多个时间之间进行选择。 channel 工作后?

php - 跟踪非垃圾邮件的电子邮件点击次数

php - 无法将 javascript 变量传递到 Google Maps API

python - 如何为 session 和登录系统设置 Pyramid_beaker?

go - 在 Go 中取消阻塞操作

javascript - Codeigniter 中自动完成多个值

php - 覆盖 block 时避免翻译丢失

laravel - session 销毁或关闭浏览器选项卡或关闭浏览器后使用 Laravel 5.2 执行注销