Use and analysis of Session sessions in PHP. Read the use and analysis of Session sessions in PHP. the Chinese translation of Session is called "Session ", the original meaning refers to a series of actions/messages that start and end, for example, a Session can be called from the process of picking up the phone and dialing the phone to hanging up the phone. The current social understanding of sessions is not "> <LINKhref =" htt
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 society's understanding of sessions is very confusing: sometimes we can see this: "During a browser Session ,... ", the session here refers to the period from opening to closing a browser window; you can also see the sentence" 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 purpose, such as the online shopping process from login to purchasing goods to checkout and logout; however, sometimes it may only mean a connection. The difference can only be inferred by context.
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 the telephone communication. "Keep status" means that the communication party can associate a series of messages so that messages can be mutually dependent, 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 a unified standard for the next definition of the Session. When reading Session-related information, we can only infer and understand it by context. However, we can understand this as follows: 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 has existed throughout the interaction between visitors and the website. when the client does not support cookies, Session variables are used to ensure data correctness and security. Visitors to the website will be assigned a unique identifier, the so-called session ID. It is either a cookie stored on the client or transmitted through a URL.
The invention of the Session fills in the limitations of the HTTP protocol: the HTTP protocol is regarded as a stateless protocol and cannot be known about the user's browsing status. after the server completes the response, the server loses contact with the browser. This is consistent with the original purpose of the HTTP protocol. the client only needs to simply request the server to download some files, and neither the client nor the server needs to record the previous behaviors of each other, each request is independent, like the relationship between a customer and a vending machine or a common (non-member) hypermarket.
Therefore, Session (cookie is another solution) is used to record user information for confirmation when the user initiates a request to the web server. The invention of session allows a user to save his information when switching between multiple pages. Website programmers have such experiences that the variables on each page cannot be used on the next page (although form and url can also be implemented, this is a very bad way ), the variables registered in the Session can be used as global variables.
So what is the usefulness of Session? Shopping cart is used for online shopping. you can add the items you bought to the shopping cart at any time and check out the items at the cashier. During the whole process, the shopping cart has always played the role of temporarily storing the selected items and used it to track users' activities 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 technology is used in the implementation of the Session. The Session will save a COOKIE containing Session_id (Session number) on the client, and save other Session variables on the server, such as Session_name. When a user requests a server, the Session_id is also sent to the server. the Session_id is used to extract the variables 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 "allow COOKIE for each conversation" to disabled). Session_id cannot be passed, and the Session becomes invalid. However, php5 can automatically check the cookie status on linux/unix platforms. if the client is disabled, the system automatically attaches 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 use cookie-based Sessions, the browser cannot output any output before 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 () every time before using the Session (). However, enabling this option also has some restrictions. 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 are serialized after the request ends. Registered but undefined variables are marked as undefined. In subsequent access, these variables are not defined by the session module unless you define them later.
Some types of data cannot be serialized and therefore cannot be stored in sessions. Including resource variables or objects with circular references (that is, an object passes a reference pointing to itself to another object ).