PHP Custom session

Source: Internet
Author: User

Session Handle Definition:
Class sessionhandler{
Private $_driver;//Drive
Private $_gcmaxlifetime=1440;//session life cycle
Private $_gcprobability=5;//recovery factor, do not execute each session garbage collection mechanism, note: The larger the recovery factor, the greater the probability of a hit recovery

Public Function __construct () {
Ini_set (' Session.gc_divisor ', 1);
$this->_driver = new Mongosession ();//Database driver
Session_set_save_handler (
Array ($this, ' _open '),
Array ($this, ' _close '),
Array ($this, ' _read '),
Array ($this, ' _write '),
Array ($this, ' _destroy '),
Array ($this, ' _gc ')
);

if (!isset ($_cookie[' Phpsessid ')) {
Session_Start ();
Setcookie (' Phpsessid ', session_id (), 0, '/');
}else{
Session_Start ();
}
}

Public Function _open ($savePath, $sessionName) {
This is the Open data connection
$this->_driver->open ($savePath, $sessionName);
return true;
}

Public Function _close () {
$this->_driver->close ();
To close a database connection
return true;
}

/**
* Read session
* @param unknown_type $keysession _id
*/
Public Function _read ($key) {
return $this->_driver->read ($key);
}

/**
* Write session
* @param unknown_type $keysession _id
* @param unknown_type $value
*/
Public Function _write ($key, $value) {
$this->_driver->write ($key, $value, $this->_gcmaxlifetime);
}

/**
* Destroy session
* @param unknown_type $key//session_id
*/
Public Function _destroy ($key) {
$this->_driver->delete ($key);
}

/**
* Recycling of garbage data expired by session
*/
Public Function _gc ($gcTime) {
RAND () Maximum value 32767
if (rand ()%) < $this->_gcprobability) {
$this->_DRIVER->GC (Time ());
}
return true;
}

}

MongoDB Driver Class:
Class mongosession{
/**
* Read session
* @param unknown_type $sessionId
*/
Public function Read ($sessionId) {
Do not perform the interpretation expiration time
$ret = Db::init (' session ')->findone (Array (' sessionId ' = $sessionId));
return isset ($ret [' data '])? $ret [' data ']: null;
}

/**
* Write session
* @param unknown_type $sessionId
* @param unknown_type $data
* @param unknown_type $expire
*/
Public function Write ($sessionId, $data, $expire =0) {
To determine if there is a session.
$chk = Db::init (' session ')->findone (Array (' sessionId ' = $sessionId));
if ($CHK) {
There is an update
Db::init (' Session ')->update (
Array (' sessionId ' = $sessionId),
Array (' data ' = $data, ' expire ' =>time () + $expire)
);
}else{
Db::init (' Session ')->insert (Array (
' SessionId ' = $sessionId,
' Data ' = $data,
' Expire ' =>time () + $expire
));
}

}

/**
* Delete Session
* @param unknown_type $sessionId
*/
Public Function Delete ($sessionId) {
$ret = Db::init (' session ')->delete (Array (' sessionId ' = $sessionId));
return $ret;
}

/**
* Recycling
* @param unknown_type $expire
*/
Public Function GC ($expire) {
Db::init (' Session ')->delete (Array (' Expire ' =>array (' $lt ' = $expire));
}

}

PHP Custom session

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.