Cookies operation instance implemented by php

Source: Internet
Author: User
Tags set cookie
This article mainly introduces the Cookies operation class implemented by php and its usage examples, including common operations such as saving, reading, updating, and clearing Cookies, which are of great practical value when cookie operations are required, if you need Cookies, refer to the example in this article to describe the Cookies operation class implemented by PHP and its usage. The specific analysis is as follows:

I. functions:

1. save, read, update, and clear cookies.
2. you can set a prefix.
3. force timeout control.
4. cookies can be strings, arrays, and objects.

II. usage:

Cookies. class. php files are as follows:

<? Php/** Cookies class: save, read, update, and clear cookies. You can set the prefix. Force timeout. Data can be strings, arrays, and objects. * Date: 2013-12-22 * Author: fdipzone * Ver: 1.0 ** Func: * public set cookie * public get read cookie * public update cookie * public clear cookie * public setPrefix set prefix * public setExpire set expiration time * private authcode encryption/decryption * private pack package Data * private unpack data * private getName get cookie name, added prefix processing */class Cookies {// class start private $ _ prefix = ''; // cookie prefix private $ _ sec Urekey = 'ekot4 _ Ut0f3XE-fJcpBvRFrg506jpcuJeixezgPNyALm '; // encrypt key private $ _ expire = 3600; // default expire/** initialization * @ param String $ prefix cookie prefix * @ param int $ expire expiration time * @ param String $ securekey cookie secure key */public function _ construct ($ prefix = '', $ expire = 0, $ securekey = '') {if (is_string ($ prefix) & $ prefix! = '') {$ This-> _ prefix = $ prefix;} if (is_numeric ($ expire) & $ expire> 0) {$ this-> _ expire = $ expire;} if (is_string ($ securekey) & $ securekey! = '') {$ This-> _ securekey = $ securekey ;}} /** set cookie * @ param String $ name cookie name * @ param mixed $ value cookie value can be a String, array, object * @ param int $ expire expiration time */public function set ($ name, $ value, $ expire = 0) {$ cookie_name = $ this-> getName ($ name); $ cookie_expire = time () + ($ expire? $ Expire: $ this-> _ expire); $ cookie_value = $ this-> pack ($ value, $ cookie_expire); $ cookie_value = $ this-> authcode ($ cookie_value, 'encoding', $ this-> _ securekey); if ($ cookie_name & $ cookie_value & $ cookie_expire) {setcookie ($ cookie_name, $ cookie_value, $ cookie_expire );}} /** read cookie * @ param String $ name cookie name * @ return mixed cookie value */public function get ($ name) {$ cookie_name = $ this-> g EtName ($ name); if (isset ($ _ COOKIE [$ cookie_name]) {$ cookie_value = $ this-> authcode ($ _ COOKIE [$ cookie_name], 'Decode', $ this-> _ securekey); $ cookie_value = $ this-> unpack ($ cookie_value); return isset ($ cookie_value [0])? $ Cookie_value [0]: null;} else {return null;}/** UPDATE cookie. only content is updated, to update the expiration time, use the set method * @ param String $ name cookie name * @ param mixed $ value cookie value * @ return boolean */public function update ($ name, $ value) {$ cookie_name = $ this-> getName ($ name); if (isset ($ _ COOKIE [$ cookie_name]) {$ old_cookie_value = $ this-> authcode ($ _ COOKIE [$ cookie_name], 'Decode', $ this-> _ securekey); $ old_cookie_value = $ t His-> unpack ($ old_cookie_value); if (isset ($ old_cookie_value [1]) & $ old_cookie_vlaue [1]> 0) {// Obtain the previous expiration time $ cookie_expire = $ old_cookie_value [1]; // update data $ cookie_value = $ this-> pack ($ value, $ cookie_expire ); $ cookie_value = $ this-> authcode ($ cookie_value, 'enabled', $ this-> _ securekey); if ($ cookie_name & $ cookie_value & $ cookie_expire) {setcookie ($ cookie_name, $ cookie_value, $ cookie_expire); return tru E ;}}return false;}/** clear cookie * @ param String $ name cookie name */public function clear ($ name) {$ cookie_name = $ this-> getName ($ name); setcookie ($ cookie_name );} /** set the prefix * @ param String $ prefix cookie prefix */public function setPrefix ($ prefix) {if (is_string ($ prefix) & $ prefix! = '') {$ This-> _ prefix = $ prefix;}/** set the Expiration Time * @ param int $ expire cookie expire */public function setExpire ($ expire) {if (is_numeric ($ expire) & $ expire> 0) {$ this-> _ expire = $ expire ;}} /** get cookie name * @ param String $ name * @ return String */private function getName ($ name) {return $ this-> _ prefix? $ This-> _ prefix. '_'. $ name: $ name;}/** pack * @ param Mixed $ data * @ param int $ expire expiration time used to determine * @ return */private function pack ($ data, $ expire) {if ($ data = '') {return'';} $ cookie_data = array (); $ cookie_data ['value'] = $ data; $ cookie_data ['expire '] = $ expire; return json_encode ($ cookie_data);}/** unpack * @ param Mixed $ data * @ return array (data, Expiration Time) */private function unpack ($ data ){ If ($ data = '') {return array ('', 0) ;}$ cookie_data = json_decode ($ data, true ); if (isset ($ cookie_data ['value']) & isset ($ cookie_data ['expire ']) {if (time () <$ cookie_data ['expire']) {// return array ($ cookie_data ['value'], $ cookie_data ['expire ']);} return array ('', 0 );} /** encrypt/decrypt data * @ param String $ str original text or ciphertext * @ param String $ operation ENCODE or DECODE * @ return String return the plaintext ciphertext */private Function authcode ($ string, $ operation = 'decode') {$ ckey_length = 4; // random key length value: 0-32; $ key = $ this-> _ securekey; $ key = md5 ($ key); $ keya = md5 (substr ($ key, 0, 16); $ keyb = md5 (substr ($ key, 16, 16); $ keyc = $ ckey_length? ($ Operation = 'decode '? Substr ($ string, 0, $ ckey_length): substr (md5 (microtime (),-$ ckey_length): ''; $ cryptkey = $ keya. md5 ($ keya. $ keyc); $ key_length = strlen ($ cryptkey); $ string = $ operation = 'decode '? Base64_decode (substr ($ string, $ ckey_length): sprintf ('% 010d', 0 ). substr (md5 ($ string. $ keyb), 0, 16 ). $ string; $ string_length = strlen ($ string); $ result = ''; $ box = range (0,255); $ rndkey = array (); for ($ I = 0; $ I <= 255; $ I ++) {$ rndkey [$ I] = ord ($ cryptkey [$ I % $ key_length]);} for ($ j = $ I = 0; I I <256; $ I ++) {$ j = ($ j + $ box [$ I] + $ rndkey [$ I]) % 256; $ tmp = $ box [$ I]; $ box [$ I] = $ box [$ j]; $ box [$ J] = $ tmp;} for ($ a = $ j = $ I = 0; $ I <$ string_length; $ I ++) {$ a = ($ a + 1) % 256; $ j = ($ j + $ box [$ a]) % 256; $ tmp = $ box [$ a]; $ box [$ a] = $ box [$ j]; $ box [$ j] = $ tmp; $ result. = chr (ord ($ string [$ I]) ^ ($ box [($ box [$ a] + $ box [$ j]) % 256]);} if ($ operation = 'decode') {if (substr ($ result, 0, 10) = 0 | substr ($ result, 0, 10) -time ()> 0) & substr ($ result, 10, 16) = substr (md5 (substr ($ result, 26 ). $ keyb), 0, 16) {return substr ($ result, 26) ;}else {return '';}} else {return $ keyc. str_replace ('=', '', base64_encode ($ result) ;}}// class end?>

The demo. php sample program is as follows:

<? Php require 'cookies. class. php'; $ type = isset ($ _ GET ['type'])? Strtolower ($ _ GET ['type']): ''; if (! In_array ($ type, array ('set', 'get', 'update', 'clear') {exit ('Type not exists ');} $ obj = new Cookies ('member', 10); // obj switch ($ type) {case 'set ': // set $ data = array ('name' => 'fdipzone ', 'gender' => 'male'); $ obj-> set ('me', $ data, 5); echo 'set cookies '; break; case 'get': // read $ result = $ obj-> get ('me'); echo'
';     print_r($result);     echo '
'; Echo 'get cookies'; break; case 'update': // update $ data = array ('name' => 'angelababy ', 'gender' => 'female'); $ flag = $ obj-> update ('me', $ data); if ($ flag) {echo 'update cookies success ';} else {echo 'update cookies false';} break; case 'clear': // clear $ obj-> clear ('me '); echo 'clear cookies '; break;}?>

Click here to download the complete instance source code in this article.

I hope this article will help you with PHP programming.

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.