Jump to content
Larry Ullman's Book Forums

Chapter 3 Sessions Example Didn't Quite Work


Recommended Posts

Hello,
 
I have been following along with this great PHP Advanced book.  I have gotten to the Sessions part in Chapter 3 and typed out all the code, including the db_sessions.inc.php file and the sessions.php file, and included the db_sessions.inc.php file with the require() function but It doesn't seem to be working properly.  
 
When I run the code in the browser, it echos that session data is stored (the dummy data that is entered when the page is first loaded), but then doesn't changed to session data exists when the page is reloaded.
 
I think its having trouble reading the included file but can't see why.
 
Here is the code from the db_sessions.inc.php file
 

<?php
 
# Script 3.1 - db_sessions.inc.php
 
 
 
// // initialise the database connection string variable.
 
$sdbc = NULL;
 
// define the function for opening a session
 
function open_session()
{
global $sdbc;
$sdbc = mysqli_connect('localhost','root','root', 'advanced_php');
return true;
}
 
// define function for closing session
 
function close_session()
{
global $sdbc;
return mysqli_close($sdbc);
}
 
// define function for reading session data
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 '';
}
}
 
// define a function for writing data to the database
function write_session($sid, $data)
{
global $sdbc;
$q = sprintf('REPLACE INTO sessions (id, data) VALUES ("%s","%s")', mysqli_real_escape_string($sdbc, $data));
$r = mysqli_query($sdbc, $q);
return true;
}
 
// function to destroy session data
function destroy_session($sid)
{
global $sdbc;
// delete the session from the database
$q = sprintf('DELETE FROM sessions WHERE id="%s"',mysqli_real_escape_string($sdbc, $sid));
$r = mysqli_query($sdbc, $q);
 
// clear the $_SESSION array
$_SESSION = array();
return true;
}
 
//define the garbade collection function
function clean_session()
{
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;
}
 
// Tell PHP to use the session hadnlinf functions
session_set_save_handler('open_session','close_session','read_session','write_session','destroy_session','clean_session');
 
// start the session
session_start();

and this is the code from the sessions.php file

 

<?php


# Script 3.2 - sessions.php


// include the db_sessions.php file
require_once('db_sessions.inc.php');


?>
<!doctype HTML>
<html>
<head>
     <meta charset="utf-8" />
        <title>DB Sessions Test</title>
    </head>
    <body>
     <?php


// store some dummy data in a session if it is currently empty
if(empty($_SESSION))
{
$_SESSION['blah'] = 'umlaut';
$_SESSION['this'] = 3615684.45;
$_SESSION['that'] = 'blue';
echo '<p>Session data stored.</p>';
}
else
{
echo '<p>Session data exists: <pre>' . print_r($_SESSION,1) . '</pre></p>'; 
}


// create the logout functionality
if(isset($_GET['logout']))
{
session_destroy();
echo '<p>Session Destroyed</p>'; 
}
else
{
echo '<a href="sessions.php?logout=true">Log Out</a>'; 
}


// Print the session data
echo '<p>Session Data:<pre>' . print_r($_SESSION,1) . '</pre></p>';


echo '</body></html>';


session_write_close();
?>

Could anyone tell me where I'm going wrong, I'd just like to understand why it didn't work as expected.

 

Thank you

John

 
Link to comment
Share on other sites

I think your code is ok. I believe you are stuck on a functional issue. When you click on the Logout link your session data (displayed on the screen) is deleted from the database - session destroyed. You need to close your browser without Logging out - session will still be active...in the database because you didn't Logout. Then run sessions.php script. You'll see 'sessions exists' because you didn't formally logout. I apologize if I'm telling you what you know already. I had to run the script a few times before I figured it out.  :lol:

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...