PHP语言 Cookies 操作类
小标 2018-07-12 来源 : 阅读 949 评论 0

摘要:本文主要向大家介绍了PHP语言 Cookies 操作类,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。

本文主要向大家介绍了PHP语言 Cookies 操作类,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。

Cookies 操作类

功能:

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

2.可设置前缀。

3.强制超时控制。

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

 

Cookies.class.php

[php] view plain copy

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.   

20. class Cookies{ // class start  

21.   

22.     private $_prefix = '';                                                  // cookie prefix  

23.     private $_securekey = 'ekOt4_Ut0f3XE-fJcpBvRFrg506jpcuJeixezgPNyALm';   // encrypt key  

24.     private $_expire = 3600;                                                // default expire  

25.   

26.   

27.     /** 初始化 

28.     * @param String $prefix     cookie prefix 

29.     * @param int    $expire     过期时间 

30.     * @param String $securekey  cookie secure key 

31.     */  

32.     public function __construct($prefix='', $expire=0, $securekey=''){  

33.   

34.         if(is_string($prefix) && $prefix!=''){  

35.             $this->_prefix = $prefix;  

36.         }  

37.   

38.         if(is_numeric($expire) && $expire>0){  

39.             $this->_expire = $expire;  

40.         }  

41.   

42.         if(is_string($securekey) && $securekey!=''){  

43.             $this->_securekey = $securekey;  

44.         }  

45.   

46.     }  

47.   

48.   

49.     /** 设置cookie 

50.     * @param String $name   cookie name 

51.     * @param mixed  $value  cookie value 可以是字符串,数组,对象等 

52.     * @param int    $expire 过期时间 

53.     */  

54.     public function set($name, $value, $expire=0){  

55.   

56.         $cookie_name = $this->getName($name);  

57.         $cookie_expire = time() + ($expire? $expire : $this->_expire);  

58.         $cookie_value = $this->pack($value, $cookie_expire);  

59.         $cookie_value = $this->authcode($cookie_value, 'ENCODE');  

60.   

61.         if($cookie_name && $cookie_value && $cookie_expire){  

62.             setcookie($cookie_name, $cookie_value, $cookie_expire);  

63.         }  

64.   

65.     }  

66.   

67.   

68.     /** 读取cookie 

69.     * @param  String $name   cookie name 

70.     * @return mixed          cookie value 

71.     */  

72.     public function get($name){  

73.   

74.         $cookie_name = $this->getName($name);  

75.   

76.         if(isset($_COOKIE[$cookie_name])){  

77.   

78.             $cookie_value = $this->authcode($_COOKIE[$cookie_name], 'DECODE');  

79.             $cookie_value = $this->unpack($cookie_value);  

80.   

81.             return isset($cookie_value[0])? $cookie_value[0] : null;  

82.   

83.         }else{  

84.             return null;  

85.         }  

86.   

87.     }  

88.   

89.   

90.     /** 更新cookie,只更新内容,如需要更新过期时间请使用set方法 

91.     * @param  String $name   cookie name 

92.     * @param  mixed  $value  cookie value 

93.     * @return boolean 

94.     */  

95.     public function update($name, $value){  

96.   

97.         $cookie_name = $this->getName($name);  

98.   

99.         if(isset($_COOKIE[$cookie_name])){  

100.   

101.             $old_cookie_value = $this->authcode($_COOKIE[$cookie_name], 'DECODE');  

102.             $old_cookie_value = $this->unpack($old_cookie_value);  

103.   

104.             if(isset($old_cookie_value[1]) && $old_cookie_value[1]>0){ // 获取之前的过期时间  

105.   

106.                 $cookie_expire = $old_cookie_value[1];  

107.   

108.                 // 更新数据  

109.                 $cookie_value = $this->pack($value, $cookie_expire);  

110.                 $cookie_value = $this->authcode($cookie_value, 'ENCODE');  

111.   

112.                 if($cookie_name && $cookie_value && $cookie_expire){  

113.                     setcookie($cookie_name, $cookie_value, $cookie_expire);  

114.                     return true;  

115.                 }  

116.   

117.             }  

118.   

119.         }  

120.   

121.         return false;  

122.   

123.     }  

124.   

125.   

126.     /** 清除cookie 

127.     * @param  String $name   cookie name 

128.     */  

129.     public function clear($name){  

130.   

131.         $cookie_name = $this->getName($name);  

132.         setcookie($cookie_name);  

133.   

134.     }  

135.   

136.   

137.     /** 设置前缀 

138.     * @param String $prefix cookie prefix 

139.     */  

140.     public function setPrefix($prefix){  

141.   

142.         if(is_string($prefix) && $prefix!=''){  

143.             $this->_prefix = $prefix;  

144.         }  

145.   

146.     }  

147.   

148.   

149.     /** 设置过期时间 

150.     * @param int $expire cookie expire 

151.     */  

152.     public function setExpire($expire){  

153.   

154.         if(is_numeric($expire) && $expire>0){  

155.             $this->_expire = $expire;  

156.         }  

157.   

158.     }  

159.   

160.   

161.     /** 获取cookie name 

162.     * @param  String $name 

163.     * @return String 

164.     */  

165.     private function getName($name){  

166.         return $this->_prefix? $this->_prefix.'_'.$name : $name;  

167.     }  

168.   

169.   

170.     /** pack 

171.     * @param  Mixed  $data      数据 

172.     * @param  int    $expire    过期时间 用于判断 

173.     * @return 

174.     */  

175.     private function pack($data, $expire){  

176.   

177.         if($data===''){  

178.             return '';  

179.         }  

180.   

181.         $cookie_data = array();  

182.         $cookie_data['value'] = $data;  

183.         $cookie_data['expire'] = $expire;  

184.         return json_encode($cookie_data);  

185.   

186.     }  

187.   

188.   

189.     /** unpack 

190.     * @param  Mixed  $data 数据 

191.     * @return              array(数据,过期时间) 

192.     */  

193.     private function unpack($data){  

194.   

195.         if($data===''){  

196.             return array('', 0);  

197.         }  

198.   

199.         $cookie_data = json_decode($data, true);  

200.   

201.         if(isset($cookie_data['value']) && isset($cookie_data['expire'])){  

202.   

203.             if(time()<$cookie_data['expire']){ // 未过期  

204.                 return array($cookie_data['value'], $cookie_data['expire']);  

205.             }  

206.   

207.         }  

208.   

209.         return array('', 0);  

210.   

211.     }  

212.   

213.     /** 加密/解密数据 

214.     * @param  String $str       原文或密文 

215.     * @param  String $operation ENCODE or DECODE 

216.     * @return String            根据设置返回明文活密文 

217.     */  

218.     private function authcode($string, $operation = 'DECODE'){  

219.   

220.         $ckey_length = 4;   // 随机密钥长度 取值 0-32;  

221.   

222.         $key = $this->_securekey;  

223.   

224.         $key = md5($key);  

225.         $keya = md5(substr($key, 0, 16));  

226.         $keyb = md5(substr($key, 16, 16));  

227.         $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';  

228.   

229.         $cryptkey = $keya.md5($keya.$keyc);  

230.         $key_length = strlen($cryptkey);  

231.   

232.         $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', 0).substr(md5($string.$keyb), 0, 16).$string;  

233.         $string_length = strlen($string);  

234.   

235.         $result = '';  

236.         $box = range(0, 255);  

237.   

238.         $rndkey = array();  

239.         for($i = 0; $i <= 255; $i++) {  

240.             $rndkey[$i] = ord($cryptkey[$i % $key_length]);  

241.         }  

242.   

243.         for($j = $i = 0; $i < 256; $i++) {  

244.             $j = ($j + $box[$i] + $rndkey[$i]) % 256;  

245.             $tmp = $box[$i];  

246.             $box[$i] = $box[$j];  

247.             $box[$j] = $tmp;  

248.         }  

249.   

250.         for($a = $j = $i = 0; $i < $string_length; $i++) {  

251.             $a = ($a + 1) % 256;  

252.             $j = ($j + $box[$a]) % 256;  

253.             $tmp = $box[$a];  

254.             $box[$a] = $box[$j];  

255.             $box[$j] = $tmp;  

256.             $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));  

257.         }  

258.   

259.         if($operation == 'DECODE') {  

260.             if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {  

261.                 return substr($result, 26);  

262.             } else {  

263.                 return '';  

264.             }  

265.         } else {  

266.             return $keyc.str_replace('=', '', base64_encode($result));  

267.         }  

268.   

269.     }  

270.   

271. } // class end  

272.   

273. ?>  


demo.php

[php] view plain copy

1. <?php  

2. require 'Cookies.class.php';  

3.   

4. $type = isset($_GET['type'])? strtolower($_GET['type']) : '';  

5.   

6. if(!in_array($type, array('set','get','update','clear'))){  

7.     exit('type not exists');  

8. }  

9.   

10. $obj = new Cookies('member', 10); // obj  

11.   

12. switch($type){  

13.   

14.     case 'set': // 设置  

15.         $data = array(  

16.             'name' => 'fdipzone',  

17.             'gender' => 'male'  

18.         );  

19.         $obj->set('me', $data, 5);  

20.         echo 'set cookies';  

21.         break;  

22.   

23.     case 'get': // 读取  

24.         $result = $obj->get('me');  

25.   

26.         echo '<pre>';  

27.         print_r($result);  

28.         echo '</pre>';  

29.   

30.         echo 'get cookies';  

31.         break;  

32.   

33.     case 'update': // 更新  

34.         $data = array(  

35.             'name' => 'angelababy',  

36.             'gender' => 'female'  

37.         );  

38.         $flag = $obj->update('me', $data);  

39.   

40.         if($flag){  

41.             echo 'update cookies success';  

42.         }else{  

43.             echo 'update cookies false';  

44.         }  

45.   

46.         break;  

47.   

48.     case 'clear': // 清除  

49.         $obj->clear('me');  

50.         echo 'clear cookies';  

51.         break;  

52. }  

53.   

54. ?>  


以上就介绍了PHP的相关知识,希望对PHP有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言PHP频道!


本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程