PHP SESSION mechanism

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

Original from



650) this.width=650; "class=" AlignCenter size-full wp-image-1415 "src=" http://smaij.com/wp-content/uploads/2016/03/ Ban.jpg "alt=" ban "width=" "height=" 338 "/>php session variables are used to store information about user sessions or to change settings for user sessions. 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 there is a session module in your configuration information 650) this.width=650; "class=" AlignCenter size-full wp-image-1412 "src="/HTTP/ Smaij.com/wp-content/uploads/2016/02/session.png "alt=" session "Width=" 632 "height=" 378 "/> Above Session_support value is The enable means that you can use the session normally. 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

Storage session
<?php$_session[' key '] = ' value ';? >

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:

<?php$_session[' key '];? >

SESSION keyword $_session[' key value '];

Destroy session
<?phpunset ($_session[' key '); Destroys a specified key value unset ($_session)//destroys the entire session?>

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

Case:

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:

<?phpsession_start ();//Open session$_session[' user_name '] = $_post[' user_name '];? >

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

<?phpsession_start (); First, you need to turn on session//read user information $user_name = $_session[' user_name '];//the user's information based on the user name?>

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:

<?phpsession_start (); unset ($_session);//Unregister all session?>

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

PHP SESSION mechanism

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.