php中cookie實現次層網域可訪問操作執行個體_PHP教程

來源:互聯網
上載者:User
cookie在一些應用中很常用,我有一個多級網域名稱要可以同時訪問主網域名稱綁定的cookie,下面我來給大傢具體介紹在php中利用setcookie實現次層網域可以成功訪問主網域名稱cookie值的方法。

有時候兩個網域名稱可能在不同的伺服器上,但是我們依然希望次層網域能夠順利訪問主網域名稱的cookie,主網域名稱可以順利訪問次層網域的cookie。比如bbs.hzhuti.com 希望能訪問www.hzhuti.com和blog.hzhuti.com的cookie
下面介紹3種你可能常聽到的全域cookie設定方式

代碼如下 複製代碼

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

*號無法成功設定一個cookie

代碼如下 複製代碼

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

成功設定一個全域cookie ss.hzhuti.com下也能正確讀取

代碼如下 複製代碼

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

成功設定一個全域cookie ss.hzhuti.com下也能正確讀取

這種方式月小升的理解是僅僅hzhuti.com能夠讀取。月小升在FireFox下測試成功。IE下成功

代碼如下 複製代碼

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

設定一個僅僅在ss.hzhuti.com網域名稱下可以正確讀取的cookie

網路上標準的說法為.hzhuti.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);
}
}
?>

http://www.bkjia.com/PHPjc/631507.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631507.htmlTechArticlecookie在一些應用中很常用,我有一個多級網域名稱要可以同時訪問主網域名稱綁定的cookie,下面我來給大傢具體介紹在php中利用setcookie實現次層網域...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.