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