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

  • 来源: 互联网 作者: rocket   2008-03-17/10:21
  • <?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);
    }
    }
    ?>


    评论 {{userinfo.comments}}

    {{money}}

    {{question.question}}

    A {{question.A}}
    B {{question.B}}
    C {{question.C}}
    D {{question.D}}
    提交

    驱动号 更多