Phpcookie Class (set, retrieve, and delete cookie values)

Source: Internet
Author: User
Tags set cookie
Phpcookie Class (set, retrieve, and delete cookie values)
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 ();?>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.