Usage and understanding of sessions in thinkphp! The role of a session: the session can save data for a long time without loss.
Session is commonly used in: 1. log on, save login information 2. Save Shopping Cart Information 3. save verification code information
Define session constants
define('WXUSER', 'wxuser_session');define('MEMBER', 'member_session');define('MERCHANT', 'merchant_session');define('AGENT', 'agent_session');define('CART', 'cart_session');
Assign a value to a session
session(MERCHANT, $user['id']);
Obtain user information based on session
$user = M('user')->where(array('id' => session(MERCHANT)))->find();$this->user = $user;$this->user_id = $user['id'];
If the session exists, go to the main interface. Otherwise, go to the logon interface.
// The MERCHANT logs on to the public function login () {if (session (MERCHANT) {$ this-> redirect (U ('user/Index/index '));} else {$ this-> display ();}}
Log out and destroy the session
// Log out of the public function userLogout () {session (null); // session_destroy (); // unset ($ _ SESSION ); $ this-> redirect (U ('Home/Index/index '));}
Session comprehension and testing
In the same browser, the same session name and website address are stored in different locations. Destroy one of them, and the other is not affected.
Sessions are stored on servers. different URLs mean different servers. Destroy one of them, and others are not affected.
When you close your browser, the session will expire.
Different browsers open a session for the same website.
Most Session mechanisms use the Cookie in the process to save the Session_id. when the browser is closed, the process disappears automatically, and the Cookie in the process disappears naturally, so the Session_id disappears, when the server is connected again, the original Session cannot be found.
You can click "remember me for one week" next time you log on ". This requires another Cookie we mentioned above-the Cookie in the hard disk. at this time, Session_id will be stored in the Cookie on the hard disk for a long time until it becomes invalid.