Brief introduction to Sessions in PHP for creating dynamic web pages

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 applied by all pages.

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 applied 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 exploitation program, and when you close the exploitation program. However, on the Internet, a title is displayed: Because the HTTP address cannot be permanently retained, it is difficult for the server to identify who you are and what you are doing.

The PHP Session allows you to store user information on the server (for example, user name [username], shopping list [shopping], and so on), thus solving the title. 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 in a PHP Session, you must start the Session first.

Note: The session_start () function must be written inBefore the tag:




The above code registers a user's session on the server, promises 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 apply the PHP $ _ SESSION variable:

Session_start ();
// Store session data
$ _ SESSION ['View'] = 1;
?>
// Retrieve session data
Echo 'pageviews = '. $ _ SESSION ['views'];
?>

Results:

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 apply the unset () function or session_destroy () function.

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

?>

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

Session_destroy ();
?>

Note: session_destroy () will reset your session and you will lose all the session data that has been retained.

 

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.