Home > php教程 > php手册 > body text

php中cookie实现二级域名可访问操作实例

WBOY
Release: 2016-06-02 09:14:47
Original
1960 people have browsed it
cookie在一些应用中很常用,我有一个多级域名要可以同时访问主域名绑定的cookie,下面我来给大家具体介绍在php中利用setcookie实现二级域名可以成功访问主域名cookie值的方法。

有时候两个域名可能在不同的服务器上,但是我们依然希望二级域名能够顺利访问主域名的cookie,主域名可以顺利访问二级域名的cookie。比如bbs.www.phprm.com 希望能访问www.phprm.com和blog.www.phprm.com的cookie
下面介绍3种你可能常听到的全局cookie设置方式

 代码如下 复制代码

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

*号无法成功设置一个cookie

 代码如下 复制代码

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

成功设置一个全局cookie ss.www.phprm.com下也能正确读取

 代码如下 复制代码

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

成功设置一个全局cookie ss.www.phprm.com下也能正确读取

这种方式月小升的理解是仅仅www.phprm.com能够读取。月小升在FireFox下测试成功。IE下成功

 代码如下 复制代码

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

设置一个仅仅在ss.www.phprm.com域名下可以正确读取的cookie

网络上标准的说法为.www.phprm.com这样。

也有*的说法(该说法完全错误。。。)

下面推荐一个不错的php cookie操作的类,可以设置cookie、获取cookie、删除cookie。

 

 代码如下 复制代码

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



教程地址:

欢迎转载!但请带上文章地址^^

Related labels:
source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!