The use and understanding of the session in thinkphp!

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

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

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

    3. When you close the browser, the session will be dead.

    4. The same URL, different browser open has a corresponding session.

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

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

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