Modify the Session storage mode to MySQL class

Source: Internet
Author: User
Lt ;? Php *** modify the session storage mode to MysqlAbocQQ: 9986584 * classSession {expiration time private $ _ LEFT_TIME1440; publicfunct

Lt ;? Php/*** modify the session storage mode to Mysql Aboc QQ: 9986584 */class Session {// expiration time private $ _ LEFT_TIME = 1440; public funct

/**
* Modify the session storage mode to Mysql Aboc QQ: 9986584
*/

Class Session {

// Expiration time
Private $ _ LEFT_TIME = 1440;

Public function open (){


}

Public function close (){

}

/**
* Read
*/
Public function read ($ sessid ){
$ SQL = "select data from dm_session where sessid = '$ sessid' and expiry> time ()";
$ Row = DMmysql: open ()-> fetchRow ($ SQL );
Return $ row ['data'];
}

/**
* Write
*/
Public function write ($ sessid, $ sessdata ){
$ Data = array (
'Expiry' => time () + $ this-> _ LEFT_TIME,
'Data' => $ sessdata,
'IP' => '192. 168.1.123'
);
If (DMmysql: open ()-> fetchRow ("select sessid from dm_session where sessid = '$ sessid '")){
// Update
$ Where = "sessid = '$ sessid '";
If (DMmysql: open ()-> update ('dm _ session ', $ data, $ where )){
Return true;
} Else {
Return false;
}
} Else {
// Insert
$ Data ['sessid '] = $ sessid;
If (DMmysql: open ()-> insert ('dm _ session ', $ data )){
Return true;
} Else {
Return false;
}
}
}

/**
* Destruction
*/
Public function destroy ($ sessid ){
$ Where = "sessid = '$ sessid '";
If (DMmysql: open ()-> delete ('dm _ session ', $ where )){
Return true;
} Else {
Return false;
}
}

/**
* Destroy expired data
*/
Public function gc ($ maxlifetime ){
// Destroy data randomly to relieve pressure on the server
If (rand (0, 3) = 3 ){
$ Where = "expiry <time ()";
If (DMmysql: open ()-> delete ('dm _ session ', $ where )){
Return true;
} Else {
Return false;
}
}
}

}

$ Session = new Session ();
Session_set_save_handler (
Array (& $ session, 'open '),
Array (& $ session, 'close '),
Array (& $ session, 'read '),
Array (& $ session, 'write '),
Array (& $ session, 'deststroy '),
Array (& $ session, 'gc ')
);
Session_start ();
?>


Just include the file before each session file.

Database:

Create table 'dm _ session '(
'Sessid 'Char (32) not null default '',
'Expiry' int (10) not null default '0 ',
'Data' text not null,
'IP' char (15) not null default '',
Primary key ('sessid '),
KEY 'sesskid' ('sessid', 'expiry ')
) ENGINE = MyISAM default charset = gbk;

,

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.