Multi-server shared session (mysql)

Source: Internet
Author: User
Php code & lt ;? Php & nbsp; * vim: setexpandtabtabstop4shiftwidth4foldmethodmarker: * & nbsp; Program: MySQL-BasedsessionClass & php code
/* Vim: set expandtab tabstop = 4 shiftwidth = 4 foldmethod = marker :*/
// ================================================ =====
// Program: MySQL-Based session Class
// Function: mysql-based Session function
// Author: yejr
// Website: http://imysql.cn
// Time: 2007-01-05
// ================================================ =====
 
/**
* Class name: MySQL Session Class
* Function: implements Session function based on MySQL HEAP table storage.
* Description: This class implements the Session function. it basically saves the SessionID by setting the Cookie of the client,
* The user data is stored on the server end, and the Session Id in the Cookie is used to determine whether the data is user,
* Perform corresponding data operations
*
* Note: This class calls the PEAR: DB Class. if you have your own DB class, you can slightly modify the code.
*
* Remarks: SQL statement for data table initialization:
* Create table 'Session '(
* 'Sid 'varchar (32) not null default '',
* 'Session 'longtext,
* 'Flush _ dt 'int unsigned not null default '0 ',
* Primary key ('Sid '),
* KEY 'la' ('flush _ dt ')
*) ENGINE = HEAP;
*/
 
// Set the SESSION validity period in seconds.
Define ('sess _ lifttime', 3600 );
Define ('Db _ DSN ', 'MySQL: // root: @ localhost/test ');
Define ('Db _ name', 'test ');
 
Require_once ('Db. php ');
 
$ GDb = new DB;
$ GDb = DB: connect (DB_DSN, TRUE );
 
If (PEAR: isError ($ gDb ))
{
Die ($ gDb-> getMessage ());
}
 
If (! Defined ('mysqlsession '))
{
Define ('mysqlsession ', TRUE );
 
Class Usess
{
Static $ mSessSavePath;
Static $ mSessName;
Static $ mSessMaxTime;
Static $ mTblSess = 'session ';
Static $ mTblSessMap;
Static $ mDb;
 
// {Initialize the constructor
/**
* Constructor
*
* @ Param string $ login_user logon user
* @ Param int $ login_type user type
* @ Param string $ login_sess logon Session value
* @ Return Esession
*/
Public function _ construct ()
{
Self: $ mSessMaxTime = SESS_LIFTTIME;
 
Self: $ mTblSessMap = array (
'Sid '=> 'Sid ',
'Data' => 'session ',
'Last' => 'flush _ dt ',
);
}
//}}}
 
/** {SessOpen ($ pSavePath, $ name)
*
* @ Param String $ pSavePath
* @ Param String $ pSessName
*
* @ Return Bool TRUE/FALSE
*/
Public function sessOpen ($ pSavePath = '', $ your SSNAME = '')
{
Global $ gDb;
 
Self: $ mDb = $ gDb;
Self: $ mSessSavePath = $ pSavePath;
Self: $ mSessName = $ inclussname;
 
Self: sessGc ();
 
Return TRUE;
}
//}}}
 
/** {SessClose ()
*
* @ Param NULL
*
* @ Return Bool TRUE/FALSE
*/
Public function sessClose ()
{
Return TRUE;
}
//}}}
 
/** {SessRead ($ wSid)
*
* @ Param String $ wSid
*
* @ Return Bool TRUE/FALSE
*/
Public function sessRead ($ wSid = '')
{
Global $ db;
 
$ WSql = sprintf ("SELECT * FROM '% s'.' % s' WHERE '% s' =' % s ';",
DB_NAME,
Self: $ mTblSess,
Self: $ mTblSessMap ['Sid '],
$ WSid
);
 
// DB_FETCHMODE_ASSOC must be used here; otherwise, the retrieved array can only be subscript with numbers
If (! PEAR: isError ($ row = self: $ mDb-> getRow ($ wSql, null, DB_FETCHMODE_ASSOC )))
{
// The session already exists.
If (is_array ($ row) & 1 <= count ($ row ))
{
Return $ row [self: $ mTblSessMap ['data'];
}
Else
{
$ WSql = sprintf ("insert into '% s'.' % s' VALUES ('% s','', UNIX_TIMESTAMP (NOW ()));",
DB_NAME,
Self: $ mTblSess,
$ WSid
);
 
If (! PEAR: isError (self: $ mDb-> query ($ wSql )))
{
Return TRUE;
}
}
}
 
Return FALSE;
}
//}}}
 
/** {SessWrite ($ wSid, $ wData)
*
* @ Param String $ wSid
* @ Param String $ wData
*
* @ Return Bool TRUE/FALSE
*/
Public function sessWrite ($ wSid = '', $ wData = '')
{
$ WData = mysql_escape_string ($ wData );
 
$ WSql = sprintf ("UPDATE '% s '. '% s' SET' % s' = '% s',' % s' = UNIX_TIMESTAMP (NOW () WHERE '% s' =' % s ';",
DB_NAME,
Self: $ mTblSess,
Self: $ mTblSessMap ['data'],
$ WData,
Self: $ mTblSessMap ['last'],
Self: $ mTblSessMap ['Sid '],
$ WSid
);
 
If (! PEAR: isError (self: $ mDb-> query ($ wSql )))
{
Return TRUE;
}
 
Return FALSE;
}
//}}}
 
/** {SessDestroy ($ wSid)
*
* @ Param String $ wSid
*
* @ Return Bool TRUE/FALSE
*/
Public function sessDestroy ($ wSid = '')
{
$ WSql = sprintf ("delete from '% s'.' % s' WHERE '% s' =' % s ';",
DB_NAME,
Self: $ mTblSess,
$ WSid
);
 
If (! PEAR: isError (self: $ mDb-> query ($ wSql )))
{
Return TRUE;
}
 
Return FALSE;
}
//}}}
 
/** {SessGc ()
*
* @ Param NULL
*
* @ Return Bool TRUE/FALSE
*/
Public function sessGc ()
{
Global $ db;
 
// Calculate the Expiration Time
$ Last = time ()-self: $ mSessMaxTime;
 
$ WSql = sprintf ("delete from '% s '. '% s' WHERE' % s' <$ last; ", DB_NAME, self ::$ mTblSess, self ::$ mTblSessMap ['last']);
 
If (! PEAR: isError (self: $ mDb-> query ($ wSql )))
{
Return TRUE;
}
 
Return FALSE;
}
//}}}
 
/** {InitSess ()
*
* @ Param NULL
*
* @ Return Bool TRUE/FALSE
*/
Public function initSess ()
{
$ Domain = '';
 
// Do not use the GET/POST variable method
Ini_set ('session. use_trans_sid ', 0 );
 
// Set the maximum time to live for garbage collection
Ini_set ('session. gc_maxlifetime', SESS_LIFTTIME );
 
// Use cookies to save SESSION IDs
Ini_set ('session. use_cookies ', 1 );
Ini_set ('session. cookie_path ','/');
 
// Multiple hosts share the COOKIE that saves the SESSION ID
Ini_set ('session. cookie_domain ', $ domain );
 
// Set session. save_handler to user instead of the default files
Session_module_name ('user ');
 
// Define the method name for each SESSION operation:
Session_set_save_handler (
Array ('uses', 'sessopen '), // corresponds to the static method My_Sess: open (), the same below.
Array ('uses', 'sessclose '),
Array ('uses', 'ssread '),
Array ('uses', 'sswrite '),
Array ('uses', 'ssdestroy '),
Array ('uses', 'ssgc ')
);
Session_start ();
 
Return TRUE;
}
//}}}
 
} // End class
 
} // End define
 
$ Sess = new Usess;
$ Sess-> initSess ();
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.