Example of using Session and Cookie in PHP

Source: Internet
Author: User
Tags session id php code set cookie setcookie unique id website server

Session and Cookie are used for tracking user information, such as user login verification, recording user browsing history, storing shopping cart data, and limiting user Session validity time. Today, let's take a look at how PHP operates sessions and cookies.

Session

PHP $ _ SESSION can store the current user data. When a user accesses a WEB site, PHP creates a session ID for each user who accesses the website. This ID is a unique ID, the session data is stored on the client and stored on the server. PHP can store different user information. When the session expires, the session information becomes invalid.
When using Session, you must add session_start () to the page header to tell the server to start using session, and there should be no output before it; otherwise, an error will be reported.
 
<? Php
Session_start ();
 
// PHP code...
?>

Set and retrieve Session in PHP

We can use PHP's $ _ SESSION to set and obtain Session data, for example:
 
<? Php
Session_start ();
 
// Set a session value
$ _ SESSION ["name"] = "Hello ";
 
// Save the session as an array
$ _ SESSION ["arr"] = array ('name' => 'hello', 'URL' => 'http: // www.helloweba.com ', 'type' => 'Website ');
?>

Once the Session data is stored, we can use the Session on the website. For example, we can obtain the Session data on another page:
 
<? Php
Session_start ();
 
// Obtain the name of the saved Session
Echo $ _ SESSION ["name"];
 
// Print the array session
Print_r ($ _ SESSION ["arr"]);
?>

Delete Session in PHP

When you no longer use Session, you can use PHP to delete and clear session data as follows:
 
<? Php
Unset ($ _ SESSION ["name"]);
?>

To clear all Session information of the current user, use the following code:
 
<? Php
Session_destroy ();
?>


Cookie

Cookie is a temporary file created by the website service end accessed by the user to the current client. It is used to save user information so that the website server can identify user information when the user continues to visit the website, common cookies are used to save user interfaces, user IDs, and other data.

Set Cookie in PHP

We can use setcookie () of PHP to create a cookie on the client. This function provides three main parameters: cookie name, value, and validity period.
 
<? Php
$ Cookie_val = 'Chrome ';
Setcookie ("browser", $ cookie_val, time () + 3600 );
?>
After running the preceding code, a Cookie named Chrome is created and saved on the client for one hour. After one hour, the cookie information becomes invalid.

PHP receives cookies

After a Cookie is created, we can easily obtain the cookie value. The usage of $ _ COOKIE in PHP is as follows:
 
<? Php
If (isset ($ _ COOKIE ['Browser ']) {
Echo 'Your browser is: '. $ _ COOKIE ['browser'];
}
?>


Delete Cookie in PHP

If you want to completely delete the stored cookie information on your machine, you can use the following code:
 
<? Php
Setcookie ("browser", "", time ()-3600 );
?>

The code above clears the cookie named "browser" and sets the validity period to 1 hour ago. The cookie information is completely cleared.
In addition, there are examples of front-end Javascript operations on cookies, which are described in this article.
From the perspective of beginners, this article explains the entry-level knowledge of PHP: the application of Session and Cookie. We don't have to go deep into its principles, as long as it will be used. 2015 is coming soon. Next, Helloweba plans to share several interactive projects between the frontend and backend PHP with you. Of course Session and Cookie will be used, such as WEB chat rooms, online videos, and HTML5 live streaming, coming soon.

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.