Using Advanced Functions to Maintain the State of Applications with PHP Sessions - Going deeper into PHP session management: creating a MySQL-based session storage module (
Page 3 of 4 )
After demonstrating how the “session_set_save_handler()” can be used to create a personalized session handling mechanism, I’ll go one step further by creating a new set of callback functions, which uses a MySQL database table for managing sessions. First, the definition for the corresponding callback functions is listed below:
// define 'openSession()' function
function openSession($sessionPath,$sessionName){
return true;
}
// define 'closeSession()' function
function closeSession(){
return true;
}
// define 'readSession()' method
function readSession($sessionId){
global $db;
// escape session ID
if(!get_magic_quotes_gpc()){
$sessionId=mysql_real_escape_string($sessionId);
}
$result=$db->query("SELECT sessiondata FROM sessions WHERE
sessionid='$sessionId' AND expiry > NOW()");
if($result->countRows()>0){
$row=$result->fetchRow();
return $row['sessiondata'];
}
// return empty string
return "";
}
// define 'writeSession()' function
function writeSession($sessionId,$sessionData){
global $db;
$expiry=time()+get_cfg_var('session.gc_maxlifetime')-1;
// escape session ID
if(!get_magic_quotes_gpc()){
$sessionId=mysql_real_escape_string($sessionId);
}
$result=$db->query("SELECT sessionid FROM sessions WHERE
sessionid='$sessionId'");
// check if a new session must be stored or an existing one
must be updated
($result->countRows()>0)?$db->query("UPDATE sessions SET
sessionid='$sessionId',expiry='$expiry',
sessiondata='$sessionData' WHERE sessionid='$sessionId'"):$db-
>query("INSERT INTO sessions (sessionid,expiry,sessiondata)
VALUES ('$sessionId','$expiry','$sessionData')");
return true;
}
// define 'destroySession()' function
function destroySession($sessionId){
global $db;
// escape session ID
if(!get_magic_quotes_gpc()){
$sessionId=mysql_real_escape_string($sessionId);
}
$db->query("DELETE FROM sessions WHERE
sessionid='$sessionId'");
return true;
}
// define 'gcSession()' function
function gcSession($maxlifetime){
global $db;
$db->query("DELETE FROM sessions WHERE expiry < NOW()");
return true;
}
As you can see, in the above example I defined the six callback functions that will be passed as arguments to the “session_set_save_handler(),” in such a way that they use a simple MySQL database table in order to read and write session data. This database table stores session IDs, the serialized session data, and finally a timestamp, which can be useful for assigning a time expiration to a particular session, so it can be deleted from the mentioned database table when the PHP garbage collection mechanism is appropriately triggered. (Notice that this situation may vary from system to system, in accordance with some settings of the session module within the php.ini file, such as the “session.gc_maxlifetime” and “session.gc_probability” directives).
Also, notice that most of the callback functions use a global $db variable, in order to have access to an instance of a MySQL wrapping class, which is utilized to connect to MySQL, together with performing all the SQL queries against the respective database table. Also, a MySQL result set processing class is used internally, in this case represented by the $result variable. Regarding these classes, in the next section I’ll show you their corresponding definition, thus you can have at hand all the source code required for implementing the previous MySQL-based session mechanism. Keep on reading to learn more.
| | Discuss Using Advanced Functions to Maintain the State of Applications with PHP Sessions | | | | | | | In this part of the series, you'll learn how to tweak the PHP session storage... | | | | | | Hi Alejandro, I greatly appreciate your efforts in bringing us these useful... | | | | | | Hi Rafael,
Thank you for commenting on my PHP article. Regarding your question,... | | | | | | Hi Alejandro, thank you so much for the example, it worked to perfection. I'm so... | | | | | | Hi Rafael,
Thank you for the kind word on my PHP articles, and certainly I feel... | | | | | | Hello again Alejandro, I run into a bug as I got my application onto the production... | | | | | | Hi Rafael,
Thank you for posting your comments. Concerning your particular... | | | | | | Hi Alejandro, a million thanks for your help. I stayed awake for a long time last... | | | | | | Hi Rafael. I'm glad to know you fixed up the problem regarding the use of mysqli and... | | | | | | Hi alejandro
Its me again, ive been making your tutorial as my book, im working... | | | | | | Hi Alejandro,
thanks for this wonderful tutorial.
When i tried to use your... | | | | | | Hi Roy,
Thanks for posting your comments, and I’m glad to know you’re learning... | | | | | | Thansk again for the comments. In fact, $id isn't necessary when coding this... | | | | | | Hello Alejandro,
Thanks for the prompt reply. Im still confused, on your reply you... | | | | | | Hello Alejandro,
I temporarily placed this code inside the writeSession()... | | | | | | Hi Roy,
with your last example, I see the possible reason of your problem.... | | | | | | Hello alehandro...
i set up my expiry as a timestamp, and that the expiry colum... | | | | | | Hello Roy,
Thanks for the comments. Using "time()" to insert your timestamps is a... | | | | | | >>> Post your comment now! | | | | | |
|
 |
|