PHP SESSION mechanism

Source: Internet
Author: User
Tags change settings php session cron script
Original from

The PHP session variable is used to store information about a user's session or to change settings for a user session. The Session variable holds information that is single-user and can be used by all pages in the application. When you visit a website in the browser, the background of the site for you to open a reply, this answer is the session. It can store some of your important information. It will assign you an ID, when you start it, and when to close it. The information stored on the server is convenient for subsequent use. It has so many advantages and strengths. But...... When you close this page. Everything went back to liberation and it forgot you. Only store in the database, you can really do it in the future remember you. The session works by: Each visitor has a unique ID and is based on the UID to store the information to the server. The UID is stored in a cookie and can be transmitted through a URL. With this unique ID, you can jump anywhere on the page you're currently visiting or remember your ID to give you the exact information you've stored on the server (e.g., user name, shopping cart, etc.).

Use session

Use session first to determine if your server supports the session. You can view the PHP configuration information via Phpinfo (). Find out if your configuration information exists in the session module above the Session_support value is enable, you can normally use the session. If you do not have the session module you must first go to PHP.ini to enable the session component. Other than that. PHP.ini session.save_handle = files in order to use the session normally.

Session_Start ()

The Session_Start () function must precede the label and no output is allowed before session_start (). Session_Start () is the beginning of the session mechanism. After the start, the session will open the garbage collection mechanism, because the session is stored in the file, it is not possible not to recycle, or occupy too much will cause a series of system problems. The garbage collection mechanism of PHP itself is invalid for the session, and the recycle of the session refers to the deletion of the stored files. The probability of session recycling is defined in php.ini. Session.gc_probability system default is 1, but some systems session.gc_probability value is 0, which means that the probability of recovery is 0, then the system will be a cron script to implement garbage collection mechanism. In addition, there is session.gc_maxlifetime in the session configuration item, this time the configuration session expires, the default is 1440, 24 minutes. This is not recommended to set too small because the session recycling needs to be based on it. Once the session is open, you can assign a value to the session. The server stores session variables. The correct way to store and retrieve SESSION variables is to use the $_session variable.

Storage session


The newly added key value, which is stored in memory, is stored in the specified session_id file when the script is executed. If it already exists, you may need to create a new file. The session_id file will be destroyed during the collection time set by the php.ini.

Read session

Read the session in the following way:


SESSION keyword $_session[' key value '];

Destroy session


Delete some session data, in addition to using unset (), you can also use the Session_destroy () function.


One of the features of user login and logoff session using session is security, so using it to implement login will be a good solution. The entire login process is probably as follows. The user enters the login page ===>> Enter the correct user account password ===>> server-side script validation succeeds, a SESSION reply is opened to store the user ID ($_session[' uid ') and user name ($_session[' User_name ']) ===>> jump to other pages in its website, use ($_session[' user_name ') to display the user's name, if necessary, by $_session[' u_id '] until the user id===>> The user exit login (unset ($_session)) login page contains the following code:

  Login Form

After the login successful jump. In other pages, such as the purchase of goods, generate trade orders and other pages, need to use the User Information page can use the following ways to obtain user information


Then if the user exits the browser directly, or closes the Web site. Session will automatically log off the collection during the specified payback time. If the user wants to manually exit, you can:


This is a basic user logon logoff system process. There are many places that can be safely handled. There will be no more than one by one lanes. Original from

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