Okay. We've intercepted PHP's session handling logic, and are systematically replacing it with our own. After the open and close, the next to areas to address are the read and write methods. Let's first take a look at the read method: function read( $id ) { // Set empty result // Fetch session data from the selected database $time = time(); $newid = mysql_real_escape_string($id); $rs = db_query($sql); if($a > 0) { return $data; } In the above example, you will see that I used functions called db_query(), db_num_rows(), and db_fetch_assoc(). These functions are from the application I wrote this class for. But a close look will show you that when the function is called, the unique session identifier is passed along with it. I then query the database to see if I can find a record for that session that has not expired. If successful, you return the data to the calling program. Now take a look at the code to write the data. function write( $id, $data ) { // Build query $newid = mysql_real_escape_string($id); $sql = "REPLACE `sessions` $rs = db_query($sql); return TRUE; } In the above example, you see that the write function is passed the unique session identifier, as well as the data to save to the database. One thing to note is what we are doing with the time. We grab the current time, then add to it the number of seconds that were defined in the constructor as lifetime. So basically, each time the data is written, we reset the timeout. So if your system is configured to expire sessions after 20 minutes of inactivity, this code supports it. You will also notice that, when writing the database, we utilize the replace function instead of an insert. Replace works exactly like an insert if the record already exists, or only updates it. And assuming all went well with the update, we return true.
blog comments powered by Disqus |
|
|
|
|
|
|
|