Home  >  Article  >  Backend Development  >  Codeigniter Session Refactoring_PHP Tutorial

Codeigniter Session Refactoring_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:52:09896browse

In order to facilitate the use of PHP session, I have rewritten a simple session method here.

Create new application/libraries/Sessions.php with the following content:


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
}
Application method:

Sessions::set("username", "admin");

Sessions::get("username");

You can set automatic loading in autoload.php, or manually call Sessions


Author: chory

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478135.htmlTechArticleIn order to facilitate the use of PHP session, I have rewritten a simple session method here. Create a new application/libraries/Sessions.php with the following content: 01 ?php 02 if (!defined(BASEPATH))...
Statement:
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