繁体   English   中英

PHP 中的 ERR_TOO_MANY_REDIRECTS

[英]ERR_TOO_MANY_REDIRECTS in PHP

我的logout.php文件是这样的。 我的代码有没有错误

注销.php

<?php
session_start();
session_destroy();
header('Location:index.php');
exit;
?>

这是我的index.php文件。 如果我设置$_SESSION['s_activId']那么它工作正常但是当我试图放置条件时如果当时没有设置$_SESSION['s_activId']我想在索引页面上传递 header 有时它有时会起作用这是行不通的。

<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
    header("Location:index.php");
}
else
{
  $wrong = '';
  if(isset($_POST['submit']))
  {
    $checkLogin = "SELECT userName,password,userType
                     FROM user
                    WHERE BINARY userName = '".$_POST['userName']."'
                      AND BINARY password = '".$_REQUEST['password']."'";
    $checkLoginresult = mysql_query($checkLogin);
    if($userLoginRow = mysql_fetch_array($checkLoginresult))
    {
      $_SESSION['s_activId']   = $userLoginRow['userName'];
      $_SESSION['s_password']  = $userLoginRow['password'];
      $_SESSION['hg_userType'] = $userLoginRow['userType'];
     
     if(!$_SESSION['s_urlRedirectDir'])
      {
        header("Location:index.php");
      }
      else
      {
        header("Location:reminder.php");
      }
    }
    else
    {
      $wrong = "UserId And Password Is Not Valid";
    }
  }
}
include("bottom.php");
$smarty->assign('wrong',$wrong);
$smarty->display("index.tpl");
?>

问题出现在index.php的以下情况中:

if(!isset($_SESSION['s_activId']))
{
    $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
    header("Location:index.php");
}

注销时,您在 logout.php 上调用session_destroy()并在index.php上重定向,并且上述条件变为true ,因为s_activId中未设置 s_activId,并且您再次在index.php上重定向(未在会话中设置s_activId )。 在 session 中设置变量s_activId之前,上述条件将为真,因此您会收到ERR_TOO_MANY_REDIRECTS错误。

解决方法是,在index.php上设置session中的变量s_activId ,然后再调用header方法。 参考下面的代码:

if(!isset($_SESSION['s_activId']))
{
    $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
    $_SESSION['s_activId'] = true;
    header("Location:index.php");
}

不要将 index.php 重定向到 index.php。 你有重定向循环。 此外,如果您有下面的代码,那么也可以触发 add die in if,因为在重定向之后,下面的代码仍然会执行。 我没有阅读您的代码,也许这没有问题,但是之后

header("Location: lalala"); always add die(); or exit();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM