This article describes a powerful PHP cookie operation class, you can set a cookie, get cookie value, delete the cookie value and other operations, the need to refer to a friend. Share a PHP cookie action class that can set cookies, get cookies, and delete cookies. Code:
_name= $cname; if ($cexpires) {$this->_expires= $cexpires;} else{$this->_expires=time () + 60*60*24*30*12;//To 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; }//Remove cookies globally 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 C Ookie size. Some data may lost ", e_user_warning); } setcookie ("$this->_name", $cookie _val, $this->_expires, $this->_dir, $this->_site); }}?> Call Example: 1, set 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 is saved in T He 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 is: "echo" Namefirst: = $_cookie[namefirst] "; echo" Namelast: = $_cookie[namelast] "; echo" Number: = $_cookie[number] "; echo" Time: = $_cookie[time] "; echo"
END ";? >
2, get cookies
Show some of the cookie values used for testing "echo" Name: "Echo $_cookie[' Namefirst '); echo "", Echo $_cookie[' Namelast '];echo " Number: "Echo $_cookie[' number '];echo" Time: "Echo $_cookie[' time '];echo"
END ";? >
3. Delete Cookies
Set ();//Clear All values# $PHP _cookie->clear ();? >
|