tekina tekina - 4 months ago 9
PHP Question

PHP login/signup 'too many redirects' error (snippet attached)

I've just started with PHP, and I'm getting this annoying error which I can't seem to get rid of.

Relevant snippets:

index.php
:

if(isset($_SESSION['isLoggedIn']) && $_SESSION['isLoggedIn']===true){
$isLoggedIn = true;
}else{
$_SESSION['isLoggedIn'] = false;
header('Location: /signup');
}


signup.php
:

if(isset($_SESSION['isLoggedIn']) && $_SESSION['isLoggedIn']===true){
header('Location: /');
} else {
$_SESSION['isLoggedIn'] = false;
}


I get redirected to signup.php page, but after that get
too many redirects
error. In server logs, there are just
302
redirects to
signup.php
page. I'm not redirecting anywhere else in signup.php.

Where could this error come from?

Answer

Your header('Location: /signup'); is failing because the file is actually called signup.php and /signup is a folder.

and is interpreted as: /go_to_root/signup/index.php

Since that folder doesn't exist, it's looping over from a most likely 404 and to an index file.

Therefore, you need to make it read as

header('Location: /signup.php');

and add exit; after each header, otherwise, your code will continue to execute.

Comments