首页 > 后端开发 > php教程 > php Cookies操作类(附源码)

php Cookies操作类(附源码)

WBOY
发布: 2016-07-25 08:55:24
原创
1392 人浏览过
  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() return array($cookie_data['value'], $cookie_data['expire']);
  144. }
  145. }
  146. return array('', 0);
  147. }
  148. /** 加密/解密数据
  149. * @param String $str 原文或密文
  150. * @param String $operation ENCODE or DECODE
  151. * @return String 根据设置返回明文活密文
  152. */
  153. private function authcode($string, $operation = 'DECODE'){
  154. $ckey_length = 4; // 随机密钥长度 取值 0-32;
  155. $key = $this->_securekey;
  156. $key = md5($key);
  157. $keya = md5(substr($key, 0, 16));
  158. $keyb = md5(substr($key, 16, 16));
  159. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
  160. $cryptkey = $keya.md5($keya.$keyc);
  161. $key_length = strlen($cryptkey);
  162. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', 0).substr(md5($string.$keyb), 0, 16).$string;
  163. $string_length = strlen($string);
  164. $result = '';
  165. $box = range(0, 255);
  166. $rndkey = array();
  167. for($i = 0; $i $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  168. }
  169. for($j = $i = 0; $i $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  170. $tmp = $box[$i];
  171. $box[$i] = $box[$j];
  172. $box[$j] = $tmp;
  173. }
  174. for($a = $j = $i = 0; $i $a = ($a + 1) % 256;
  175. $j = ($j + $box[$a]) % 256;
  176. $tmp = $box[$a];
  177. $box[$a] = $box[$j];
  178. $box[$j] = $tmp;
  179. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  180. }
  181. if($operation == 'DECODE') {
  182. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
  183. return substr($result, 26);
  184. } else {
  185. return '';
  186. }
  187. } else {
  188. return $keyc.str_replace('=', '', base64_encode($result));
  189. }
  190. }
  191. } // class end
  192. ?>
复制代码

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. echo 'get cookies';
  22. break;
  23. case 'update': // 更新
  24. $data = array(
  25. 'name' => 'angelababy',
  26. 'gender' => 'female'
  27. );
  28. $flag = $obj->update('me', $data);
  29. if($flag){
  30. echo 'update cookies success';
  31. }else{
  32. echo 'update cookies false';
  33. }
  34. break;
  35. case 'clear': // 清除
  36. $obj->clear('me');
  37. echo 'clear cookies';
  38. break;
  39. }
  40. ?>
复制代码

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



来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板