PHP create and use sessioncookie variable _ PHP Tutorial

Source: Internet
Author: User
PHP creates and uses sessioncookie Variables .? Phpsessionstartsession_start (); to start a session, add $ _ SESSION [user_id] 123 at the beginning of the session program. assign a value to a session variable. // Session start
Session_start (); // starts a session. if you want to use the session program, you must add this sentence at the beginning.
$ _ SESSION ['User _ id'] = '000000'; // assign a value to a session variable. if the variable does not exist, it is created.

Echo $ _ SESSION ['User _ id']; // access the session variable

$ _ SESSION = array (); // clear all session variables

Session_destroy (); // clear the session ID
// Session end

// Cookie start
Setcookie ('User _ id', 123); // Create a cookie variable user_id = 123

Echo $ _ COOKIE ['User _ id']; // The access cookie variable is the same as the change variable.

Setcookie ('User _ id', 0, time ()-1); // delete the cookie variable
// Codie end

// This code is not running, but all usage methods are listed here. actually, different functions should be used on different pages. this will be demonstrated in the following example.
?>
// Session start
Session_start (); // starts a session. if you want to use the session program, you must add this sentence at the beginning.
$ _ SESSION ['User _ id'] = '000000'; // assign a value to a session variable. if the variable does not exist, it is created.

Echo $ _ SESSION ['User _ id']; // access the session variable

$ _ SESSION = array (); // clear all session variables

Session_destroy (); // clear the session ID
// Session end

// Cookie start
Setcookie ('User _ id', 123); // Create a cookie variable user_id = 123

Echo $ _ COOKIE ['User _ id']; // The access cookie variable is the same as the change variable.

Setcookie ('User _ id', 0, time ()-1); // delete the cookie variable
// Codie end

// This code is not running, but all usage methods are listed here. actually, different functions should be used on different pages. this will be demonstrated in the following example.
?>
Cookie, session is a way for WEB applications to maintain the user state

Cookie is the information of the stored client, which is sent to the server when the client connects to the server.

Session is the information stored on the server. From this perspective, session is safer than cookie.
When a session is created, the server returns an encrypted session id to the client to identify the user. the session id is generally stored in the cookie and transmitted by the URL when the cookie is unavailable.

The code above demonstrates how to create and use session cookie variables

Http://www.bkjia.com/PHPjc/364050.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/364050.htmlTechArticle? Php // session start session_start (); // start a session, to use the session program, add $ _ SESSION ['User _ id'] = '000000' at the beginning; // assign a value to a session variable...

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.