Codeigniter Session Reconstruction

Source: Internet
Author: User

To facilitate the use of php sessions, I have rewritten a simple session method here.

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


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

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

Sessions: get ("username ");

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


Author: chory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.