1 | <?php |
||
17 | class Session { |
||
18 | |||
19 | /** |
||
20 | * Start new or resume existing session. |
||
21 | * |
||
22 | * @param string $name [optional] The session name references the name of the |
||
23 | * session, which is used in cookies and URLs (e.g. PHPSESSID). |
||
24 | * It should contain only alphanumeric characters; it should be short |
||
25 | * and descriptive (i.e. for users with enabled cookie warnings). |
||
26 | * If name is specified, the name of the current session is changed |
||
27 | * to its value. |
||
28 | * |
||
29 | * The session name can't consist of digits only, at least one letter |
||
30 | * must be present. Otherwise a new session id is generated every time. |
||
31 | * @param array $iniSettings [optional] List of parameters for the ini_set() function. |
||
32 | * |
||
33 | * @param bool $useCookie [optional] Use cookie or not. All next arguments |
||
34 | * works only if this parameter is TRUE. |
||
35 | * @param int $lifetime [optional] Lifetime of the session cookie, defined in seconds. |
||
36 | * @param string $path [optional] Path on the domain where the cookie will work. |
||
37 | * Use a single slash ('/') for all paths on the domain. |
||
38 | * @param string $domain [optional] Cookie domain, for example 'www.asymptix.com'. |
||
39 | * To make cookies visible on all subdomains then the domain must |
||
40 | * be prefixed with a dot like '.asymptix.com'. |
||
41 | * @param bool $secure [optional] If TRUE cookie will only be sent over secure connections. |
||
42 | * @param bool $httponly [optional] If set to TRUE then PHP will attempt to |
||
43 | * send the httponly flag when setting the session cookie. |
||
44 | * |
||
45 | * @return mixed This function returns TRUE if a session was successfully |
||
46 | * started, otherwise FALSE. If session was resumed returns session |
||
47 | * status: PHP_SESSION_DISABLED if sessions are disabled. |
||
48 | * PHP_SESSION_NONE if sessions are enabled, but none exists. |
||
49 | * PHP_SESSION_ACTIVE if sessions are enabled, and one exists. |
||
50 | */ |
||
51 | public static function start($name = "", array $iniSettings = [], $useCookie = false, |
||
86 | |||
87 | /** |
||
88 | * Start new or resume existing session. |
||
89 | * |
||
90 | * @return mixed This function returns TRUE if a session was successfully |
||
91 | * started, otherwise FALSE. If session was resumed returns session |
||
92 | * status: PHP_SESSION_DISABLED if sessions are disabled. |
||
93 | * PHP_SESSION_NONE if sessions are enabled, but none exists. |
||
94 | * PHP_SESSION_ACTIVE if sessions are enabled, and one exists. |
||
95 | */ |
||
96 | public static function open() { |
||
99 | |||
100 | /** |
||
101 | * Sets session variable value. |
||
102 | * |
||
103 | * @param string $fieldName |
||
104 | * @param mixed $fieldValue |
||
105 | */ |
||
106 | public static function set($fieldName, $fieldValue = true) { |
||
109 | |||
110 | /** |
||
111 | * Save data to the session. |
||
112 | * |
||
113 | * @param array $data |
||
114 | */ |
||
115 | public static function save($data = []) { |
||
120 | |||
121 | /** |
||
122 | * Verify if session variable exists. |
||
123 | * |
||
124 | * @param string $fieldName |
||
125 | * @return bool |
||
126 | */ |
||
127 | public static function exists($fieldName) { |
||
130 | |||
131 | /** |
||
132 | * Returns value of the session variable exists or null otherwise. |
||
133 | * |
||
134 | * @param string $fieldName Variable name. |
||
135 | * @return mixed Value of the session variable or null if varible with this |
||
136 | * name is not exists in teh session. |
||
137 | */ |
||
138 | public static function get($fieldName) { |
||
145 | |||
146 | /** |
||
147 | * Removes varibale from session. |
||
148 | * |
||
149 | * @param string $fieldName Variable name. |
||
150 | * @return bool True if variable removed or false if it doesn't exist. |
||
151 | */ |
||
152 | public static function remove($fieldName) { |
||
161 | |||
162 | /** |
||
163 | * Destroys all data registered to a session. |
||
164 | * |
||
165 | * @return bool True on success or false on failure. |
||
166 | */ |
||
167 | public static function destroy() { |
||
172 | |||
173 | /** |
||
174 | * Destroys all data registered to a session. |
||
175 | * |
||
176 | * @return bool True on success or false on failure. |
||
177 | */ |
||
178 | public static function close() { |
||
181 | |||
182 | } |
||
183 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: