Session is added to the implementation code of redis, sessionredis

Source: Internet
Author: User
Tags php session

Session is added to the implementation code of redis, sessionredis

Session information is imported into redis

Session Introduction

Session, which is often translated as a session. Its original meaning refers to a series of actions/messagesFor example, a series of processes from picking up a phone call and dialing to hanging up a phone call can be called a session. Sometimes we can see this: "during a browser session ,... the term session here uses its original meaning, which refers to the period from opening a browser window to closing it. What is most confusing is the phrase "a user (client) is in a session". It may refer to a series of actions related to a specific purpose, for example, an online shopping process, from login to purchase of goods to checkout and logout, is also called a transaction. However, sometimes it may only refer to a connection, it may also be the meaning ①, where the difference can only be inferred by context ②.

In this article, we use the Chinese "browser session period" to express the meaning ①, the "session mechanism" to express the meaning ④, and the "session" to express the meaning ⑤, use the specific "HttpSession" to express the meaning 6

Why save the SESSION in the cache?

For php, sessions supported by the language itself are stored as files in Disk Files and saved in the specified folder, you can set the saved path in the configuration file or use the session_save_path () function in the program. However, this method has drawbacks,
First, it is saved to the file system, which is less efficient. When used to the session, it will find the specified sessionid from many files, which is very inefficient.
Second, when multiple servers are used, session loss may occur (in fact, they are stored on other servers ).
Of course, saving in the cache can solve the above problem. If you use the session function of php, you can use the session_set_save_handler () function to conveniently re-control the session processing process. If you do not need php session functions, you can write a similar session function by yourself. This is also the project I am working on, hash is used as the sessionId based on the user's mid and logon time. The sessionId must be added for each request to be valid (this is not required during the First Login, and sessionId will be created at this time, return to the client), which is convenient, concise, and efficient. Of course, I am mainly talking about "Hands and feet" in the php SESSION ".

SESSION stored in cache

Php saves the cache to redis. You can use the configuration file to modify the processing and saving of the session. Of course, you can also use the ini_set () function in the program to modify it, this is very convenient for testing. I will use this method here. Of course, we recommend that you use the configuration file in the production environment.

If you want to perform simple session-based redis operations, you can run the code.

<? 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']}"; // set the session here. the save_handler mode is redis and session. the save_path is the address and port of apsaradb for redis. Refresh the settings and check redis again. Then, you will find that the s is generated in apsaradb for redis. EssionId and sessionId are the same as those requested by the browser. // 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 the session processing 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-> gc (null); return true ;} // close 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 () {// obtain all sessionids, release expired $ this-> _ redis ['handler']-> keys ("*"); return true ;}} $ ses = new RedisSession (array ('host' => '127. 0.0.1 ', 'Port' => '000000'); $ ses-> begin (); session_start (); $ _ SESSION ['name'] = 'hangsan '; echo $ _ SESSION ['name'];

In this way, session data, such as redis, must be installed during code execution.

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.