tags:

views:

46

answers:

4

I've done tons of redirects using PHP's header function. This one has stumped me.

On my dashboard controller, I check whether or not the $_SESSION['loggedin'] is set. If it's not set, I want to send the user back to the main page. However, I keep getting the "too many redirects" error, even though I only have it set once. Can anyone help me out? Thanks for the help in advance!

Here's my code -

function index() {
    if(!isset($_SESSION['loggedin'])) {
        header("Location: ./");
    } else {
        die("The user is logged in.");
    }
}
+4  A: 

./ means "here", so yes, you're redirecting in a circle. You probably mean /, the root.

The Location header field should really contain a complete, absolute URL though. So you should redirect to http://example.com/. Relative URLs just happen to be (incorrectly) accepted by some browsers.

deceze
+2  A: 

This is because you just refresh the page. It means the user isn't redirected to different URL, he stays where he were.

You're using wrong path for Location header. ./ is equal to . which is a relative path and means current path. Certainly, you want to use / which is absolute path, ie. it's related to domain's root.

Crozin
A: 

I would use absolute paths it will reduce possible errors.

aromawebdesign.com
A: 

You might need to use global to access the session variables in your case.

fedmich