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 to you how to use setcookie in php to implement a second-level domain name.
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, 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 the two domain names may be on different servers, but we still hope that the second-level domain names can smoothly access the cookies of the primary domain names, and the primary domain names can smoothly access the cookies of the second-level domain names, for example, bbs.phpfensi.com wants to access the cookies of www.phpfensi.com and blog.phpfensi.com.
The following describes three global cookie settings that you may frequently hear.
The first example code is as follows:
Setcookie ("phpfensi", $ s, time () + 3600*12, '/', '* .phpfensi.com ');
* No. a cookie cannot be set successfully.
The second example code is as follows:
Setcookie ("phpfensi", $ s, time () + 3600*12, '/', '.phpfensi.com ');
A global cookie ss.phpfensi.com is successfully set and can be correctly read.
The third example code is as follows:
Setcookie ("hzhui", $ s, time () + 3600*12, '/', 'hzhui. com ');
A global cookie ss.phpfensi.com is successfully set and can be correctly read.
This method can only be read by phpfensi.com. the test is successful in FireFox and the test is successful in IE. the code is as follows:
Setcookie ("phpfensi", $ s, time () + 3600*12, '/', 'SS .phpfensi.com ');
Set a cookie that can be correctly read only under the ss.phpfensi.com domain name. The standard saying on the network is .phpfensi.com. There are also '*' statements (This statement is completely incorrect ). we recommend a good php cookie operation class. you 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 (emptyempty ($ cname) & isset ($ this )){
- $ Cname = $ this-> _ name;
- }
-
- If (! Emptyempty ($ _ 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 (emptyempty ($ value )){
- Unset ($ this-> _ val [$ var]);
- }
- }
- Function clear ()
- {
- $ This-> _ val = array ();
- }
- Function set ()
- {
- If (emptyempty ($ 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 );
- }
- }
- ?>