In php, the session is rewritten to memcache, and the cross-top-level domain name restriction (application Singleton mode) is implemented. in the past two days, the problem of cross-top-level domain names in php has been studied: Finally, the problem is barely solved, it is because the method for generating $ _ COOKIE [& quot; mzlid & quot;] & #20540; is not perfect. rewrite the session in php to memcache, and implement cross-top-level domain name restrictions (application Singleton mode)
These two days I have studied the problem of php cross-top-level domain names: I finally made a stubborn solution (it is barely because the method for generating the value of $ _ COOKIE ["mzlid"] is not perfect.
First, let's talk about the following ideas:
The program can recognize every user to solve the problem of cross-top-level domain names,
In a single domain name, sessionid is used to generate the sessionid value based on the client IP address and other values, plus the time value and random number, then, a certain rule is used to generate and save it in the cookie. Each request page brings this value into the header, so that the php page on the server can recognize different users.
To cross-origin, the value of this Id must be the same as the value generated when accessing several top-level domain names.
Here, my practice is to generate A value based on the customer's IP address and some other environmental values of the customer and store it in A cookie. when the guest accesses website, this value is generated. if this value is not set for website B for the first time, it will be generated again based on the previous generation rules.
When the two websites read the value $ _ COOKIE ["mzlid"], the same customers get the same value.
Next, we store it in memcache. The key name is named after this value, and the content is saved as json data.
In the future, the value of this guest's session will be added, modified, or deleted, and the $ _ COOKIE ["mzlid"] key name value will be queried in memcache, and json will be output. OK.
Cross-server mode is implemented by using the memcache cluster function.
The following is the session class for php to operate memcache.
The generation of $ _ COOKIE ["mzlid"] is omitted.
Set ("host "," http://www.10np.com "); $ Mss-> close (); */class MySession {public $ memcache; public $ seid; private static $ instance; // stores the instantiated object, private function _ construct () {global $ SYS_config; $ this-> memcache = memcache_connect ($ SYS_config ["memcachedIP"], $ SYS_config ["memcacheddk"]);} // initialize function init () {if (! Isset (self ::$ instance) {$ c = _ CLASS __; self ::$ instance = new $ c;} return self ::$ instance ;} /*** k value sessionk value * $ v content value */function set ($ k, $ v) {$ this-> seid = ($ _ COOKIE ["mzlid"]); if ($ this-> seid = "") return "none "; // get the existing value $ a = memcache_get ($ this-> memcache, $ this-> seid); if ($ a) $ arr = json_decode ($, true); $ arr [$ k] = $ v; $ a = json_encode ($ arr); $ f = memcache_set ($ this-> memcache, $ this-> seid, $ a, MEMCACHE_COMPRESSED, 36000 ); }/*** K value sessionk * $ v content value */function get ($ k, $ v) {$ this-> seid = ($ _ COOKIE ["mzlid"]); if ($ this-> seid = "") return ""; $ a = memcache_get ($ this-> memcache, $ this-> seid); $ arr = json_decode ($ a, true); return ($ arr [$ k])? $ Arr [$ k]: "";}/*** delete a value * if the $ k value is null, delete all session values of this user */function del ($ k) {$ this-> seid = ($ _ COOKIE ["mzlid"]); if ($ this-> seid = "") return ""; if ($ k = "") {memcache_delete ($ this-> memcache, $ this-> seid); return;} $ a = memcache_get ($ this-> memcache, $ this-> seid); $ arr = json_decode ($ a, true ); unset ($ arr [$ k]); memcache_set ($ this-> memcached, $ this-> seid, json_encode ($ arr), MEMCACHE_COMPRESSED, 36000 );} /*** disable memcache connection */functi On close () {memcache_close ($ this-> memcache);} // prevents users from copying objects to the public function _ clone () {trigger_error ('Do not copy the object. ', E_USER_ERROR) ;}}?>