Dynamic web page creation PHP Sessions

Source: Internet
Author: User
The function of the PHPsession variable is to store the user's session information or change the user's session settings. The Session variable stores the information of a single user, which can be used by all pages. PHPSession variable when you run an application on your computer, you open it, make some changes to it, and then close it. This process

The PHP session variable is used to store the user's session information or change the user's session settings. The Session variable stores the information of a single user, which can be used by all pages. PHP Session variable when you run an application on your computer, you open it, make some changes to it, and then close it. This process

The PHP session variable is used to store the user's session information or change the user's session settings. The Session variable stores the information of a single user, which can be used by all pages.

PHP Session variable

When you run an application on your computer, you open it, make some changes to it, and then close it. This process is similar to session. The computer knows who you are, when you start the application, and when you close the application. But on the internet, there will be a problem: Because the HTTP address cannot be permanently retained, it is difficult for the server to identify who you are and what you are doing.

PHP Session allows you to store user information on the server (for example, username [username], shopping list [shopping], and so on) to solve this problem. However, the session information also exists temporarily. When you leave this website, it will be automatically deleted. If you want to keep this information permanently, you can try to store it in the database.

The Session creates an independent ID (UID) for each visitor and stores UID-based variables for running. UID is stored in cookies and displayed in URLs.

Start PHP Session

Before you store user information into a PHP Session, you must start the Session.

The above code registers a user's session on the server, allows you to store user information, and specifies a UID for the user session.

Store a Session variable

The best way to store and retrieve session variables is to use the PHP $ _ SESSION variable:

Session_start ();

// Store session data

$ _ SESSION ['view'] = 1;

?>

// Retrieve session data

Echo "Pageviews =". $ _ SESSION ['view'];

?>

Result:

Pageviews = 1

In the above case, we have created a simple page counter. The Isset () function checks whether the "views" variable has been set. If the "views" variable has been set, we will increase our count. If the "views" variable does not exist, we will first create a "views" variable and assign "1" to it.

Session_start ();

If (isset ($ _ SESSION ['view'])

$ _ SESSION ['view'] = $ _ SESSION ['view'] + 1;

Else

$ _ SESSION ['view'] = 1; echo "views =". $ _ SESSION ['view'];

?>

Delete Session

If you want to delete some session data, you can use the unset () function or session_destroy () function.

The Unset () function is used to release the specified session variable:

?>

You can also use the session_destroy () function to delete all sessions:

Session_destroy ();

?>

Note: session_destroy () will reset your session and you will lose all the stored session data.

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.