Method of cookie in php to Realize Accessibility Operation of Secondary Domain Name

  • 2021-07-26 07:03:42
  • OfStack

In this paper, an example is given to describe the method of cookie in php to realize the two-level domain name accessibility operation. Share it for your reference. The specific methods are as follows:

cookie is commonly used in some applications. Suppose I have a multi-level domain name that requires to access cookie bound to the main domain name at the same time. Here, I will give you a specific introduction to the method that the second-level domain name can successfully access the main domain name cookie value by using setcookie in php.

Sometimes the two domain names may be on different servers, but we still hope that the level 2 domain name can successfully access cookie of the primary domain name, and the primary domain name can smoothly access cookie of the level 2 domain name, such as sc. ofstack. com. We hope to access www. ofstack. com and blog. ofstack. com's cookie

Here are three ways to set up the global cookie that you may often hear.

The first example code is as follows:

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

* Unable to successfully set 1 cookie

The second example code is as follows:

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

Successfully set 1 global cookie, so that it can be read correctly under ss. ofstack. com

The third example code is as follows:

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

Successfully set 1 global cookie, and it can be read correctly under ss. ofstack. com

This way of understanding is that only ofstack. com can be read, tested successfully under FireFox, tested successfully under IE, and the code is as follows:

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

Set an cookie that can be read correctly only under the domain name ss. ofstack. com. The standard statement on the network is. ofstack. com, and there is also a statement of * (this statement is completely wrong). The following recommends a good class of php cookie operation, which can set cookie, obtain cookie and delete cookie. The 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; // ~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 globally 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(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 is helpful to everyone's PHP programming.


Related articles: