Usage and understanding of sessions in thinkphp! -Php Tutorial

Source: Internet
Author: User
Tags define session
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

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

  2. Sessions are stored on servers. different URLs mean different servers. Destroy one of them, and others are not affected.

  3. When you close your browser, the session will expire.

  4. Different browsers open a session for the same website.

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

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

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.