The example in this article describes how PHP saves session sessions using MySQL. Share to everyone for your reference. The specific analysis is as follows:
In many large systems generally have this function, but to separate the analysis, the online reliable information is not too much here I organized an issue to share with you
Saving session sessions with MySQL has many advantages over files:
1 in favor of distributed systems, files can only be stored on a single machine
2 for large access to the system, the use of files each session saved in a file, the directory will be super large, find session files will be more difficult.
To save a session using MySQL first, you will create a table of sessions: <?php $hostname _login = "localhost"; Server address $username _login = "root"; User name $password _login = ""; Password//$data _name = "session"; Database name $login = Mysql_pconnect ($hostname _login, $username _login, $password _login) or Trigger_error (mysql_
Error (), e_user_error); $sql = "Show DATABASES". $data _name. "'"; If it is exist if ($rs _table=mysql_query ($sql, $login)) {if ($rs _value=mysql_fetch_array ($rs _table)) {echo database already exists !
\n! ";
Exit ();
}} $sql = "CREATE DATABASE $data _name"; mysql_query ($sql); Crate DB Echo database created successfully!
\ n ";
mysql_select_db ($data _name, $login); $sql = "CREATE TABLE ' Sessions ' (' sessionkey ' varchar ') NOT NULL default ', ' Sessionarray ' blob not null, ' Sessionexp Time ' int ' unsigned not NULL default ' 0 ', PRIMARY key (' SessionKey '), key ' SessionKey ' (' SessionKey ') Engine=myisa M DEFAULT Charset=utf8 ";
New Database SQL statement mysql_query ($SQL); Echo succeeded in creating a new database table!
";?>
The Mysqlsession class is as follows: <?php class Mysqlsession {//Note the page with session usage.
Page must be below, the beginning of the page can not be left blank. Private $DB _server = "localhost"; Database server host name private $DB _name = ""; Database name private $DB _user = "root"; MYSQL database access User name private $DB _pass = "";
MYSQL database access password private $DB _select_db = ""; Private $SESS _life = 1440;
Maximum length of session, per second.
Private $SESS _life = 0;
Function mysqlsession (& $sessionDB) {//session_write_close ();
$this->db_name = & $sessionDB;
$this->sess_life = Get_cfg_var ("Session.gc_maxlifetime");
Session_module_name (' user '); Session_set_save_handler (Array (& $this, ' Sess_open '), Array (& $this, ' Sess_close '), Array (& $this, ' Sess_r
EAD '), Array (& $this, ' Sess_write '), Array (& $this, ' Sess_destroy '), Array (& $this, ' sess_gc '));
Session_Start (); function Sess_open ($save _path, $session _name) {//Open database connection if (! $this->db_select_db = mysql_pconnect ($this->db_s
Erver, $this->db_user, $this->db_pass)) { echo "sorry!
MYSQL Error:can ' t connect to $this->db_server as $DB _user ";
echo "MySQL Error:", mysql_error ();
Die } if (! mysql_select_db ($this->db_name, $this->db_select_db)) {echo sorry!
MYSQL error:unable to select database $this->db_name ";
Die
return true;
function Sess_close () {return true; The function Sess_read ($SessionKey) {$Query = "Select Sessionarray from sessions WHERE SessionKey = '". $SessionKey. "' and Sessionexptime > ".
Time ();
Expired does not read.
$Result = mysql_query ($Query, $this->db_select_db);
if (list ($SessionArray) = Mysql_fetch_row ($Result)) {return $SessionArray;
return false;
function Sess_write ($SessionKey, $VArray) {$SessionExpTime = time () + $this->sess_life;
Update session expiration,//session expiration = Last Update time + Session maximum usage length $SessionArray = addslashes ($VArray); $Query = "INSERT into sessions (Sessionkey,sessionexptime,sessionarray) VALUES ('. $SessionKey." ', ' ". $SessionExpTime." " , ' ". $SessIonarray. "')";
$Result = mysql_query ($Query, $this->db_select_db); if (! $Result) {$Query = "UPDATE sessions SET sessionexptime = '". $SessionExpTime. "', Sessionarray = '. $SessionArray." ' WHERE SessionKey = '. $SessionKey. ' and Sessionexptime > ".
Time ();
$Result = mysql_query ($Query, $this->db_select_db);
return $Result;
The function Sess_destroy ($SessionKey) {$Query = "DELETE from sessions WHERE SessionKey = '". $SessionKey. "'";
$Result = mysql_query ($Query, $this->db_select_db);
return $Result;
The function sess_gc ($maxlifetime) {//This garbage scavenger system call. The default is 1440 seconds to clear once.
Parameters can be set inside the php.ini. $Query = "DELETE from sessions WHERE Sessionexptime <".
Time ();
$Result = mysql_query ($Query, $this->db_select_db);
Return Mysql_affected_rows ($this->db_select_db);
?>//Usage: In the original use of Session_Start place, replace with $session = new mysqlsession ()//Note: To open the database before the program is included, the main program cannot close the database before exiting, otherwise it will be wrong.
I hope this article will help you with your PHP programming.