PHP-Session Sessions

Source: Internet
Author: User
Tags change settings session id php session setcookie unique id

PhpSessions

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 .

The origin of the PHP Session variable

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

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:

<? PHP  session_start();  // Store session Data $_session [' Views ']=1;? >PHP // Retrieve session Data Echo $_session [' views '];? ></body>

Output:

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:

<? PHP  session_start();  if (isset($_session[' views '])) $_session [' Views ' of]=$_session[' views ']+1; Else $_session [' Views ']=1; Echo $_session [' views '];? >

Destroy Session

If you want to delete some session data, you can use the

Unset ()

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

<? PHP Session_Start (); if (isset($_session[' views '])) unset ($_session[' views ']);? >

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

Session_destroy ()

Session_destroy () destroys all data in the current session, but does not reset the global variables associated with the current session and does not reset the session cookie. If you need to use session variables again, you must call the session_start () function again. In order to completely destroy the session, such as when the user exits the login, the session ID must be reset at the same time. If the session ID is transferred through a cookie, the Setcookie () function is also called to remove the client's session cookie.

return value

Returns when successful TRUE , or on failure FALSE .

<?PHP//initializes the session. If you want to use a session, don't forget to call it now:Session_Start();//Reset All variables in a session$_session=Array();//If you want to clean up more thoroughly, then delete the session cookie//Note: This not only destroys the data in the session, but also destroys the session itselfif(Ini_get("Session.use_cookies")) {    $params=Session_get_cookie_params(); Setcookie(Session_name(), ‘‘, Time()-42000,$params["Path"],$params["Domain"],$params["Secure"],$params["HttpOnly"]    );}//Finally, the session is destroyedSession_destroy();?>

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

PHP-Session Sessions

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.