PHP cookies Implement a two-level domain name accessible operation method, cookie two-level domain name _php tutorial

Source: Internet
Author: User
Tags setcookie net domain

Cookies in PHP implement a two-level domain name accessible operation method, cookie two-level domain name


The examples in this article describe the ways in which cookies in PHP implement the two-level domain access operation. Share to everyone for your reference. Here's how:

Cookies are commonly used in some applications, assuming that I have a multi-level domain name requirement to access the primary domain binding cookie at the same time, let us give you a detailed introduction to the method of using Setcookie in PHP to achieve a two domain name can successfully access the primary domain cookie value.

Sometimes two domain names may be on different servers, but we still want the two-level domain name to be able to access the primary domain of the cookie, the primary domain can access the two-level domain name cookies, such as sc.jb51.net want to access www.jb51.net and Blog.jb51.net's Cookie

Here are 3 ways you can often hear global cookie settings.

The first instance code is as follows:
Copy the Code code as follows: Setcookie ("jb51", $s, Time () +3600*12, '/', ' *.jb51.net ');

* Cannot set a cookie successfully

The second example code is as follows:
Copy the Code code as follows: Setcookie ("jb51", $s, Time () +3600*12, '/', '. jb51.net ');

Successfully set a global cookie so that it can be read correctly under Ss.jb51.net

The third example code is as follows:
Copy the Code code as follows: Setcookie ("jb51", $s, Time () +3600*12, '/', ' jb51.net ');

Successfully set a global cookie that can be read correctly under Ss.jb51.net

The understanding of this way is only jb51.net can read, under the Firefox test success, ie under test success, the code is as follows:
Copy the Code code as follows: Setcookie ("jb51", $s, Time () +3600*12, '/', ' ss.jb51.net ');

Set a cookie that can be read correctly only under the Ss.jb51.net domain name, which is the standard on the network. Jb51.net this way, there is also a saying (which is completely wrong). Here's a nice PHP cookie class that you can set up cookies, To obtain cookies and delete cookies, the code is as follows:
Copy CodeThe code is as follows: <?php
/**
* 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 (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 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 (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 lost ", e_user_warning);
}
Setcookie ("$this->_name", $cookie _val, $this->_expires, $this->_dir, $this->_site);
}
}
?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/909331.html www.bkjia.com true http://www.bkjia.com/PHPjc/909331.html techarticle Cookies in PHP implementation of the two-level domain name accessible operation method, cookie two domain name This article describes a cookie in PHP to implement a two-level domain name accessible operation method. Share to everyone 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.