Php5 session explanation 1: What is session?

Source: Internet
Author: User
1. What is the Chinese translation of SessionSession called "session"? its original meaning refers to a series of action messages with beginning and end stages, 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 the session is very confusing: Sometimes we can see the following:

1. What is a Chinese translation of a Session called a "Session"? its original meaning refers to a series of actions/messages that have started and ended, 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 the session is very confusing: Sometimes we can see the following:

1. What is Session?

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.


Thing vehicles have always played the role of temporarily storing the selected items and used them 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 =" "> 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.

2. 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 (outputstarted at/usr/local/apache/htdocs/cga/member/1.php: 2 )............
You can start session. auto_start = 1 in php = ""> 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. Warning: Some data types cannot be listed in sequence, because they cannot be stored in the session. Including resource variables or objects with circular references (that is, an object passes a reference pointing to itself to another object ).
● Register SESSION variables:
PHP5 registers the SESSION global variable with $ _ SESSION ['xxx'] = xxx. Similar to GET, POST, and COOKIE usage.
Note:
Session_register (),
Session_unregister (),
Session_is_registered ()
It is not used in php5 unless register_globle is set to on in php. ini. However, for security reasons, it is strongly recommended to disable register_globle. HTTP_SESSION_VARS is not recommended. We recommend that you use $ _ SESSION instead. For example:
 
// Page2.php
 Back to the mountain page ';?>

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.