Home > php教程 > php手册 > Codeigniter Session重构

Codeigniter Session重构

WBOY
Release: 2016-06-13 10:50:17
Original
1129 people have browsed it

为了方便使用php的session,我在这里重写了一个简单的session方法。

新建application/libraries/Sessions.php,内容如下:


01
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


作者:chory
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template