Codeigniter Session Refactoring_PHP Tutorial

WBOY
Release: 2016-07-13 17:52:09
Original
928 people have browsed it

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))...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!