php实现的Cookies操作类实例

这篇文章主要介绍了php实现的Cookies操作类及其用法实例,包括了常见了保存、读取、更新及清除cookie等操作,在需要进行cookie操作时非常具有实用价值,需要的朋友可以参考下。

本文实例讲述了PHP实现的Cookies操作类及其用法,分享给大家供大家参考。具体分析如下:

一、功能:

1.保存,读取,更新,清除cookies数据。

2.可设置前缀。

3.强制超时控制。

4.cookies数据可以是字符串,数组,对象等。

二、用法:

Cookies.class.php类文件如下:

  1. <?php
  2. /** Cookies class 保存,读取,更新,清除cookies数据。可设置前缀。强制超时。数据可以是字符串,数组,对象等。
  3. * Date: 2013-12-22
  4. * Author: fdipzone
  5. * Ver: 1.0
  6. *
  7. * Func:
  8. * public set 设置cookie
  9. * public get 读取cookie
  10. * public update 更新cookie
  11. * public clear 清除cookie
  12. * public setPrefix 设置前缀
  13. * public setExpire 设置过期时间
  14. * private authcode 加密/解密
  15. * private pack 将数据打包
  16. * private unpack 将数据解包
  17. * private getName 获取cookie name,增加prefix处理
  18. */
  19. class Cookies{ // class start
  20. private $_prefix = ''; // cookie prefix
  21. private $_securekey = 'ekOt4_Ut0f3XE-fJcpBvRFrg506jpcuJeixezgPNyALm'; // encrypt key
  22. private $_expire = 3600; // default expire
  23. /** 初始化
  24. * @param String $prefix cookie prefix
  25. * @param int $expire 过期时间
  26. * @param String $securekey cookie secure key
  27. */
  28. public function __construct($prefix='', $expire=0, $securekey=''){
  29. if(is_string($prefix) && $prefix!=''){
  30. $this->_prefix = $prefix;
  31. }
  32. if(is_numeric($expire) && $expire>0){
  33. $this->_expire = $expire;
  34. }
  35. if(is_string($securekey) && $securekey!=''){
  36. $this->_securekey = $securekey;
  37. }
  38. }
  39. /** 设置cookie
  40. * @param String $name cookie name
  41. * @param mixed $value cookie value 可以是字符串,数组,对象等
  42. * @param int $expire 过期时间
  43. */
  44. public function set($name, $value, $expire=0){
  45. $cookie_name = $this->getName($name);
  46. $cookie_expire = time() + ($expire? $expire : $this->_expire);
  47. $cookie_value = $this->pack($value, $cookie_expire);
  48. $cookie_value = $this->authcode($cookie_value, 'ENCODE', $this->_securekey);
  49. if($cookie_name && $cookie_value && $cookie_expire){
  50. setcookie($cookie_name, $cookie_value, $cookie_expire);
  51. }
  52. }
  53. /** 读取cookie
  54. * @param String $name cookie name
  55. * @return mixed cookie value
  56. */
  57. public function get($name){
  58. $cookie_name = $this->getName($name);
  59. if(isset($_COOKIE[$cookie_name])){
  60. $cookie_value = $this->authcode($_COOKIE[$cookie_name], 'DECODE', $this->_securekey);
  61. $cookie_value = $this->unpack($cookie_value);
  62. return isset($cookie_value[0])? $cookie_value[0] : null;
  63. }else{
  64. return null;
  65. }
  66. }
  67. /** 更新cookie,只更新内容,如需要更新过期时间请使用set方法
  68. * @param String $name cookie name
  69. * @param mixed $value cookie value
  70. * @return boolean
  71. */
  72. public function update($name, $value){
  73. $cookie_name = $this->getName($name);
  74. if(isset($_COOKIE[$cookie_name])){
  75. $old_cookie_value = $this->authcode($_COOKIE[$cookie_name], 'DECODE', $this->_securekey);
  76. $old_cookie_value = $this->unpack($old_cookie_value);
  77. if(isset($old_cookie_value[1]) && $old_cookie_vlaue[1]>0){ // 获取之前的过期时间
  78. $cookie_expire = $old_cookie_value[1];
  79. // 更新数据
  80. $cookie_value = $this->pack($value, $cookie_expire);
  81. $cookie_value = $this->authcode($cookie_value, 'ENCODE', $this->_securekey);
  82. if($cookie_name && $cookie_value && $cookie_expire){
  83. setcookie($cookie_name, $cookie_value, $cookie_expire);
  84. return true;
  85. }
  86. }
  87. }
  88. return false;
  89. }
  90. /** 清除cookie
  91. * @param String $name cookie name
  92. */
  93. public function clear($name){
  94. $cookie_name = $this->getName($name);
  95. setcookie($cookie_name);
  96. }
  97. /** 设置前缀
  98. * @param String $prefix cookie prefix
  99. */
  100. public function setPrefix($prefix){
  101. if(is_string($prefix) && $prefix!=''){
  102. $this->_prefix = $prefix;
  103. }
  104. }
  105. /** 设置过期时间
  106. * @param int $expire cookie expire
  107. */
  108. public function setExpire($expire){
  109. if(is_numeric($expire) && $expire>0){
  110. $this->_expire = $expire;
  111. }
  112. }
  113. /** 获取cookie name
  114. * @param String $name
  115. * @return String
  116. */
  117. private function getName($name){
  118. return $this->_prefix? $this->_prefix.'_'.$name : $name;
  119. }
  120. /** pack
  121. * @param Mixed $data 数据
  122. * @param int $expire 过期时间 用于判断
  123. * @return
  124. */
  125. private function pack($data, $expire){
  126. if($data===''){
  127. return '';
  128. }
  129. $cookie_data = array();
  130. $cookie_data['value'] = $data;
  131. $cookie_data['expire'] = $expire;
  132. return json_encode($cookie_data);
  133. }
  134. /** unpack
  135. * @param Mixed $data 数据
  136. * @return array(数据,过期时间)
  137. */
  138. private function unpack($data){
  139. if($data===''){
  140. return array('', 0);
  141. }
  142. $cookie_data = json_decode($data, true);
  143. if(isset($cookie_data['value']) && isset($cookie_data['expire'])){
  144. if(time()<$cookie_data['expire']){ // 未过期
  145. return array($cookie_data['value'], $cookie_data['expire']);
  146. }
  147. }
  148. return array('', 0);
  149. }
  150. /** 加密/解密数据
  151. * @param String $str 原文或密文
  152. * @param String $operation ENCODE or DECODE
  153. * @return String 根据设置返回明文活密文
  154. */
  155. private function authcode($string, $operation = 'DECODE'){
  156. $ckey_length = 4; // 随机密钥长度 取值 0-32;
  157. $key = $this->_securekey;
  158. $key = md5($key);
  159. $keya = md5(substr($key, 0, 16));
  160. $keyb = md5(substr($key, 16, 16));
  161. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
  162. $cryptkey = $keya.md5($keya.$keyc);
  163. $key_length = strlen($cryptkey);
  164. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', 0).substr(md5($string.$keyb), 0, 16).$string;
  165. $string_length = strlen($string);
  166. $result = '';
  167. $box = range(0, 255);
  168. $rndkey = array();
  169. for($i = 0; $i <= 255; $i++) {
  170. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  171. }
  172. for($j = $i = 0; $i < 256; $i++) {
  173. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  174. $tmp = $box[$i];
  175. $box[$i] = $box[$j];
  176. $box[$j] = $tmp;
  177. }
  178. for($a = $j = $i = 0; $i < $string_length; $i++) {
  179. $a = ($a + 1) % 256;
  180. $j = ($j + $box[$a]) % 256;
  181. $tmp = $box[$a];
  182. $box[$a] = $box[$j];
  183. $box[$j] = $tmp;
  184. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  185. }
  186. if($operation == 'DECODE') {
  187. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
  188. return substr($result, 26);
  189. } else {
  190. return '';
  191. }
  192. } else {
  193. return $keyc.str_replace('=', '', base64_encode($result));
  194. } //www.phpfensi.com
  195. }
  196. } // class end
  197. ?>

demo.php示例程序如下:

  1. <?php
  2. require 'Cookies.class.php';
  3. $type = isset($_GET['type'])? strtolower($_GET['type']) : '';
  4. if(!in_array($type, array('set','get','update','clear'))){
  5. exit('type not exists');
  6. }
  7. $obj = new Cookies('member', 10); // obj
  8. switch($type){
  9. case 'set': // 设置
  10. $data = array(
  11. 'name' => 'fdipzone',
  12. 'gender' => 'male'
  13. );
  14. $obj->set('me', $data, 5);
  15. echo 'set cookies';
  16. break;
  17. case 'get': // 读取
  18. $result = $obj->get('me');
  19. echo '<pre>';
  20. print_r($result);
  21. echo '</pre>';
  22. echo 'get cookies';
  23. break;
  24. case 'update': // 更新
  25. $data = array(
  26. 'name' => 'angelababy',
  27. 'gender' => 'female'
  28. );
  29. $flag = $obj->update('me', $data);
  30. if($flag){
  31. echo 'update cookies success';
  32. }else{
  33. echo 'update cookies false';
  34. }
  35. break;
  36. case 'clear': // 清除
  37. $obj->clear('me');
  38. echo 'clear cookies';
  39. break;
  40. }
  41. ?>