Cristi
Cristi

Reputation: 49

localhost redirected you too many times when using header ()

Hi am getting this error when I am trying to send a redirect to the login page. here is my code...

The localhost page isn’t working

localhost redirected you too many times. Try clearing your cookies. ERR_TOO_MANY_REDIRECTS

anything I am trying it's giving me this error.

any suggestions would be much appreciated, thank you!

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

    header("Location:login.php");


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

} ?>

and this

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();

Upvotes: 2

Views: 4810

Answers (2)

Try using a POST request in your form. I had this problem using a redirect after a get request from. Doing two get requests, one from the form, and one from the redirect causes an infinite loop.

Upvotes: 0

Saji
Saji

Reputation: 1394

Seems that your logout method has error. Replace this line unset($this->$_SESSION['user_id']); to unset($_SESSION['user_id']); and try

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

Should be like below

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

Upvotes: 2

Related Questions