PHP Sessions

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

PHP session variables are used to store user session information or change settings. The Session variable contains about a single user information and is provided to all pages of an application.

PHP session variables
When you work with the application, you open it, make some changes, and then close it. This is like a session. The computer knows who you are. It knows when you start the application and when you end. But there is a problem: the Web server does not know who you are and what you do, because the HTTP address is not maintained on the Internet.

A php conference allows you to store user information on the server for future use (such as user names and shopping items. However, the session information is temporary, and the user will be deleted and has left the website. If you need a long-term storage, you may need to store data in the database.

The work of the meeting, by creating a unique ID for each passenger and storage variable (UID) and based on this UID. The UID is either stored in the cookie or transmitted in the URL.


--------------------------------------------------------------------------------

Start a PHP session
Before you can store user information in PHP sessions, you must first start the meeting.

Note: The session_start () function must appear in the <HTML> tag:

<? Php session_start ();?>

<Html>
<Body>

</Body>
</Html>

The above code registers a user's session with the server, allows you to start saving user information, and specifies the UID of the user's session.


--------------------------------------------------------------------------------

Stored in session variables
The correct method to store and retrieve SESSION variables is to use the PHP $ _ SESSION variable:

<? Php
Session_start ();
// Store session data
$ _ SESSION ['View'] = 1;
?>

<Html>
<Body>

<? Php
// Retrieve session data
Echo "Pageviews =". $ _ SESSION ['View'];
?>

</Body>
</Html>

Output:

Pageviews = 1

In the following example, we create a simple page and click the counter. Check whether the "opinion" variable has been set in the isset () function. If "comments" have been established, we can add our counters. If the comment does not exist, we create a comment variable and set it to 1:

<? Php
Session_start ();

If (isset ($ _ SESSION ['View'])
$ _ SESSION ['View'] = $ _ SESSION ['View'] + 1;
Else
$ _ SESSION ['View'] = 1;
Echo "Views =". $ _ SESSION ['View'];
?>

Destroy a 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.

<? Php
Unset ($ _ SESSION ['View']);
?>

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

<? Php
Session_destroy ();
?>

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.