Before you view the instance, understand the usage of the PHP session_set_save_handler function
Define a SessionManager class
Class SessionManager {
Private $redis;
Public Function __construct () {
$this->redis = new Redis ();
$this->redis->connect (' 192.168.0.102 ', 6379);
$retval =session_set_save_handler (
Array ($this, "open"),
Array ($this, "close"),
Array ($this, "read"),
Array ($this, "write"),
Array ($this, "destroy"),
Array ($this, "GC")
);
Session_Start ();
}
Public function open ($path, $name) {
return true;
}
Public function Close () {
return true;
}
Public function Read ($id) {
$session _value = $this->redis->get ($id);
if ($session _value) {
return $session _value;
}else{
Return "";
}
}
Public function Write ($id, $data) {
if ($this->redis->set ($id, $data)) {
return true;
}else{
return false;
}
}
Public function Destroy ($id) {
if ($this->redis->delete ($id)) {
return true;
}else{
return false;
}
}
Public Function GC ($MAXLIFETIME) {
return true;
}
Public Function __destruct () {
Session_write_close ();
}
}
Create a session_set.php code as follows
Include ("sessionmanager.php");
New SessionManager ();
$_session[' user_name ']= "xxdcsnd@sina.com";
Create a session_set.php code as follows
Include ("sessionmanager.php");
New SessionManager ();
echo $_session[' user_name '];
Test output Results xxdcsnd@sina.com
Note: PHP.ini Session.save-hadler is set to user, otherwise session_set_save_handler will not take effect
The above describes the Redis replacement PHP file storage session, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.