- /** 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
- * Edit: bbs.it-home.org
- * Func:
- * Public set cookie
- * Public get reads cookies.
- * Public update: update cookie
- * Public clear cookie
- * Set the public setPrefix.
- * Public setExpire: Set the Expiration Time
- * Private authcode encryption/decryption
- * Private pack data
- * Private unpack data
- * Private getName: Get the cookie name and add prefix processing.
- */
- Class Cookies {// class start
- Private $ _ prefix = ''; // cookie prefix
- Private $ _ securekey = '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, etc.
- * @ 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-> getName ($ 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. use the set method to update the Expiration Time.
- * @ 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 = $ this-> unpack ($ old_cookie_value );
- If (isset ($ old_cookie_value [1]) & $ old_cookie_vlaue [1]> 0) {// Obtain the Expiration Time
- $ Cookie_expire = $ old_cookie_value [1];
- // Update data
- $ 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 );
- Return true;
- }
- }
- }
- 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 is 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 ']) {// not expired
- 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 based on the settings
- */
- Private function authcode ($ string, $ operation = 'decode '){
- $ Ckey_length = 4; // The random key length ranges from 0 to 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 (1, 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
- ?>
2. demo. 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;
- }
- ?>
Appendix: PHP Cookies Operation Source code |