在我的页面中使用此代码时遇到问题:
带有过期 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/