views:

73

answers:

4

I have a facebook login system that seems to work fine, but I want to redirect to the home page if the user is logged in. SO this is the top of my page

<?php
session_start();

if(!empty($_SESSION)){
 header("Location: home.php");
}

But that gets ignored and it runs the login script as if $_SESSION is empty, even tho I print the Session array on the home page and it always shows it has values, plus the rest of the script assigns session values anyway. Even if I change it to if (empty($_SESSION)) or add a else { die()} after it, it just completely ignores it.

Now if I add anything to print the session either using print_r($_SESSION) or echo'ing a particular value, even if I place this out side of the if statement, it seems to realise that the $_SESSION has a value and does the redirect. I could just put that there permanently as it would never appear...but I would rather figure out why it isn't working now.

A: 

You should add die() or exit() in the if block or the rest of the script will continue executing.

banzaimonkey
A: 

if(!empty($_SESSION['loggedIn']){ header("Location: home.php"); die(); }

xXx
Seems OK to me. Try: `var_dump(empty($_SESSION));`
banzaimonkey
+3  A: 

If you don't interrupt the script it will, well, run until the end:

<?php
session_start();

if(!empty($_SESSION)){
    header("Location: home.php");
    exit;
}
Álvaro G. Vicario
Of course! *sigh* Brain hiccupThanks a lot everyone.
Michael Mallett
@Michael: don't forget to accept this answer.
janmoesen
A: 

You've got it pretty much right, it should look like this:

<?php
    session_start();

    if(isset($_SESSION)){
        header("Location: home.php");
        exit;
     }

Use isset() instead of empty(). Also, shouldn't the session var looks something like this?

$_SESSION['logged_on']; 
James Eggers
`isset()` will always return true in this case because you've preceded it with `session_start();` (unless something else is broken). `empty()` checks to see whether any data has been loaded from an existing session.
banzaimonkey