PHP in Sessions

Source: Internet
Author: User

Php Sessions The Chinese translation of the session is called "conversation", and its original meaning is to refer to a series of actions/messages.

The PHP session variable is used to store information about user sessions (session) or to change settings for user sessions. The Session variable stores information for a single user and is available for all pages in the application.

PHP Session variables

When you manipulate an application on your computer, you open it, make some changes, and then close it. It's much like a conversation (session). The computer knows who you are. It knows when you open and close the application. However, the problem arises on the Internet: the WEB server does not know who you are and what you have done because the HTTP address cannot remain in the state.

The PHP session solves this problem by storing user information on the server for subsequent use (e.g., user name, purchase of goods, etc.). However, the session information is temporary and will be deleted when the user leaves the site. If you need to store information permanently, you can store the data in a database.

The Session works by creating a unique ID (UID) for each visitor and storing the variables based on this UID. The UID is stored in a cookie or transmitted through a URL.

Start PHP Session

Before you store user information in a PHP session, you must first start the session.

Note:the session_start () function must precede the

<?php session_start ();?>

  

The code above registers the user's session with the server so that you can start saving user information and assign a UID to the user session.

Store Session variables

The correct way to store and retrieve session variables is to use the PHP $_session variable:

<?phpsession_start ();//store session data$_session[' views ']=1;? >
Pageviews=1

  

In the following example, we created a simple page-view counter. The Isset () function detects whether the "views" variable has been set. If you have set the "views" variable, we accumulate the counter. If "views" does not exist, create a "views" variable and set it to 1:

<?phpsession_start (); if (Isset ($_session[' views ")) {    $_session[' views ']=$_session[' views ']+1;} else{    $_session[' views ']=1;} echo "views=". $_session[' views ';? >

  

Destroy Session

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

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

<?phpsession_start (); if (Isset ($_session[' views ")) unset ($_session[' views ']);? >

  

You can also completely destroy the session by calling the Session_destroy () function:

<?phpsession_destroy ();? >

  

Note:Session_destroy () resets the session and you will lose all stored session data.

Sessions in PHP

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.