简体   繁体   English

PHP 中的 ERR_TOO_MANY_REDIRECTS

[英]ERR_TOO_MANY_REDIRECTS in PHP

My logout.php file is like this.我的logout.php文件是这样的。 Is there any mistake in my code我的代码有没有错误

logout.php注销.php

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

Here is my index.php file.这是我的index.php文件。 If I am set $_SESSION['s_activId'] then it is working properly but when I am trying to put condition if $_SESSION['s_activId'] is not set at that time I want to pass header on index page sometimes it works sometimes it does not work.如果我设置$_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");
?>

The problem arise in the condition below in index.php :问题出现在index.php的以下情况中:

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

When you logout, you are calling session_destroy() on logout.php and redirecting on index.php and the condition above gets true as s_activId is not set in session and again you are redirecting on index.php (without setting s_activId in session).注销时,您在 logout.php 上调用session_destroy()并在index.php上重定向,并且上述条件变为true ,因为s_activId中未设置 s_activId,并且您再次在index.php上重定向(未在会话中设置s_activId )。 The above condition will be true until the variable s_activId set in session and because of this you are getting ERR_TOO_MANY_REDIRECTS error.在 session 中设置变量s_activId之前,上述条件将为真,因此您会收到ERR_TOO_MANY_REDIRECTS错误。

The solution is, on index.php set the variable s_activId in session before calling the header method.解决方法是,在index.php上设置session中的变量s_activId ,然后再调用header方法。 Refer the code below:参考下面的代码:

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

Dont redirect index.php to index.php.不要将 index.php 重定向到 index.php。 you having redirects loop.你有重定向循环。 Also if you have code below that also can fire add die in if because after redirect code below still executes.此外,如果您有下面的代码,那么也可以触发 add die in if,因为在重定向之后,下面的代码仍然会执行。 I didnt read your code, maybe there isnt problems with this but after我没有阅读您的代码,也许这没有问题,但是之后

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

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

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