-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSession.php
More file actions
executable file
·131 lines (110 loc) · 2.57 KB
/
Session.php
File metadata and controls
executable file
·131 lines (110 loc) · 2.57 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
<?php
/**
* Provides more efficient session management.
*
*/
class WebLab_Session {
/**
* Flag that remembers if a session has been started.
*
* @var boolean
*/
protected static $_started = false;
/**
* Check whether a key is contained within the current session.
* This will start the session if it was not started before.
*
* @param string $key
* @return boolean
*/
public static function contains( $key ) {
self::start();
return isset( $_SESSION[$key] );
}
/**
* Set the value of a key in the session.
* This will start the session if it was not started before.
*
* @param array|string $key
* @param null|mixed $value
*/
public static function set( $key, $value=null ) {
self::start();
if( !is_array( $key ) ) {
$_SESSION[$key] = $value;
return;
}
foreach( $key as $name => $value )
self::set( $name, $value );
}
/**
* Retrieve the value of a key in the current session.
* This will start the session if it was not started before.
*
* @param array|string $key
* @return array|mixed
*/
public static function get( $key ) {
self::start();
if( !is_array( $key ) )
return self::contains($key) ? $_SESSION[$key] : null;
$result = array();
foreach( $key as $name )
$result[$name] = $_SESSION[$name];
return $result;
}
/**
* Remove a key from the current session.
* This will start the session if it was not started before.
*
* @param string $key
*/
public static function remove( $key ) {
self::start();
if( !is_array( $key ) ) {
unset( $_SESSION[$key] );
return;
}
foreach( $key as $name )
unset( $_SESSION[$name] );
}
/**
* This will start the session if it was not started before.
*
*/
public static function start() {
if( !self::$_started )
session_start();
self::$_started = true;
}
/**
* This will destroy the current session.
* This will start a session before destroying it.
*
*/
public static function destroy() {
self::start();
session_destroy();
}
/**
* This will set the name of the key used in the cookie
*
* @param string $name
* @return string
*/
public static function name( $name=null ) {
if( $name === null )
return session_name();
session_name($name);
}
/**
* Retrieves and sets the session id for current session.
* @param string $id
* @return string
*/
public static function id( $id=null ) {
if( $id === null )
return session_id();
session_id( $id );
}
}