Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- MySQL Session storage. Designed to work with the following table:
- CREATE TABLE IF NOT EXISTS `phpsessions` (
- `SESSID` int(11) NOT NULL,
- `data` text CHARACTER SET utf8 NOT NULL,
- `last` int(11) NOT NULL,
- UNIQUE KEY `SESSID` (`SESSID`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
- `last` is a unix timestamp, hence INT.
- Take note that in it's current form this system wont support hexadecimal session IDs, which mean the default
- IDs, assigned by PHP will be trimmed. This is done, because SESSID has to be a unique column, and this is only
- achievable with columns of type INT. If you wish to employ a TEXT column, you'll have to modify the _write
- function to accomodate detection of existing records other than ON DUPLICATE UPDATE, since this only works with
- unique fields.
- Fucntion names are self-explanatory.
- Debugging mode is enabled, uncomment the specified lines in order to disable it.
- */
- ini_set('session.gc_probability', 1); // Set to 1000 to make sure cleanup fires every time
- ini_set('session.gc_divisor', 1000);
- ini_set('session.gc_maxlifetime', 1440); // Reduce to see cleanup in action
- // Main config function (of sorts)
- function __DB($string)
- {
- // Set access credentials
- $db_host = "yourhost";
- $db_user = "yourname";
- $db_pass = "yourpass";
- $db_name = "database"; // Database name
- switch($string)
- {
- case "host":
- return $db_host;
- case "user":
- return $db_user;
- case "pass":
- return $db_pass;
- case "name":
- return $db_name;
- }
- return '';
- }
- ### BEGIN CUSTOM SESSION RECORDING FUNCTIONS ##################################################
- function _open($sess_path, $sess_name)
- {
- // Since a session is openned on every page load, you might want to disable this anyway
- // Output debug information to a file, uncomment next line to disable
- // /*
- $file=fopen("debug.txt","a");
- $ostring = time() . " :: Openning session\n";
- fwrite($file,$ostring);
- fclose($file);
- // */
- // Must always return true; if persistent connections are possible, setup the use of one here
- // You may also generate your own ID here, just make sure it's compatible with the var type of the db column
- // Since the sonctructor takes two parameters, you may want to use them, in case you forked your code to use
- // several locations to store diffirent sessions or for whatever crazy reason you might. Don't remove them
- // From the paramether list, though, it will break things
- return true;
- }
- function _close()
- {
- // Output debug information to a file, uncomment next line to disable
- // /*
- $file=fopen("debug.txt","a");
- $ostring = time() . " :: Closing session\n";
- fwrite($file,$ostring);
- fclose($file);
- // */
- // Must always return true; take into account the use of persistent connections here
- return true;
- }
- function _read($id)
- {
- // Open connection to database
- $_sess_db = mysql_connect(__DB("host"), __DB("user"), __DB("pass"));
- mysql_select_db(__DB("name"), $_sess_db);
- // Make sure data is properly escaped and not mangled by the ini settings
- if(get_magic_quotes_gpc()) $id = stripslashes($id);
- $id = mysql_real_escape_string($id);
- $last = time();
- $data = '';
- // Get requested data
- $result = mysql_query("SELECT * FROM `phpsessions` WHERE `SESSID` = '{$id}'");
- while($sess_data = mysql_fetch_array($result))
- {
- $data = $sess_data['data'];
- }
- mysql_close($_sess_db);
- // Output debug information to a file, uncomment next line to disable
- // /*
- $file=fopen("debug.txt","a");
- $ostring = time() . " :: READ << ID: {$id} :: Data: {$data}\n";
- fwrite($file,$ostring);
- fclose($file);
- // */
- // Resulting data is a serialized array, which is exactly what we need. Do not process it, PHP takes care of everything.
- return $data;
- }
- function _write($id, $data)
- {
- // Open connection to database
- $_sess_db = mysql_connect(__DB("host"), __DB("user"), __DB("pass"));
- mysql_select_db(__DB("name"), $_sess_db);
- // Make sure data is properly escaped and not mangled by the ini settings
- if(get_magic_quotes_gpc())
- {
- $id = stripslashes($id);
- $data = stripslashes($data);
- }
- $id = mysql_real_escape_string($id);
- $data = mysql_real_escape_string($data);
- $last = time();
- // Modify database; PHP gives us a serialized array to begin with, so we're cool without processing it.
- $result = mysql_query("
- INSERT INTO `phpsessions` (`SESSID`,`data`,`last`)
- VALUES ('{$id}','{$data}','{$last}')
- ON DUPLICATE KEY UPDATE
- `data` = '{$data}',
- `last` = '{$last}'
- ");
- mysql_close($_sess_db);
- // Output debug information to a file, uncomment next line to disable
- ### NOTE: Changes to the database wont be visible in the debug until the database is flushed. The database, however, will change in real time ###
- // /*
- $file=fopen("debug.txt","a");
- $ostring = time() . " :: WRITE >> ID: {$id} :: Data: {$data}\n";
- fwrite($file,$ostring);
- fclose($file);
- // */
- return $result;
- }
- function _destroy($id)
- {
- // Output debug information to a file, uncomment next line to disable
- // /*
- $file=fopen("debug.txt","a");
- $ostring = time() . " :: Destroying session: {$id}\n";
- fwrite($file,$ostring);
- fclose($file);
- // */
- // Open connection to database
- $_sess_db = mysql_connect(__DB("host"), __DB("user"), __DB("pass"));
- mysql_select_db(__DB("name"), $_sess_db);
- // Make sure data is properly escaped and not mangled by the ini settings
- if(get_magic_quotes_gpc()) $id = stripslashes($id);
- $id = mysql_real_escape_string($id);
- // Preform requested operation
- $result = mysql_query("DELETE FROM `phpsessions` WHERE `id` = '{$id}'");
- mysql_close($_sess_db);
- return $result;
- }
- function _clean($max)
- {
- // Open connection to database
- $_sess_db = mysql_connect(__DB("host"), __DB("user"), __DB("pass"));
- mysql_select_db(__DB("name"), $_sess_db);
- // Determine deletion treshhold
- $max = (int)$max;
- $old = time() - $max;
- // Finalize cleanup
- $result = mysql_query("DELETE FROM `phpsessions` WHERE `last` < '{$old}'");
- mysql_close($_sess_db);
- // Output debug information to a file, uncomment next line to disable
- // /*
- $file=fopen("debug.txt","a");
- $ostring = time() . " :: Cleaning up sessions older than: {$old}\n";
- fwrite($file,$ostring);
- fclose($file);
- // */
- return $result;
- }
- // Initialize our custom handler; you might want this at the beginning of the file, but it might cause problems
- session_set_save_handler('_open',
- '_close',
- '_read',
- '_write',
- '_destroy',
- '_clean');
- ### END CUSTOM SESSION RECORDING FUNCTIONS ###################################################
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement