Php learning session

Source: Internet
Author: User
The php learning content introduced today is session. So what is session? how can we use session.

The php learning content introduced today is session. So what is session? how can we use session.
Session indicates a series of actions and messages from the beginning to the end. For example, for online shopping, from logging on to the website, purchasing books, checking out, and finally exiting, a shopping process can become a session. It can be understood as a set of server information records for a user during the period from entering the website to closing the browser when the user browses a website.
The idea of session control is to track users based on a session on a website. The session in php is driven by a unique sessionID. SessionID is an encrypted random number generated by php. it is stored on the client during the session lifecycle or transmitted through a URL over the network.
SessionID is like a key, which is the only visible information on the client. it allows you to register specific variables, so it is also called session variables. By default, session variables are stored in common files on the server.
How to use session?
Basic steps for using session:
1. start a session
2. Register session variables
3. use session variables
4. log out of the variable and destroy the session
These steps do not have to be completed in the same file. they can occur in many scripts. The following describes these steps in detail:
1. start a session.
The first method is to call the session_start () function to start a script. This function checks whether a sessionID exists. if it does not exist, a sessionID is created and can be accessed through the Super Global Array $ _ SESSION. if it already exists, load the session variable.
The second method is to use php. session in the INI file. auto_start option. set php to automatically start a session when a user accesses the website. However, the object cannot be used as the session variable.
2. Register a session variable.
You can use $ _ SESSION to create a session variable;
For example, $ _ 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 this variable through the $ _ SESSION global array.
When an object is used as a session variable, the session object must be defined before the session_start () function is called to re-load the session variable. otherwise, php cannot construct the session object.
On the contrary, when checking whether the session variable has been set, you must consider the security issue, because the variable can be set through the get or post method. Check the $ _ SESSION array to check whether a variable has been registered.
4. log out of variables and destroy sessions
After a session variable is used, it should be destroyed. you can cancel the variable by destroying the appropriate elements of the array $ _ SESSION.
For example, unset ($ _ SESSION ['name'])
If you want to destroy all sessions at a time, you cannot destroy the entire array $ _ session. This will also disable the cookie function. We can solve $ _ SESSION = array ();
After all variables are eliminated, sessionID should also be destroyed. you can do session_destroy () in this way to clear sessionID.

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.