PHP:“该网站有太多重定向"当使用 php 会话时 [英] PHP: "The website has too many redirects" when use php sessions with time

查看:54
本文介绍了PHP:“该网站有太多重定向"当使用 php 会话时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've a problem when use this code in my page:

带有过期会话的代码

<?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
}
?>

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

But if I use this code the problem disappears and the page works normally:

代码没有过期会话

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

谷歌浏览器中的错误:

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']),否则当客户端从重定向返回时会话中的值相同,将再次重定向.

You need to reset the $_SESSION value for timeout ($_SESSION['timeLogin']) when you execute redirection, otherwise when the client is back from redirect the value in session is the same and will be again redirected.

你可以用以下方法解决:

You could solve it with:

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_destroy())重新配置它.

Maybe (depending on your logic) is better clear the entire session, and let it be reconfigured through the normal flow (session_destroy()) when you perform redirect.

这篇关于PHP:“该网站有太多重定向"当使用 php 会话时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆