Follow me to learn PHP5: application and analysis of session | follow me to learn

Source: Internet
Author: User
The Chinese translation of a Session is called "Session". its original meaning refers to a series of actions/messages starting and ending, for example, a series of processes from picking up a phone call and dialing to hanging up a phone call can be called a session. At present, the Chinese translation of the ses Session is called "Session". its original meaning refers to a series of actions/messages that start and end with the beginning and end, for example, a series of processes from picking up a phone call and dialing to hanging up a phone call can be called a session. At present, the society is very confused about session: sometimes we can see this: "During a browser session ,... ", the session here refers to the period from opening a browser window to closing it. you can also see the sentence" the user (client) is in a session, it may refer to a series of actions of a user (generally a series of actions related to a specific target, such as the online shopping process from login to purchasing goods to checkout and logout; however, sometimes it may only refer to a connection. The difference can only be inferred from high and low texts.

However, when a session is associated with a network protocol, it often implies two meanings: "connection-oriented" and "/" persistence, "Connection orientation" refers to the establishment of a communication channel before the communication parties establish a communication channel, such as a call, until the other party receives a telephone communication before starting. "Persistence" means that the communication party can associate a series of messages so that messages can be mutually attached, for example, a waiter can recognize an old customer who has visited the store again and remembers that the customer still owes a dollar to the store. Such examples include "one TCP session" or "one POP3 session ".

Since such chaos cannot be changed, it is difficult to have the same scale for the next definition of the session. When browsing session-related materials, we can only infer from high and low texts. However, we can understand this: for example, when we make a call, we call the call from the moment we call the call to the end of the call, because the call is always in the connected state, so we call the session status. It is a public variable that persists during the interaction between visitors and all websites. when the client does not support cookies, SESSION variables are used to ensure data accuracy and security. Visitors to the website will be assigned a unique identifier, the so-called session ID. It is either stored in the client's cookie or transmitted through the URL.

The invention of SESSION makes up for the limitations of the HTTP protocol: the HTTP protocol is considered as a stateless protocol and cannot be known about the user's browsing status. after the server completes the response, the server lost contact with the browser. This is consistent with the original goal of the HTTP protocol. the client simply needs to request the server to download some files, and neither the client nor the server needs to record the previous actions of each other, each request is independent. for example, the relationship between a customer and an active vending machine or an ordinary (non-member) hypermarket is the same.

Therefore, SESSION (cookie is another solution) is used to record the user's information, so that the user can confirm the request to the web server again in this identity. The invention of session allows a user to retain his/her information when switching between multiple pages. Website programmers have such understanding that the variables on each page cannot be applied on the next page (although form and url can also be implemented, but this is a very unimagined measure ), the variables registered in the SESSION can be applied as global variables.

So what is the usefulness of SESSION? Shopping cart is used for online shopping. you can add your purchased items to the shopping cart at any time and check out the items at the cashier. In the whole process, the shopping cart has always played the role of temporary storage of the selected item, and used it to track users' movements on the website. this is the role of SESSION, which can be used for user identity authentication, program Status record, parameter transfer between pages, etc.

The COOKIE technique is used in SESSION implementation. The SESSION retains a COOKIE containing session_id (SESSION number) on the client, and other session variables, such as session_name, on the server side. When the user requests the server, the session_id is also sent together to the server. the session_id is used to extract the real variable stored on the server to identify who the user is. At the same time, it is not difficult to understand why the SESSION sometimes fails.

When the client disables cookies (click "tools"-"Internet options" in IE, and click "security"-"custom level" in the pop-up dialog box, set "promise COOKIE for each conversation" to disabled), session_id cannot be passed, and the SESSION becomes invalid. However, on the linux/unix platform, php5 can actively check the cookie status. if the client is disabled, the system automatically attaches the session_id to the url for transmission. Windows host does not have this function.

   Common Session functions and usage?

Session_start (): start a session or return an existing session.

Note: This function has no parameters and returns true. If you apply cookie-based session, the browser cannot output any output before the application Session_start (). Otherwise, the following error occurs:

Warning: Cannot send session cache limiter-headers already sent (output started at/usr/local/apache/htdocs/cga/member/1.php: 2 )............

You can start session. auto_start = 1 in php. ini, so you do not need to call session_start () before each session application (). However, there are some restrictions on enabling this option. if the session is enabled. auto_start, the object cannot be put into the session, because the class definition must be loaded before the session is started to recreate the object in the session.

All registered variables will be serialized after the request ends. Registered but undefined variables are marked as undefined. In subsequent visits, these variables are not defined by the session module unless you define them later.

Warning some types of data cannot be serialized and therefore cannot be stored in sessions. Contains resource variables or objects with circular references (that is, an object passes a reference pointing to itself to another object ). [1] [2] [3] Next page 
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.