Session function: Session can save data for a long time, not lost.
Sessions are used in: 1. Login, save login information 2. Save cart Information 3. Save CAPTCHA Information
Defining session Constants
Define (' Wxuser ', ' wxuser_session ');d efine (' MEMBER ', ' member_session ');d efine (' MERCHANT ', ' merchant_session '); Define (' AGENT ', ' agent_session ');d efine (' CART ', ' cart_session ');
Assign a value to a session
Session (MERCHANT, $user [' id ']);
Get user Information by session
$user = M (' user ')->where (array (' id ' = = Session (MERCHANT)))->find (); $this->user = $user; $this->user_id = $user [' id '];
If the session exists, go directly to the main interface, otherwise enter the login screen
Merchant Login Public function login () { if (session (MERCHANT)) { $this->redirect (U (' User/index/index ')); } else { $this->display (); } }
Log out, destroy session
Log out public function userlogout () { session (NULL); Session_destroy (); Unset ($_session); $this->redirect (U (' Home/index/index ')); }
Some understandings and tests about the session
The same browser, the same session name, different URLs, save the location of the different. Destroy one of them, and the other did not receive the impact.
The session is saved on the server, and different URLs mean different servers. Destroy one of them, and the others did not receive the effect.
When you close the browser, the session will be dead.
The same URL, different browser open has a corresponding session.
Most of the session mechanism uses the cookie in the process to save the session_id, the process will automatically disappear after the browser is closed, the cookie in the process disappears naturally, then session_id disappears, When you connect to the server again, you will not be able to find the original session.
We can click on the next automatic login when landing, such as "Remember Me for a week". This will use the other cookie that we mentioned earlier-the cookie on the hard disk, when session_id is kept in a cookie on the hard disk for a long time until it expires.