gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:56:59 25 4
gpt4 key购买 nike

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

带有过期 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com