| This article introduces a powerful php cookie operation class that allows you to set, retrieve, and delete cookies. For more information, see.
Shares a php cookie operation class. you can set, retrieve, and delete cookies.
Code:
_ 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;} // globally removes the 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-> _ expir Es, $ 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 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. obtain the cookie
Show some 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 a cookie
Set (); // Clear all values # $ PHP_COOKIE-> clear ();?> |