|
本文介绍下,一个功能强大的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 "
2,获取cookie 显示用于测试的一些cookie值" ; echo " 3,删除cookie set(); // Clear all values #$PHP_COOKIE->clear(); ?> |
0
0
相关文章
php怎么换源码_php源码更换版本与替换方法
php在主机里面的源码怎么查看_看主机内php源码方法
php如何清除浏览器cookie_php设置过期时间为过去删除指定cookie
php数据整理中如何按相似度合并数组项_php相似度算法与阈值合并重复数据方法
php如何实现数据库字段自动翻译_php结合翻译api与缓存减少重复请求方案
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
相关专题
2026赚钱平台入口大全
2026年最新赚钱平台入口汇总,涵盖任务众包、内容创作、电商运营、技能变现等多类正规渠道,助你轻松开启副业增收之路。阅读专题下面的文章了解更多详细内容。
76
2026.01.31
无需付费的漫画app大全
想找真正免费又无套路的漫画App?本合集精选多款永久免费、资源丰富、无广告干扰的优质漫画应用,涵盖国漫、日漫、韩漫及经典老番,满足各类阅读需求。阅读专题下面的文章了解更多详细内容。
67
2026.01.31
漫画免费在线观看地址大全
想找免费又资源丰富的漫画网站?本合集精选2025-2026年热门平台,涵盖国漫、日漫、韩漫等多类型作品,支持高清流畅阅读与离线缓存。阅读专题下面的文章了解更多详细内容。
19
2026.01.31
热门下载
相关下载
精品课程
相关推荐
/
热门推荐
/
最新课程
第三期培训_PHP开发
共116课时 | 26.2万人学习








