PHP Session Implementation principle

Source: Internet
Author: User

Consider the following questions first:

Disable cookie,session can I use it?

How to store the session storage database/memcached/redis

How do I implement a strict 30-minute expired session? 、

How do I practice session sharing for a Web server cluster?

How do you implement session sharing for two different domain sites?

Answer: Yes!

The relationship between a cookie and a session

The cookie is passed session_id for the server to determine the session file.

So as long as can be passed to the server session_id,session can be used normally.

And a cookie is just a way of passing session_id.

SESSION_ID can also be passed with a URL

The php.ini configuration is as follows:

session.use_only_cookies = 0

Session.use_trans_sid = 1

It's OK.

Implementation code:

class sess {    protected static  $mem  = null;     public static function open ()  {         if (self:: $mem  === null)  {             self:: $mem  = new memcache ();             self:: $mem->connect (' localhost '  , 11211);         }    }    public static function close ()  {        self:: $mem->close ();    }     public static function read ($id)  {         return self:: $mem->get ($id);    }    public  Static function write ($id  ,  $data)  {        return self:: $mem->add ($ ID, $data  , false);     }    public static function  destroy ($id)  {        return self:: $mem->delete ($id );     }    PUBLIC STATIC FUNCTION GC ($lifetime)  {         //     }}session_set_save_handler (' Sess::open ',  ' sess::close ',  ' sess::read ',  ' sess::write ',  ' sess::d estroy ',  ' SESS::GC ') ;

PHP Session Implementation principle

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.