Home  >  Article  >  Backend Development  >  Example of how cookies can be used to implement secondary domain name accessibility in PHP_PHP Tutorial

Example of how cookies can be used to implement secondary domain name accessibility in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 16:57:36858browse

Cookies are very commonly used in some applications. I have a multi-level domain name that needs to be able to access the cookies bound to the main domain name at the same time. Let me introduce to you how to use setcookie in PHP to realize that the secondary domain name can successfully access the main domain name cookie. value method.

Sometimes the two domain names may be on different servers, but we still hope that the secondary domain name can successfully access the cookies of the primary domain name, and the primary domain name can successfully access the cookies of the secondary domain name. For example, bbs.hzhuti.com hopes to access the cookies of www.hzhuti.com and blog.hzhuti.com
Here are 3 global cookie setting methods that you may often hear

The code is as follows Copy code
 代码如下 复制代码

setcookie("hzhuti",$s,time()+3600*12,'/','*.hzhuti.com');

setcookie("hzhuti",$s,time()+3600*12,'/','*.hzhuti.com');

The number * could not successfully set a cookie
 代码如下 复制代码

setcookie("hzhuti",$s,time()+3600*12,'/','.hzhuti.com');

The code is as follows Copy code

setcookie("hzhuti",$s,time()+3600*12,'/','.hzhuti.com');

 代码如下 复制代码

setcookie("hzhuti",$s,time()+3600*12,'/','hzhuti.com');

Successfully set a global cookie and it can be read correctly under ss.hzhuti.com

The code is as follows Copy code
setcookie("hzhuti",$s,time()+3600*12,'/','hzhuti.com');

Successfully set a global cookie and it can be read correctly under ss.hzhuti.com
 代码如下 复制代码

setcookie("hzhuti",$s,time()+3600*12,'/','ss.hzhuti.com');

In this way, Yue Xiaosheng understands that only hzhuti.com can read it. Yue Xiaosheng successfully tested under FireFox. Successfully under IE
The code is as follows Copy code
setcookie("hzhuti",$s,time()+3600*12,'/','ss.hzhuti.com');

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

The standard statement on the Internet is .hzhuti.com.

There is also a * statement (this statement is completely wrong...)

The following recommends a good php cookie operation class, which can set cookies, obtain cookies, 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(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;

}
// 在全局范围内移除cookie
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 be lost", E_USER_WARNING);
}
setcookie("$this->_name", $cookie_val, $this->_expires, $this->_dir, $this->_site);
  }
}
?>

Copy code
/**
* 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;

$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 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(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 be lost", E_USER_WARNING);
}
setcookie("$this->_name", $cookie_val, $this->_expires, $this->_dir, $this->_site);
}
}
?> http://www.bkjia.com/PHPjc/631507.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631507.htmlTechArticleCookies are very commonly used in some applications. I have a multi-level domain name that needs to be able to access the cookies bound to the main domain name at the same time. , let me introduce to you in detail how to use setcookie to realize the second-level domain name in php...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn