删除cookie的值 PHP,php cookie类(设置、获取、删除cookie值)

本文介绍下,一个功能强大的php cookie操作类,可以完成设置cookie、获取cookie值、删除cookie值等操作,有需要的朋友参考下。

分享一个php cookie操作的类,可以设置cookie、获取cookie、删除cookie。

代码:

_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);

}

}

?>

调用示例:

1,设置cookie

put("namefirst","Jo");

$PHP_COOKIE->put("namelast","Foo");

$PHP_COOKIE->put("number","1234");

$PHP_COOKIE->put("time",time());

// Set the cookie

$PHP_COOKIE->set();

$PHP_COOKIE=new PHP_COOKIE("test_cookie 123");

// Add the variables to be saved in the cookie

$PHP_COOKIE->put("namefirst","Jo123");

$PHP_COOKIE->put("namelast","Foo13");

$PHP_COOKIE->put("number","123413");

// Set the cookie

$PHP_COOKIE->set();

echo "

The values saved in the cookie test_cookie are:";

echo "

namefirst: = $_COOKIE[namefirst]";

echo "

namelast: = $_COOKIE[namelast]";

echo "

number: = $_COOKIE[number]";

echo "

time: = $_COOKIE[time]";

echo "

END";

?>

2,获取cookie

3,删除cookie

set();

// Clear all values

#$PHP_COOKIE->clear();

?>

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值