Jump to content
Larry Ullman's Book Forums

Recommended Posts

I have followed the book closely and looked over the code to make sure it matches Larry's. However, no matter what I do, I am not able to load the session into the database when I run the sessions.php file as per the book. I was hoping on some help to figure out what the issue is.

 

I am running PHP 5.4.10 and Mysql 5.5.29. Thanks for the help in advance!

 

******This is my db_sessions.inc.php file:*********

<?php # Script 3.1 - db_sessions.inc.php


/* 
 *  This page creates the functional interface for 
 *  storing session data in a database.
 *  This page also starts the session.
 */


// Global variable used for the database 
// connections in all session functions:
$sdbc = NULL; //diff than DBC bc this is meant for sessions... make global for session connections (does not have to be and in fact would only use one in general so change code accordingly)


function open_session() {
    global $sdbc;
    $sdbc = mysqli_connect( 'localhost', 'root', 'pass', 'advPHP' ) ;
    return true; //always return somethign except for read function (indicate success Boolean rather than just true)
}


function close_session() {
    global $sdbc;
    return mysqli_close( $sdbc );
}


function read_session( $sid ) {
    global $sdbc;
    $q = sprintf( 'SELECT data FROM sessions WHERE id="%s"', mysqli_real_escape_string($sdbc, $sid) );
    $r = mysqli_query( $sdbc, $q );
    if( mysqli_num_rows($r) == 1) {
        list($data) = mysqli_fetch_array($r, MYSQLI_NUM);
        return $data;
    } else {
        return '';
    }
}


function write_session($sid, $data) {
    global $sdbc;
    $q = sprintf( 'REPLACE INTO sessions (id, data) VALUES("%s", %s")', mysqli_real_escape_string($sdbc, $sid), mysqli_real_escape_string($sdbc, $data) );
    $r = mysqli_query($sdbc, $q);
    return true;
}


function delete_session( $sid ) {
    global $sdbc;
    $q = sprintf( 'DELETE FROM sessions WHERE id="%s"', mysqli_real_escape_string($sdbc, $sid) );
    $r = mysqli_query( $sdbc, $q );
    $_SESSION = [];
    // return mysqli_affected_rows($sdbc) ;
    return true;
}
function clean_session($expire) {
    global $sdbc;
    $q = sprintf('DELETE FROM sessions WHERE DATE_ADD (last_accessed, INTERVAL %d SECOND) < NOW()', (int) $expire);
    $r = mysqli_query($sdbc, $q);
    return true;
}


session_set_save_handler( 'open_session', 'close_session', 'read_session', 'write_session', 'delete_session', 'clean_session' );
session_start();
 
******And this is my sessions.php file********
<?php # Script 3.2 - sessions.php


/*  This page does some silly things with sessions.
 *  It includes the db_sessions.inc.php script
 *  so that the session data will be stored in a database.
 */


// Include the sessions file:
// The file already starts the session.
require('db_sessions.inc.php');
?><!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>DB Session Test</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<?php
print_r($_SESSION);
// Store some dummy data in the session, if no data is present:
if( empty($_SESSION) ) {
    $_SESSION['blah'] = 'umlaut';
    $_SESSION['this'] = 3615684.45;
    $_SESSION['that'] = 'blue';
    echo '<p> Session data stored. </p>';
} else {
    echo '<p>Curren session contains <pre>' . print_r($_SESSION, 1) . '</pre> </p>';
}
if( isset($_GET['logout']) ) {
    session_destroy();
    echo '<p>Session destroyed.</p>';
} else {
    echo '<a href="sessions.php?logout=true"> Log Out </a>';


}
echo '<p> Session data: <pre>' . print_r($_SESSION, 1) . '</pre></p>';
echo '</body>
</html>';
session_write_close();
?>

 

Link to comment
Share on other sites

Thats just the thing. The script works fine. I can add the information to the $_SESSION array and it will show on the web page. I use the 

print_r()

 at the beginning of the script to see if the $_SESSION array is loaded or still empty after my reloading of the page. However, the $_SESSION stays empty. In other words, the else statement does not run, which must mean that its not getting loaded into the database because the script is not reading from the database the new values.

 

Likewise, if I check my database after running the script (the first time), the database is still empty.

 

Thanks for the help by the way!

Link to comment
Share on other sites

I had the same problem untill i found out i forgot to replace the database connection info with my own hahaha... I forgive myself because it's late at night :)

 

Have you checked php.ini to see if session.auto_start is turned off? In the book they mention that having it turned on means you cannot use the session_set_save_handler() function.

  • Upvote 1
Link to comment
Share on other sites

  • 1 month later...
 Share

×
×
  • Create New...