0

I am trying to make the user get redirected to the index.html page in case the $_SESSION['loggedin'] is already set. For some reason, it doesn't redirect the user, although the $_SESSION['loggedin'] when echoed returns a value.

if(!empty($_SESSION['loggedin'])) { header('Location: index.html');};

What am I doing wrong here?

3
  • Do you get a warning error? If you try to header redirect after anything has been printed to the screen it will fail. Commented May 1, 2013 at 23:02
  • Are you sure session_start() isn't missing and $_SESSION['loggedin'] has a value, that does not return false when checked with empty()? Commented May 1, 2013 at 23:03
  • session_start() is right on top of the page, before any HTML. The line of code from the original post is right under the session_start(). When I echo $_SESSION['loggedin'] it returns a value that is set in the script. Commented May 1, 2013 at 23:10

1 Answer 1

2

header('Location: index.html'); sets an http header which needs to be set before any content is sent, i.e. before any echo. Also you should terminate your script after you set the location header.

Sign up to request clarification or add additional context in comments.

1 Comment

I added exit; after the header redirect and it worked. I guess the script continued to run and it caused some other redirects to trigger which are below that code. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.