php劫持cookie,php 处理cookie的类功能实例

/**

* 一个用于处理cookie的PHP类

*

* @param

* @arrange 网: 512Pic.com

**/

class cookieClass{

var $cName = '';

var $cTime = '';

var $cSerialize = false;

var $cPath = '';

function cookieClass($cookieName,$cookieTimeout,$cookieSerialize = false,$cookiePath = "/"){

$this->cName = $cookieName;

$this->cTime = $cookieTimeout;

$this->cSerialize = $cookieSerialize;

$this->cPath = $cookiePath;

// This should fix the issue if you have cookies set and THEN turn on the serialization.

$iname = $this->cName . "_S";

if($this->cSerialize && !isset($_COOKIE[$iname])){

$cookArr = array();

foreach($_COOKIE as $name=>$val){

if(strpos($name,$this->cName) !== false ){ // make sure it is a cookie set by this application

$subname = substr($name,strlen($this->cName) + 1);

$cookArr[$subname] = $val;

$this->KillCookie($name);

}

}

$this->WriteCookie($cookArr);

}

// This is the opposite from above. changes a serialized cookie to multiple cookies without loss of data

if(!$this->cSerialize && isset($_COOKIE[$iname])){

$cookArr = unserialize($_COOKIE[$iname]);

$this->KillCookie($iname);

$this->WriteCookie($cookArr);

}

}

function DestroyAllCookies(){

foreach($_COOKIE as $name=>$val){

if(strpos($name,$this->cName) !== false){

$_COOKIE[$name] = NULL;

$this->KillCookie($name);

}

}

}

function ReadCookie($item){

if($this->cSerialize){

$name = $this->cName . "_S";

if(isset($_COOKIE[$name])){

// handle the cookie as a serialzied variable

$sCookie = unserialize($_COOKIE[$name]);

if(isset($sCookie[$item])){

return $sCookie[$item];

}else{

return NULL;

}

}else{

return NULL;

}

}else{

$name = $this->cName . "_" . $item;

if(isset($_COOKIE[$name])){

// handle the item as separate cookies

return $_COOKIE[$name];

}else{

return NULL;

}

}

}

function KillCookie($cName){

$tStamp = time() - 432000;

setcookie($cName,"",$tStamp,$this->cPath);

}

function WriteCookie($itemArr){

if($this->cSerialize){

$sItems = serialize($itemArr);

$name = $this->cName . "_S";

$_COOKIE[$name] = $sItems;

$tStamp = time() + $this->cTime;

setcookie($name,$sItems,$this->cPath);

}else{

$tStamp = time() + $this->cTime;

foreach($itemArr as $nam=>$val){

$name = $this->cName . "_" . $nam;

$_COOKIE[$name] = $val;

setcookie($name,$val,$this->cPath);

}e

}

}

}

/*** 来自编程之家 jb51.cc(jb51.cc) ***/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值