0

I am a complete noob at php sessions. I've gotten them working before, but never on a live server, just on my local testing environment.

My problem is that the $_SESSION variable is always an empty array (not undefined).

This is for a login form that is making an AJAX call to a php file.

Here is the AJAX call:

$.ajax({
        url: 'xhr/login.php',
        data: $(this).serialize(),
        type: 'post',
        dataType: 'json',
        success: function(result){
            if (result.success){
                console.log(result);
            };
        },
        error: function(e){console.log("Could not retrieve login information")}
    });

Here are the relevant parts of the login script:

# Start the user session
if(!isset($_SESSION)) {
    session_start();
    session_regenerate_id();
};

# Set our session values
                WHILE($session_row = mysql_fetch_assoc($session_result)){
                    $_SESSION['id'] = $session_row['id'];
                    $_SESSION['last_login'] = $session_row['last_login'];
                    $_SESSION['username'] = $session_row['username'];
                    $_SESSION['signup_date'] = $session_row['signup_date']; 
                };

I have the returning Json returning the session variable as:

echo json_encode(array("success"=>"user logged in", "session"=>$_SESSION));

but when I log the result.session variable to the console, it simply returns an empty array.

2
  • 1
    Why is while capitalized? Where are you making this database query? Commented Jan 7, 2012 at 18:04
  • where is $session_result being generated? If no errors are being generated, it may be that "$session_row = mysql_fetch_assoc($session_result)" never returns true, so the loop is ignored. Also, as Charles asked, it should be "while", not WHILE. Commented Jan 7, 2012 at 18:05

1 Answer 1

1

My problem is that the $_SESSION variable is always an empty array (not undefined).

That's totally normal for every session that has been successfully started.

session_regenerate_id();

ouch. ouch. ouch. Why do you regenerate the id out of the blue on every request?

Remove that line. It might also cause your problem.

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

2 Comments

Apparently changing to this: $session_result = mysql_query($session_sql); $returned_rows = mysql_fetch_assoc($session_result); IF(count($returned_rows) > 0 && count(mysql_fetch_assoc($session_result)) < 2){ from this: $session_result = mysql_query($session_sql); IF(count(mysql_fetch_assoc($session_result)) != 0 && count(mysql_fetch_assoc($session_result)) < 2){ $returned_row = mysql_fetch_assoc($session_result); was the key. Not really sure why that worked?
I don't have either ;) You're using an own session save handler probably?

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.