cookie_class.php
3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* Tiny - A PHP Framework For Web Artisans
* @author Tiny <tinylofty@gmail.com>
* @copyright Copyright(c) 2010-2014 http://www.tinyrise.com All rights reserved
* @version 1.0
*/
/**
* Cookie封装类
*
* @author Tiny
* @class Cookie
*/
class Cookie
{
private static $per = 'Tiny_';
private static $safeCode;
private static $safeLave = 0;
/**
* 设置安全码
*
* @access public
* @param string $scode
* @return mixed
*/
public static function setSafeCode($scode='')
{
self::$safeCode = $scode.self::cookieId();
}
/**
* 取得安全码
*
* @access public
* @return mixed
*/
public static function getSafeCode()
{
if(self::$safeCode == '')self::setSafeCode();
return self::$safeCode;
}
/**
* 设置cookie值
*
* @access public
* @param mixed $name
* @param string $value
* @param string $time
* @param string $path
* @param mixed $domain
* @return mixed
*/
public static function set($name,$value='',$time='86400',$path='/',$domain=null)
{
if($time<=0) $time = -3600;
else $time = time() + $time;
setCookie('safecode',self::cookieId(),$time,$path,$domain);
if(is_array($value) || is_object($value)) $value=serialize($value);
$value = Crypt::encode($value,self::getSafeCode());
setCookie(self::$per.$name,$value,$time,$path,$domain);
}
/**
* 取得cookie值
*
* @access public
* @param mixed $name
* @return mixed
*/
public static function get($name)
{
if(self::checkSafe()==1)
{
if(isset($_COOKIE[self::$per.$name]))
{
$cryptCookie = $_COOKIE[self::$per.$name];
$cookie= Crypt::decode($cryptCookie,self::getSafeCode());
$tem = substr($cookie,0,10);
if(preg_match('/^[Oa]:\d+:.*/',$tem)) $cookie = unserialize($cookie);
return $cookie;
}
return null;
}
if(self::checkSafe()==0) self::clear($name);// Tiny::msg('非法窃取COOKIE,系统将终止工作!',0);
else return null;
}
/**
* 清除cookie对应值
*
* @access public
* @param mixed $name
* @return mixed
*/
public static function clear($name)
{
self::set($name,'',0);
}
/**
* 清空所有
*
* @access public
* @return mixed
*/
public static function clearAll()
{
}
/**
* 安全码
*
* @access private
* @return mixed
*/
private static function checkSafe()
{
if(isset($_COOKIE['safecode']))
{
if($_COOKIE['safecode']==self::cookieId())
{
return 1;
}
else
{
return 0;
}
}
else
{
return -1;
}
}
/**
* cookie ID身份认证
*
* @access private
* @return mixed
*/
private static function cookieId()
{
if(self::$safeLave==0)return 1;
if(self::$safeLave==1) return md5(Chips::getIP());
if(self::$safeLave==2) return md5(Chips::getIP().$_SERVER["HTTP_USER_AGENT"]);
}
}
?>