Php cookies enable second-level domain names to access operational instances

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

  1. /**
  2. * Php cookie
  3. * Class: PHP_COOKIE
  4. */
  5. Class PHP_COOKIE
  6. {
  7. Var $ _ name = "";
  8. Var $ _ val = array ();
  9. Var $ _ expires;
  10. Var $ _ dir = '/'; // all dirs
  11. Var $ _ site = '';
  12. Function PHP_COOKIE ($ cname, $ cexpires = "", $ cdir = "/", $ csite = "")
  13. {
  14. $ This-> _ name = $ cname;
  15. If ($ cexpires ){
  16. $ This-> _ expires = $ cexpires;
  17. }
  18. Else {
  19. $ This-> _ expires = time () + 60*60*24*30*12 ;//~ 12 months
  20. }
  21. $ This-> _ dir = $ cdir;
  22. $ This-> _ site = $ csite;
  23. $ This-> _ val = array ();
  24. $ This-> extract ();
  25. }
  26. Function extract ($ cname = "")
  27. {
  28. If (! Isset ($ _ COOKIE )){
  29. Global $ _ COOKIE;
  30. $ _ COOKIE = $ GLOBALS ["HTTP_COOKIE_VARS"];
  31. }
  32. If (emptyempty ($ cname) & isset ($ this )){
  33. $ Cname = $ this-> _ name;
  34. }
  35.  
  36. If (! Emptyempty ($ _ COOKIE [$ cname]) {
  37. If (get_magic_quotes_gpc ()){
  38. $ _ COOKIE [$ cname] = stripslashes ($ _ COOKIE [$ cname]);
  39. }
  40. $ Arr = unserialize ($ _ COOKIE [$ cname]);
  41. If ($ arr! = False & is_array ($ arr )){
  42. Foreach ($ arr as $ var => $ val ){
  43. $ _ COOKIE [$ var] = $ val;
  44. If (isset ($ GLOBALS ["PHP_SELF"]) {
  45. $ GLOBALS [$ var] = $ val;
  46. }
  47. }
  48. }
  49. If (isset ($ this) $ this-> _ val = $ arr;
  50. }
  51. // Remove the cookie globally
  52. Unset ($ _ COOKIE [$ cname]);
  53. Unset ($ GLOBALS [$ cname]);
  54. }
  55. Function put ($ var, $ value)
  56. {
  57. $ _ COOKIE [$ var] = $ value;
  58. $ This-> _ val ["$ var"] = $ value;
  59. If (isset ($ GLOBALS ["PHP_SELF"]) {
  60. $ GLOBALS [$ var] = $ value;
  61. }
  62. If (emptyempty ($ value )){
  63. Unset ($ this-> _ val [$ var]);
  64. }
  65. }
  66. Function clear ()
  67. {
  68. $ This-> _ val = array ();
  69. }
  70. Function set ()
  71. {
  72. If (emptyempty ($ this-> _ val )){
  73. $ Cookie_val = "";
  74. }
  75. Else {
  76. $ Cookie_val = serialize ($ this-> _ val );
  77. }
  78.  
  79. If (strlen ($ cookie_val)> 4*1024 ){
  80. Trigger_error ("The cookie $ this-> _ name exceeds the specification for the maximum cookie size. Some data may be lost", E_USER_WARNING );
  81. }
  82. Setcookie ("$ this-> _ name", $ cookie_val, $ this-> _ expires, $ this-> _ dir, $ this-> _ site );
  83. }
  84. }
  85. ?>

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.