為了方便使用php的session,我在這裡重寫了一個簡單的session方法。
新建application/libraries/Sessions.php,內容如下:
01
<?php
02
if (!defined('BASEPATH')) exit('No direct script access allowed');
03
04
/**
05
* Reconstruct the session class
06
* @author chory
07
* @version 1.0
08
* @copyright 2011/6/12
09
*/
10
class Sessions{
11
private static $instances;
12
private static function instance()
13
{
14
if (empty(self::$instances)){
15
@self::$instances = &load_class('session');
16
}
17
return self::$instances;
18
}
19
public static function set($key, $value = "")
20
{
21
self::instance() -> set_userdata(array($key => $value));
22
}
23
public static function get($key = null)
24
{
25
if ($key)
26
{
27
return self::instance() -> userdata($key);
28
}
29
else
30
{
31
return self::instance() -> all_userdata();
32
}
33
}
34
public static function _unset($key){
35
self::instance() -> unset_userdata($key);
36
}
37
public static function destroy(){
38
self::instance() -> sess_destroy();
39
}
40
}
應用方法:
Sessions::set("username", "admin");
Sessions::get("username");
可以在autoload.php中設置自動加載,或手動調用Sessions