【问题标题】:localhost redirected you too many times when using header ()localhost 在使用 header() 时重定向了你太多次
【发布时间】:2016-11-23 09:48:52
【问题描述】:

您好,当我尝试将重定向发送到登录页面时出现此错误。 这是我的代码...

本地主机页面不工作

localhost 将您重定向了太多次。尝试清除您的 cookie。 ERR_TOO_MANY_REDIRECTS

我正在尝试的任何东西都会给我这个错误。

任何建议将不胜感激,谢谢!

<?php 
if(!$session->is_signed_in()){

    header("Location:login.php");


    } else{
         header("Location:logout.php");

} ?>

还有这个

class Session{

    private $signed_in = false;
    public $user_id;

    public function __construct(){
            session_start();
        $this->check_the_login();

    }
// check the value of signed in property - getter method

    public function is_signed_in(){

        return $this->signed_in;
    }

// login method 

    public function login($user){

        if($user){
            $this->user_id = $_SESSION['user_id'] = $user->id;
            $this->signed_in = true;
        }


    }
// log out method 

    public function logout(){

        unset($this->$_SESSION['user_id']);
        unset($this->user_id);
        $this->signed_in = false;


    }


// check the login method

    private function check_the_login(){

            if(isset($_SESSION['user_id'])){

                $this->user_id = $_SESSION['user_id'];
                $this->signed_in = true;

            }else{

                unset($this->user_id);
                $this->signed_in = false;
            }

        }
    }

$session = new Session();

【问题讨论】:

  • exit()放在header()之后
  • 第一段代码来自哪个页面? login.php?
  • 我将 exit() 放在标题之后,我得到了相同的结果。 @Jon - 第一段代码是 index.php 标头的一部分
  • 开始会话 session_start();
  • @Cristi 在您的开发工具中,在网络下,您应该看到正在发生的重定向。正在重定向到哪些文件?

标签: php


【解决方案1】:

好像你的注销方法有错误。unset($this-&gt;$_SESSION['user_id']);这一行换成unset($_SESSION['user_id']);试试

public function logout(){
    unset($this->$_SESSION['user_id']);
    unset($this->user_id);
    $this->signed_in = false;
}

应该如下所示

public function logout(){
    unset($_SESSION['user_id']);
    unset($this->user_id);
    $this->signed_in = false;
}

【讨论】:

    【解决方案2】:

    尝试在您的表单中使用POST 请求。我在收到get 请求后使用重定向时遇到了这个问题。执行两个get 请求,一个来自form,一个来自redirect 会导致无限循环。

    【讨论】:

      猜你喜欢
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-26
      • 1970-01-01
      • 1970-01-01
      • 2016-10-20
      相关资源
      最近更新 更多