Redis replaces PHP file storage session

Source: Internet
Author: User
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.

  • 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.