How to operate sessions and cookies in PHP, sessioncookie_PHP tutorial

Source: Internet
Author: User
Tags set cookie website server
In PHP, how is Session and Cookie operated? sessioncookie. How are Session and Cookie operations in PHP? the $ _ SESSION of sessioncookieSessionPHP can store the current user data information. when a user accesses a WEB site, PHP will give each accessed user how the Session and Cookie are operated in PHP, sessioncookie

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 saved Session name 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.
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.

Reset Session PHP's $ _ SESSION can store the current user data. when a user accesses a WEB site, PHP will give it to each user who accesses the website...

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.