In php, cookies enable second-level domain names to access operation instances. Cookies are frequently used in some applications. I have a multi-level domain name that can simultaneously access the Cookies bound to the primary domain name, next, I will introduce how to use setcookie to implement second-level domain name cookies in php. in some applications, I have a multi-level domain name that can simultaneously access the Cookies bound to the primary domain name, the following describes how to use setcookie in php to enable second-level domain names to successfully access the cookie value of the primary domain name.
Sometimes two domain names may be on different servers, but we still hope that the second-level domain name can smoothly access the cookie of the primary domain name, and the primary domain name can smoothly access the cookie of the second-level domain name. For example, bbs.hzhuti.com wants to access cookies from www.hzhuti.com and blog.hzhuti.com.
The following describes three global cookie settings that you may hear
The code is as follows: |
|
Setcookie ("hzhui", $ s, time () + 3600*12, '/', '* .hzhuti.com '); |
* No. a cookie cannot be set successfully.
The code is as follows: |
|
Setcookie ("hzhui", $ s, time () + 3600*12, '/', '.hzhuti.com '); |
A global cookie ss.hzhuti.com is successfully set and can be correctly read.
The code is as follows: |
|
Setcookie ("hzhui", $ s, time () + 3600*12, '/', 'hzhui. com '); |
A global cookie ss.hzhuti.com is successfully set and can be correctly read.
In this way, Yue Xiaosheng understands that only hzhuti.com can read data. Yue Xiaosheng is successfully tested in FireFox. IE succeeded
The code is as follows: |
|
Setcookie ("hzhui", $ s, time () + 3600*12, '/', 'SS .hzhuti.com '); |
Set a cookie that can be correctly read only under the ss.hzhuti.com domain name
The standard statement on the network is .hzhuti.com.
There are also '*' statements (This statement is completely incorrect ...)
We recommend a good php cookie operation class, which can set, retrieve, and delete cookies.
The code is as follows: |
|
/** * Php cookie * Class: PHP_COOKIE */ Class PHP_COOKIE { Var $ _ name = ""; Var $ _ val = array (); Var $ _ expires; Var $ _ dir = '/'; // all dirs Var $ _ site = ''; Function PHP_COOKIE ($ cname, $ cexpires = "", $ cdir = "/", $ csite = "") { $ This-> _ name = $ cname; If ($ cexpires ){ $ This-> _ expires = $ cexpires; } Else { $ This-> _ expires = time () + 60*60*24*30*12 ;//~ 12 months } $ This-> _ dir = $ cdir; $ This-> _ site = $ csite; $ This-> _ val = array (); $ This-> extract (); } Function extract ($ cname = "") { If (! Isset ($ _ COOKIE )){ Global $ _ COOKIE; $ _ COOKIE = $ GLOBALS ["HTTP_COOKIE_VARS"]; } If (empty ($ cname) & isset ($ this )){ $ Cname = $ this-> _ name; } If (! Empty ($ _ COOKIE [$ cname]) { If (get_magic_quotes_gpc ()){ $ _ COOKIE [$ cname] = stripslashes ($ _ COOKIE [$ cname]); } $ Arr = unserialize ($ _ COOKIE [$ cname]); If ($ arr! = False & is_array ($ arr )){ Foreach ($ arr as $ var => $ val ){ $ _ COOKIE [$ var] = $ val; If (isset ($ GLOBALS ["PHP_SELF"]) { $ GLOBALS [$ var] = $ val; } } } If (isset ($ this) $ this-> _ val = $ arr; } // Remove the cookie globally Unset ($ _ COOKIE [$ cname]); Unset ($ GLOBALS [$ cname]); } Function put ($ var, $ value) { $ _ COOKIE [$ var] = $ value; $ This-> _ val ["$ var"] = $ value; If (isset ($ GLOBALS ["PHP_SELF"]) { $ GLOBALS [$ var] = $ value; } If (empty ($ value )){ Unset ($ this-> _ val [$ var]); } } Function clear () { $ This-> _ val = array (); } Function set () { If (empty ($ this-> _ val )){ $ Cookie_val = ""; } Else { $ Cookie_val = serialize ($ this-> _ val ); } If (strlen ($ cookie_val)> 4*1024 ){ Trigger_error ("The cookie $ this-> _ name exceeds the specification for the maximum cookie size. Some data may be lost", E_USER_WARNING ); } Setcookie ("$ this-> _ name", $ cookie_val, $ this-> _ expires, $ this-> _ dir, $ this-> _ site ); } } ?> |
...