PHP implementation session stored to Redis,sessionredis
It is not appropriate to use the default session for a site with a large number of visits, we can store it in a database, or use a Redis key-value data storage scheme
Create a new session table first
CREATE TABLE ' Sessions ' (' Sid ' char (+) NOT null, ' updatetime ' int (a) ' is not NULL, ' data ' varchar ($) NOT NULL, UNIQUE KEY ' Sid ' (' Sid ') USING HASH) engine=memory DEFAULT Charset=utf8;
The memory engine of Mysql uses the table, all the data is stored in the memory, the operation speed is fast
<?php//introduction of database files include "db.php"; class Mysessionhandler implements sessionhandlerinterface{private $savePath; Private $sessData; Public $expiretime; Set expiration time public $db; Database Public Function __construct ($hanlder = ') {$this->db = database::getinstance (); Get database Strength///var_dump ($this->db); Public function open ($savePath, $sessionName) {return true; Public function Close () {return true; The public function read ($id) {$sql = "SELECT * from sessions where sid = ' $id '"; $result = $this->db->execute ($sql); if (!empty ($result)) {return $this->sessdata = $result; }}//Parameters of the function $id the string public function write ($id, $data) {//Echo $id, after serialization with current session ID//data Echo $data; $now = time (); $NEWEXP = $now + $this->expiretime; Total Time = current time + term time $sql = "SELECT * from sessions where sid = ' $id '"; $result = $this->db->getone ($sql); Var_dump ($result);if ($data = = "| | Isset ($data)) {$data = $this->sessdata; if ($result) {//if present update $sql = "Update sessions Set updatetime = ' $newExp ', data = ' $data ' where sid = ' $id '"; Echo $sql; $update _data = $this->db->execute ($sql); if ($update _data) {return true; }}else{//Does not exist generate build $sql = "INSERT into sessions (Sid,updatetime,data) VALUES (' $id ', ' $now ', ' $data ')"; $insert _data = $this->db->execute ($sql); if ($insert _data) {return true; }} return false; The Public function destroy ($ID) {//destroys $sql = "Delete from sessions where sid=". " $id "; $destory = $this->db->execute ($sql); if ($destory) {return true; }else{return false; }} Public Function GC ($sessMaxLifeTime) {$t = time (); $sql = "Delete from sessions where $t-' UpdateTime ' >${sessmaxlifetime}"; $data = $this->db->execute ($this->tosql); if ($data) {return true; }else{return false; } return true; }}
Instantiation of
There are two ways PHP manuals can be used here
1, realize the object of Sessionhandlerinterface excuse, since PHP5.4 can use
2, direct use of Session_set_save_handler
Determine the PHP version if (Version_compare (php_version,5.4) ==1) { Session_set_save_handler ($handler, true); Session_Start (); } else{ ini_set (' Session.use_trans_sid ', 0); Ini_set (' session.use_cookies ', 1); Ini_set (' Session.cookie_path ', '/'); Ini_set (' Session.save_handler ', ' user '); Session_module_name (' user '); Session_set_save_handler (Array ($session, "open"), Array ($session, "close"), Array ($session, "read"), Array ($session, "Write"), Array ($session, "Destory"), Array ($session, "GC")); Session_Start (); } $_session[' QQ ']= "QQ"; Echo $_session[' QQ '];
Database code
<?php class database{ static $instance; static $db; static function getinstance () { if (self:: $instance) { return self:: $instance; } else{ return new Database (); } } Public Function __construct () {self :: $db = new PDO (' Mysql:host=localhost;dbname=session ', ' root ', '); } Public Function GetOne ($sql) { $rs =self:: $db->query ($sql); @ $rs->setfetchmode (PDO::FETCH_ASSOC);//returns associative array $result = FETCH () $rs; return $result; } Public function Execute ($sql) { $rs = self:: $db->exec ($sql); return $rs; } } $data = Database::getinstance ();//var_dump ($data);
Using Redis storage session
<?phpclass sessionmanager{Private $redis; Private $sessionSavePath; Private $sessionName; Private $sessionExpireTime = 30; Public Function __construct () {$this->redis = new Redis (); $this->redis->connect (' 127.0.0.1 ', 6379); Connect Redis $retval = Session_set_save_handler (Array ($this, "open"), Array ($this, "close"), Array ($this, "re Ad "), Array ($this," write "), Array ($this," Destory "), Array ($this," GC ")); Session_Start (); Public function open ($path, $name) {return true; Public function Close () {return true; Public function Read ($id) {$value = $this->redis->get ($id); if ($value) {return $value; }else{return ""; }} Public Function write ($id, $data) {if ($this->redis->set ($id, $data)) {$this->redis->expire ($id, $this->sessionexpiretime); Set expiration time return true; } return false; } Public Function DestorY ($id) {if ($this->redis->delete ($id)) {return true; } return false; The Public Function gc ($MAXLIFETIME) {return true; }//Destructors Public Function __destruct () {session_write_close (); }} $re = new SessionManager (); $_session[' name '] = "QQ"; Echo $_session[' name '];
The above is a detailed introduction of PHP implementation of the session to the Redis method, I hope that everyone's learning is helpful.
http://www.bkjia.com/PHPjc/1070277.html www.bkjia.com true http://www.bkjia.com/PHPjc/1070277.html techarticle PHP Implementation session store to Redis,sessionredis the default session for a site with a large number of visits is not appropriate, we can save it in the database, or use Redis key-value data storage ...