How to implement second-level domain name access using cookies in php. cookie second-level domain name _ PHP Tutorial

Source: Internet
Author: User
Tags net domain
The second-level domain name can be accessed using cookies in php. In php, the cookie can be accessed by second-level domain names. in this article, the cookie second-level domain name describes how to implement second-level domain name access operations in php. I would like to share with you how to implement second-level domain name access using cookies in php. cookie second-level domain name

This example describes how to implement second-level domain name access using cookies in php. Share it with you for your reference. The specific method is as follows:

Cookies are frequently used in some applications. suppose I have a multi-level domain name that requires simultaneous access to the cookies bound to the primary domain name, the following describes how to use setcookie in php to successfully access the cookie value of the primary domain name for a second-level 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, SC .jb51.net wants to access cookies of www.jb51.net and blog.jb51.net.

The following describes three global cookie settings that you may frequently hear.

The first example code is as follows:

The code is as follows:

Setcookie ("jb51", $ s, time () + 3600*12, '/', '* .jb51.net ');

* No. a cookie cannot be set successfully.

The second example code is as follows:

The code is as follows:

Setcookie ("jb51", $ s, time () + 3600*12, '/', '.jb51.net ');

A global cookie is successfully set so that it can be correctly read under ss.jb51.net.

The third example code is as follows:

The code is as follows:

Setcookie ("jb51", $ s, time () + 3600*12, '/', 'jb51. net ');

A global cookie is successfully set and can be correctly read under ss.jb51.net.

This method can only be read by jb51.net. the test is successful in FireFox and the test is successful in IE. the code is as follows:

The code is as follows:

Setcookie ("jb51", $ s, time () + 3600*12, '/', 'SS .jb51.net ');

Set a cookie that can be correctly read only under the ss.jb51.net domain name. The standard saying on the network is .jb51.net. There is also a saying * (This statement is completely false ). we recommend a good php cookie operation class. you can set, retrieve, and delete cookies. the code is as follows:

The code is as follows:

<? Php
/**
* 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 );
}
}
?>

I hope this article will help you with PHP programming.

Examples in this article describes how to implement second-level domain name access using cookies in php. Share it with you for your reference...

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.