当前位置:Gxlcms > PHP教程 > phpCookies操作类(附源码)

phpCookies操作类(附源码)

时间:2021-07-01 10:21:17 帮助过:21人阅读

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

2,演示示例 demo.php

  1. require 'Cookies.class.php';
  2. $type = isset($_GET['type'])? strtolower($_GET['type']) : '';
  3. if(!in_array($type, array('set','get','update','clear'))){
  4. exit('type not exists');
  5. }
  6. $obj = new Cookies('member', 10); // obj
  7. switch($type){
  8. case 'set': // 设置
  9. $data = array(
  10. 'name' => 'fdipzone',
  11. 'gender' => 'male'
  12. );
  13. $obj->set('me', $data, 5);
  14. echo 'set cookies';
  15. break;
  16. case 'get': // 读取
  17. $result = $obj->get('me');
  18. echo '
    ';  
  19. print_r($result);
  20. echo '
  21. ';
  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. ?>

附,PHP Cookies操作类的源码下载地址

人气教程排行