PHP cookies enable two-level domain access to operational examples _php tutorial

Source: Internet
Author: User
Tags setcookie
Cookies are commonly used in some applications, I have a multi-level domain name to be able to access the primary domain binding cookie, let me give you a specific introduction in PHP using Setcookie to achieve a two-level domain name can successfully access the primary domain cookie value method.

Sometimes two domain names may be on different servers, but we still want the level two domain name to be able to access the primary domain of the cookie, the primary domain can access the two-level domain name cookies. For example, bbs.hzhuti.com would like to have access to www.hzhuti.com and blog.hzhuti.com cookies.
Here are 3 ways you can often hear global cookie settings

The code is as follows Copy Code

Setcookie ("Hzhuti", $s, Time () +3600*12, '/', ' *.hzhuti.com ');

* Cannot set a cookie successfully

The code is as follows Copy Code

Setcookie ("Hzhuti", $s, Time () +3600*12, '/', '. hzhuti.com ');

Successfully set a global cookie ss.hzhuti.com can also be read correctly

The code is as follows Copy Code

Setcookie ("Hzhuti", $s, Time () +3600*12, '/', ' hzhuti.com ');

Successfully set a global cookie ss.hzhuti.com can also be read correctly

This way the understanding of the month's small liter is only hzhuti.com able to read. The month small liter tests successfully under the Firefox. Success under IE

The code is as follows Copy Code

Setcookie ("Hzhuti", $s, Time () +3600*12, '/', ' ss.hzhuti.com ');

Set a cookie that can be read correctly only under the ss.hzhuti.com domain name

The standard on the web is said to be. Hzhuti.com this way.

There is also a saying (that is totally wrong ...). )

Here is a good class of PHP cookie manipulation that can set cookies, get cookies, and delete cookies.

The code is as follows Copy Code

/**
* PHP Cookie Class
* 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; ~ 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 cookies at global scope
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 lost ", e_user_warning);
}
Setcookie ("$this->_name", $cookie _val, $this->_expires, $this->_dir, $this->_site);
}
}
?>

http://www.bkjia.com/PHPjc/631507.html www.bkjia.com true http://www.bkjia.com/PHPjc/631507.html techarticle cookies are commonly used in some applications, I have a multi-level domain name to be able to access the primary domain binding cookie, let me give you specific introduction in PHP using Setcookie to achieve level two domain name ...

  • Related Article

    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.