The session of PHP learning

Source: Internet
Author: User
Today, we introduce the content of PHP learning, is the session. So what is the session and how do we use the session?
The meaning of the session is to accomplish one thing, from start to finish some series of actions and messages. For example, on-line shopping, then from landing the site, to buy books, then to checkout, and finally exit, so that a shopping process can become a session. We can understand the collection of information that the server has recorded for this user during the period of time when the user browses a website, from entering the website to the browser closing.
The idea of session control is the ability to track users in a Web site based on a session. The session in PHP is driven by a unique sessionid. SessionID is an encrypted random number generated by PHP that is stored in the client during the lifetime of the session, or passed through the network via a URL.
SessionID is like a key, which is the only visible information on the client that allows you to register some specific variables and therefore also called session variables. By default, session variables are saved in the server's normal file.
How do I use the session?
Basic steps for using the session:
1. Start a session
2. Register Session Variables
3. Use the session variable
4. Unregister the variable and destroy the session
These steps do not have to be done in the same file, and can occur in many scripts. The following steps are described in detail below:
1. Start a session.
The first method is to call the Session_Start () function to start a script. The function checks whether a SessionID exists, creates a sessionid if it does not exist, and accesses the SessionID through a Super global array $_session, and loads the session variable if it already exists.
The second method is to use the Session.auto_start option in the php.ini file to set PHP to start a session automatically when a user visits the site, but you cannot use the object as a session variable.
2. Register a Session variable.
You can use $_session to create a session variable;
such as: $_session[' NAME ']= "Messi";
3. Use the session variable.
To use a session variable, you must use the Session_Start () function to activate a session first so that you can access the variable by $_session the global array.
When using an object as a session variable, you must include the definition of the class object before calling the Session_Start () function to reload the session variable, otherwise PHP cannot build the session object.
Conversely, security issues must be considered when checking whether a session variable has been set, because the variable can be set by the Get or post method. Determine if a variable has been registered by examining the $_session array.
4. Unregister variables and destroy session
When a session variable is used, it should be destroyed and the variable can be unregistered by destroying the appropriate elements of the array $_session.
Example: unset ($_session[' NAME ')
If you want to destroy all sessions at once, you cannot use the method of destroying the entire array $_session, which also disables the cookie function. We can solve this $_session = Array ();
After all the variables have been eliminated, SessionID should be destroyed, so Session_destroy () can be done, so the SessionID is cleared.


The above is the PHP learning session content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.