Session join Redis Implementation code mode

Source: Internet
Author: User
Tags php session

Session, Chinese is often translated into a conversation, its original meaning refers to the beginning and end of a series of actions/messages, such as the phone from the pick up the phone to dial to hang up the phone in the middle of a series of processes can be called a session. Sometimes we can see the words "during a browser session, ...", where the term "session" is used in its original meaning, refers to open from a browser window to close this period ①. The most confusing is the phrase "User (client) during a session", which may refer to a series of actions by a user (in the case of a series of actions related to a specific purpose, such as a process of online shopping from login to purchase to checkout, sometimes referred to as a transaction), However, sometimes it is possible to simply refer to a connection, or it may refer to the meaning of ①, where the difference can only be inferred by context ②.  
in this article, using the Chinese "browser session" to express the meaning of ①, using the "session mechanism" to express the meaning of ④, using "session" to express the meaning of ⑤, using the specific "HttpSession" to express the meaning ⑥ 
Why save the session in the cache
? PHP, the language itself is supported by the session is saved to the disk file, saved in the specified folder, the saved path can be set in the configuration file or use the function in the program Session_save_ Path () is set, but this is a disadvantage,
the first is to save to the file system, inefficient, as long as the usefulness of the session will be a lot of files to find the specified SessionID, inefficient.
The second is when you use more than one server may appear, the session lost problem (in fact, is saved on the other server).
Of course, save in the cache can solve the above problem, if you use the PHP itself session function, you can use the Session_set_save_handler () function is convenient to the session of the processing process to re-control. If you do not use the PHP session series functions, you can write a similar session function, it is also possible, I am doing this project is this, will be based on the user's mid, login time to hash as sessionId, Each request must add SessionID to be considered legal (when the first login is not required, this time will create SessionID, return to the client), so it is very convenient, concise and efficient. Of course, this article is mainly about the PHP itself in the session "do the Dirty."
session is saved in cache

PHP to save the cache to Redis, you can use the configuration file, the session processing and save to make changes, of course, in the program using the Ini_set () function to modify also can, this is very convenient to test, I use this method here, of course, if the production environment is recommended to use the configuration file.

If you want to simply operate the session into Redis operation, you can run the code a bit.

<?php

Ini_set ("Session.save_handler", "Redis");

Ini_set ("Session.save_path", "tcp://localhost:6379");

Session_Start ();

Header ("Content-type:text/html;charset=utf-8");

if (Isset ($_session[' view ')) {

$_session[' view ' = $_session[' view ' + 1;

}else{

$_session[' view ' = 1;

}

echo "View" {$_session[' view '} ";

Here the Session.save_handler is set to Redis,session.save_path as the address and port of Redis, set to refresh later, and then look back at Redis, and you will find the SessionID generated in Redis, SessionID and browser requests are the same,



You can also use

Session_set_save_handler (' open ', ' close ', ' read ', ' write ', ' destory ', ' GC ');

Use the following to customize a Redis_session class

<?php

Class redissession{

Private $_redis = Array (

' Handler ' = NULL,//database connection handle

' Host ' = null,//redis port number

' Port ' = null,

);

Public function __construct ($array = Array ()) {

Isset ($array [' Host '])? $array [' Host ']: "false";

Isset ($array [' Port '])? $array [' Host ']: "false";

$this->_redis = Array_merge ($this->_redis, $array);

}

Public function begin () {

Set Session handler function

Session_set_save_handler (

Array ($this, ' open '),

Array ($this, ' close '),

Array ($this, ' read '),

Array ($this, ' write '),

Array ($this, ' destory '),

Array ($this, ' GC ')

);

}

Public Function open () {

$redis = new Redis ();

$redis->connect ($this->_redis[' host '), $this->_redis[' Port '];

if (! $redis) {

return false;

}



$this->_redis[' handler ') = $redis;

$this-&GT;GC (NULL);

return true;

}

Off

Public function Close () {

Return $this->_redis[' handler ']->close ();

}

Read

Public function read ($session _id) {

Return $this->_redis[' handler ']->get ($session _id);

}

Write

Public function Write ($sessionId, $sessionData) {

Return $this->_redis[' handler ']->set ($sessionId, $sessionData);

}

Public Function Destory ($sessionId) {

Return $this->_redis[' handler ']->delete ($sessionId) >= 1? True:false;

}

Public Function GC () {

Get all the SessionID, let the expired release out

$this->_redis[' handler ']->keys ("*");

return true;

}

}

$ses = new Redissession (Array (' host ' = ' 127.0.0.1 ', ' port ' = ' 6379 '));

$ses->begin ();

Session_Start ();

$_session[' name ']= ' Zhangsan ';

echo $_session[' name '];

This enables you to implement session data, such as Redis, which must be installed during Redis code execution.

Session join Redis Implementation code mode

Related Article

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.