Home php教程 php手册 php中cookie实现二级域名可访问操作实例

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

Jun 02, 2016 am 09:14 AM
foreach php cookie

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



教程地址:

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

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 admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? Apr 27, 2023 pm 03:40 PM

What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList?

How to determine the number of foreach loop in php How to determine the number of foreach loop in php Jul 10, 2023 pm 02:18 PM

How to determine the number of foreach loop in php

PHP returns an array with key values ​​flipped PHP returns an array with key values ​​flipped Mar 21, 2024 pm 02:10 PM

PHP returns an array with key values ​​flipped

PHP returns the current element in an array PHP returns the current element in an array Mar 21, 2024 pm 12:36 PM

PHP returns the current element in an array

How to iterate through the properties of an object using the forEach function? How to iterate through the properties of an object using the forEach function? Nov 18, 2023 pm 06:10 PM

How to iterate through the properties of an object using the forEach function?

What is the difference between foreach and for loop What is the difference between foreach and for loop Jan 05, 2023 pm 04:26 PM

What is the difference between foreach and for loop

How to get the index value of the current element in php foreach loop How to get the index value of the current element in php foreach loop Mar 23, 2023 am 09:17 AM

How to get the index value of the current element in php foreach loop

What does php foreach mean? What does php foreach mean? Mar 10, 2023 am 09:54 AM

What does php foreach mean?

See all articles