Detailed introduction to php session control instance code

Source: Internet
Author: User
The http protocol is stateless. for each request, the server cannot distinguish users. Php session control is to give users a key (an encrypted session string), and this is also a proof of user identity. the server stores the box (database, memory database or files), the box contains the user's variable information.


Overview

The http protocol is stateless. for each request, the server cannot distinguish users. Php session control is to give users a key (an encrypted session string), and this is also a proof of user identity. the server stores the box (database, memory database or files), the box contains the user's variable information.

Where is the key?
1. url query string
2. in the browser cookie

Traditional php session usage
 
 
  

Ask a question. what about the key? Didn't you see the operation for the user key?
This operation is behind php. since you visit page1.php to run the program session_start (), php will follow the conditions (user ip address, browser number, generate a PHPSESSID variable. after http response returns to the client, the PHPSESSID will already exist in your browser cookie. every time you access this domain name again, the PHPSESSID will be sent to the server. This PHPSESSID is the user key I mentioned here.

Another question is: is PHPSESSID security easy to steal, forge, or tamper?
Https can be used to prevent tampering. Instead of using PHPSESSID, you can generate a secret key to prevent forgery. I have never studied whether it is easy to be stolen. For example, if your computer is connected to the Internet, hackers intrude into your computer.

Store the generated key in the browser cookie.
Set cookiesetCookie ('key', 'value', time () + 3600); delete cookiesetCookie ('key', '', time ()-1 );
Single Sign-On: session sharing

Single Sign-On: multiple subsystems share a set of user verification systems. you can access all subsystems by logging on to one of them.
Suppose that the php environment of server A and server B is the same. The user obtained his key on server A, and then he took the key to access server B. do you know server B?
Obviously, the key generated by server A is not recognized by the server.
Solution: no matter the user accesses A or B, the generated keys are stored in C (the same database or cache system). when the user accesses A or B again, both A and B ask C: is this user's key correct? If yes, you can use the boxes that exist in A or B.

  $ Uid, 'session' => session_encrypt (session_id (). time (); // Save $ session_info to C.

The above is a detailed description of the php session control instance code. For more information, see other related articles in the first PHP community!

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.