Two previous articles, custom session (ii)-database save and why don't I use the session
But then we found that there were problems. The former treatment is practically useless in practice, and the session recycling has to be handled separately. The latter frequently operates the database, hitting a lot of performance issues.
The two days of careful consideration, a general proposal, but there is no specific detailed test.
1, session processing and statistics together. Visitors also have a record.
2. Fully use database and cookie to simulate the function of session.
3, the user's operation to the session as far as possible to ensure that the completion of a SQL statement. Not to the session, there is absolutely no more than one query.
4, for the sake of efficiency, the recovery of the session is not integrated, but provides an interface, can call the implementation.
Temporarily give the code, not specifically explained.
Sql
CREATE TABLE ' *****_session ' (
' Sid ' char (+) is not NULL,
' UID ' int (ten) is not NULL,
' username ' char (+) is not NULL,
' Usertype ' tinyint (1) Not NULL,
' Activetime ' int (ten) is not NULL,
' Expiry ' int (ten) is not NULL,
' IP ' char (+) is not NULL,
' URL ' char (not NULL),
' Value ' char (255) is not NULL,
PRIMARY KEY (' Sid ')
) Engine=memory DEFAULT Charset=utf8;
PHP code
Class session{
Private $_sessionprex= ';//session prefix
Private $_time = ";//Current time
Private $_model = null;//database operation model
Private $_expiry = 1200;//session effective Time
Private $_domain = "; Scope of//session
Protected $isNew = 0;//decision Action Action 0 Update 1 increased
Protected $session = Array ();//a session record corresponding to
Public function __construct ($options) {
$this->_setoptions ($options);
if (Empty ($this->_time)) $this->_time = time ();
$this->session[' activetime ') = $this->_time;
}
Public Function Start () {
$this->_getsid ();
}
Public function set ($key, $value) {
if (In_array ($key, Array (' uid ', ' username ', ' usertype ', ' url ', ' expiry '))) {
if ($key = = ' expiry ') {
$this->_setcookie ($this->_sessionprex. ' _sid ', $this->session[' Sid '], $value);
$this->_setcookie ($this->_sessionprex. ' _uid ', $this->session[' uid '], $value);
}
$this->session[$key] = $value;
}else{
$other = $this->session[' value '];
$other [$key] = $value;
$this->session[' value '] = $other;
}
}
Public function Get ($key) {
if (In_array ($key, Array (' uid ', ' username ', ' usertype ', ' url ', ' expiry '))) {
return $this->session[$key];
}else{
if (isset ($this->session[' value ' [$key])) {
return $this->session[' value ' [$key];
}
return null;
}
}
Public Function gc ($file, $time = 1200) {
$lasttime = file_get_contents ($file);
if ($lasttime + $time < $this->_time) {
File_put_contents ($file, $this->_time);
return $this->_model->delete (' activetime+expiry< '. $this->_time);
}
}
Public function Destroy () {
$this->session[' uid '] = 0;
$this->session[' username '] = ';
$this->session[' usertype ') =-1;
$this->session[' expiry ') = $this->_expiry;
$this->session[' value '] = Array ();
$this->_setcookie ($this->_sessionprex. ' _sid ', $this->session[' Sid '], $this->_expiry);
$this->_setcookie ($this->_sessionprex. ' _uid ', $this->session[' uid '], $this->_expiry);
}
Public Function __destruct () {
$this->_save ();
}
Private Function _save () {
$dbSession = $this->session;
$dbSession [' value '] = Serialize ($dbSession [' value ']);
if (strlen ($dbSession [' value ']) >255) $this->_error (' Session->value is too long! ');
if ($this->isnew = = 1) {
Increase
$this->_model->insert ($dbSession);
}else{
Update
$sid = $dbSession [' Sid '];
$this->_model->update (Array_slice ($dbSession, 1), ' sid=\ '. $sid. ' \'');
}
}
Private Function _getsession ($SID) {
$dbSession = $this->_model->detail (' sid = \ '. $sid. ' \'');
if (! $dbSession) return false;
$dbSession [' value '] = unserialize ($dbSession [' value ']);
$this->session = Array_merge ($dbSession, $this->session);
return true;
}
Private Function _getsid () {
$sid = Strip_tags ($_cookie[$this->_sessionprex. ' _sid ']);
if (strlen ($SID) ==32) {
if ($this->_getsession ($sid)) {
return true;
}
}else{
$SID = MD5 (Time (). Mt_rand (1000,10000));
$this->_setcookie ($this->_sessionprex. ' _sid ', $sid);
}
$this->_setcookie ($this->_sessionprex. ' _uid ', 0);
$this->session = Array (
' UID ' = 0,
' Username ' = ',
' Usertype ' =-1,
' Activetime ' = $this->_time,
' IP ' = $this->_getip (),
' URL ' = strip_tags ($_server[' Request_uri '),
' Expiry ' = $this->_expiry,
' Value ' = = Array ()
);
$this->isnew = 1;
$this->session[' sid '] = $sid;
}
Private Function _setcookie ($name, $value, $expiry =0) {
if (empty ($expiry)) $expiry = $this->_expiry;
if (Empty ($this->_domain)) {
Setcookie ($name, $value, $this->_time + $expiry, '/');
}else{
Setcookie ($name, $value, $this->_time + $expiry, '/', $this->_domain);
}
}
Private Function _getip () {
return GetIP ();
}
Private Function _setoptions ($options) {
foreach ($options as $key = = $value) {
if (In_array ($key, Array (' Sessionprex ', ' time ', ' model ', ' expiry ', ' domain '))) {
$key = ' _ '. $key;
$this $key = $value;
}
}
}
Private Function _error ($msg) {
throw new Phpbean_exception ($msg);
}
}
?>
(Note that this code cannot be used directly, this article is mainly to provide a way of thinking)
http://www.bkjia.com/PHPjc/318536.html www.bkjia.com true http://www.bkjia.com/PHPjc/318536.html techarticle I've written two articles, custom session (ii)--database save and why don't I use the session, but then I found out there was a problem. The former treatment in practice almost nothing ...