Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hello,

 

Just read through sessions in Ch3. There is one thing I did not get. How do I obtain the session_id(). It seems to be always empty. I even tried to set it in the read_session() but still no effect. Is this the expected behavior or am I missing anything?

 

Cheers

Link to comment
Share on other sites

You need to start the session before trying to read the session id. Did you do that?

 

Everything is as per the book and works. The only thing I've added to the read_session() function is the following line of code:

session_id($sid);

This is in the db_sessions.inc.php file. Session is started at the end of the file and the whole file is included into sessions.php where I try to print session_id() but nothing.

Link to comment
Share on other sites

Remove the param $sid from session_id() and see what that gives you. You should only specify the param when you want to use it as a setter, and leave it blank when using it as a getter function.

 

I might also have misunderstood you, but to get values from a function, you need to return something. Make sure the function looks like this:

function read_session()
{
   return session_id();
}

Hope that helps you out.

Link to comment
Share on other sites

Hi Antonio,

 

read_session() expects $data to be returned as per the book and the manual so I cannot return session_id(). As per the manual if I am not using database for sessions I can just issue print session_id() and it would print the session hash. But when I switch to database sessions the session_id() function seems to loose its ability and I can't figure out why.

Link to comment
Share on other sites

Ah sorry. I've been think all the time that you actually have the book. So this is the function that reads the session from DB located in db_sessions.inc.php [i am providing file names for consistency]:

// Define the read_session() function:
// This function takes one argument: the session ID.
// This function retrieves the session data.
// This function returns the session data as a string.
function read_session($sid) {
    global $sdbc;
    session_id($sid);

    // Query the database:
    $q = sprintf('SELECT data FROM sessions WHERE id="%s"', mysqli_real_escape_string($sdbc, $sid));
    $r = mysqli_query($sdbc, $q);

    // Retrieve the results:
    if (mysqli_num_rows($r) == 1) {
        list($data) = mysqli_fetch_array($r, MYSQLI_NUM);

        // Return the data:
        return $data;

    } else { // Return an empty string.
        return '';
    }
} // End of read_session() function.

db_sessions.inc.php starts the session at the end of the file. This file is included in another file called sessions.php Now If i print session_id() in the sessions.php file it returns an empty string. I am failing to understand why!

 

As you can see php passes the session id hash to this function when reading the session. As per the manual: 

http://php.net/manual/en/function.session-id.php

 

one can print the session hash with print session_id() and this is working fine using the default session mechanism of php. When I however change to store the session into DB as per Larry's book session_id() seems to stop functioning. So once you start the session php seems to correctly record it into DB. But if I decide to get the session hash it does not work.

Link to comment
Share on other sites

Ok. That function declaration looks good. How do you call that function? Also, as I said earlier, you cannot use the parameter in session_id() if you want to get the session value. When you do that, you basically set a new session_id for the user.

 

I'm also a little skeptical about you starting the session in the bottom of the file. That might work if everything is encapsulated in functions, but it will fail if you call read_session() before session_start() is called. Try something along these lines where you read the session.

// Start session
session_start();

// Read session data
$session = read_session(session_id());

// Simple debug, remove once working
echo '<pre>', print_r($session) , '</pre>';
  • Upvote 1
Link to comment
Share on other sites

In the tutorial from the book we use this function and a couple of other function like this:

// Declare the functions to use:
session_set_save_handler('open_session', 'close_session', 'read_session', 'write_session', 'destroy_session', 'clean_session');

// Make whatever other changes to the session settings, if you want.

// Start the session:
session_start();

This is actually the end of the db_sessions.inc.php file. So I don't call this function directly but php does when I call $_SESSION

Link to comment
Share on other sites

Ok. That makes more sense then. All the other functions listed in the handler is obviously defined too? Make sure you don't get any error by including this in the top of one of your files:

ini_set('display_errors', 1);
error_reporting(-1);

One thing I notice is that the callable you define for reading session (ie. read_session()) has to return serialized data. You currently return an array. Try calling serialize($data) where you return the data from your DB. Aka:

// Return the data:
return serialize($data);

This data will be unserialized for you automaticly and will populate the $_SESSION global. However, this callback function needs to be returned serialized.

Link to comment
Share on other sites

Hi Antonio,

 

$data is already serialized as it comes directly from DB. And as per PHP manual it is written there in a serialized form. The error reporting did not bring anything new. Just got an idea how to present you the whole picture. You may download the actual code of the book from here:

 

http://www.larryullman.com/downloads/phpvqp3_scripts.zip

 

Unzip it and navigate to Ch03 folder (unfortunately this requires an active DB connection and session table) but you may get around it. So all the functions are in db_session.inc.php What I am trying to achieve is to run sessions.php and print the session_id(). In my case it is always an empty string and I am fighting to understand why.

 

Thanks for your help so far!

Link to comment
Share on other sites

No problem. I'm on an iPad right now, so I can't check the files until tonight.

 

I suggest you apply some debug techniques. First, echo session_id() before an after invoking read_session() through using $_SESSION. Then try echo out $sid somewhere inside read_session() to make sure you get a value. If those are empty, I would suggest reading more about how session_start(); affects everything and would maybe try to move it around.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...