session_class.php
1.62 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
<?php
/**
* @copyright Copyright(c) 2011 http://www.Tinyhu.com
* @file
* @brief
* @author Tinyhu
* @date 2010-03-25
* @version 0.6
* @note
*/
session_start();
/**
* @brief Session
* @class Session
* @note
*/
class Session
{
//session前缀
private static $per='Tiny_';
private static $safeLave = 0;
//设定session
public static function set($name,$value='',$time='1800',$path='/',$domain=null)
{
if(self::checkSafe()==-1) $_SESSION['safecode']=self::sessionId();
$_SESSION[self::$per.$name]=$value;
}
//取得session
public static function get($name)
{
if(self::checkSafe()==1){
if(isset($_SESSION[self::$per.$name])){
$session = $_SESSION[self::$per.$name];
return $session;
}else{
return null;
}
}
if(self::checkSafe()==0) self::clearAll($name);//Tiny::msg('非法窃取SESSION,系统将终止工作!',0);
else return null;
}
//清空某一个Session
public static function clear($name)
{
unset($_SESSION[self::$per.$name]);
}
//清空所有Session
public static function clearAll()
{
return session_destroy();
}
//Session的安全验证
private static function checkSafe()
{
if(isset($_SESSION['safecode']))
{
if($_SESSION['safecode']==self::sessionId())
{
return 1;
}
else
{
return 0;
}
}
else
{
return -1;
}
}
//得到session安全码
private static function sessionId()
{
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"]);
}
}
?>