SiteCookie:整个站点只要用一个COOKIE,但可以保存

资讯 驱动中国 rocket 948 次阅读 0 条评论
分享 Q

<?PHP
//
// Class: SiteCookie
// Version: 1.0
// Author: Brian Moon <brian@phorum.org>
// Purpose: Easily handle multiple cookies for a site in one browser cookie.
// Whatever you enter with put() is added to the single cookie stored
// when you call set(). The cookie name the browser receives is the
// string specified when creating the object. The constructor reads
// the browser cookie and creates the variables for use in your code.
//
// Usage:
//
// $cookie=new SiteCookie("mycookie");
//
// $cookie->put("bob","tom2"); // the difference between using put() and
// $cookie->put("tom","bob2"); // the setting the val array is that the
// // or you can do: // variables bob and tom are created by
// $cookie->val["bob"]="tom2"; // put(). By just setting the var array,
// $cookie->val["tom"]="bob2"; // the variables are not created.
//
// $cookie->set();
//
// echo $bob;
// echo $tom;
//
// Properties:
//
// $name: the name that the cookie is stored with in the browser.
// $val: an internal array to hold the cookie information.
// $expires: the expiration date of the cookie. default 1 year.
// $dir: the http dir that cookie is set to. default is /.
// $site: the domain for the cookie. default is none. The browser
// will use the current domain.
class SiteCookie {
var $name="";
var $val=array();
var $expires=time()+ 31536000; // one year
var $dir='/'; // all dirs
var $site='';
function SiteCookie($cname, $cexpires="", $cdir=""){
GLOBAL $$cname
$this->name=$cname;
if($cexpires) $this->expires=$cexpires;
if($cdir) $this->dir=$cdir;
@parse_str($$cname);
@parse_str(str_replace("GLOBALS[", "val[", $$cname));
$this->val=$val;
}
function put($var, $value){
GLOBAL $$var;
$$var=$value;
$this->val["$var"]=$value;
}
function set(){
if(is_array($this->val)){
reset($this->val);
$cnt=count($this->val);
$curr=current($this->val);
$cookie_val='';
for($x=0;$x<$cnt;$x++){
$key=key($this->val);
$cookie_val.="GLOBALS[$key]=$curr";
$curr=next($this->val);
if($curr) $cookie_val.='&';
}
}
setcookie("$this->name", $cookie_val, $this->expires, $this->dir, $this->site);
}
}
?>

可以 保存 一个 只要 整个 站点 // the var t
版权声明:本文由驱动中国整理发布,转载需注明出处与原文链接。如有疑问请联系 editor@qudong.com
本文价值 成为第一个评分的人
登录后为本文打分
网友评论0 条评论
正在回复 的评论取消
评论加载中…
🔐

登录后参与互动

评论、点赞均可赚积分
连续签到、邀请好友也有奖励 🎁

电脑端: 微信扫码登录 微信内: 一键登录

微信扫一扫

打开微信「扫一扫」,分享本文到朋友圈或好友