-
Notifications
You must be signed in to change notification settings - Fork 0
/
native_session.php
174 lines (149 loc) · 4.36 KB
/
native_session.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* @author: Alamo Pereira Saravali
* @email: [email protected]
* @github: alamops
* @facebook: fb.me/alamosaravali
* @twitter: @alamosaravali
* @instagram: @alamoweb
* @website: alamoweb.com.br
*/
Class Native_Session
{
public $sess_name;
public $sess_expire_time;
public function __construct()
{
// Load CI
$CI =& get_instance();
// Set session name
$this->sess_name = $CI->config->item('sess_cookie_name');
// Set session expire
$this->sess_expire_time = $CI->config->item('sess_expiration');
// Init Session
session_start();
// Verify Session Part 1
if(isset($_SESSION[$this->sess_name]) && $_SESSION[$this->sess_name])
{
if(!$this->verify_expire_time())
$this->destroy();
else
{
// Verify Keep flash
if(isset($_SESSION[$this->sess_name]['keep_flash']) && $_SESSION[$this->sess_name]['keep_flash'])
$_SESSION[$this->sess_name]['keep_flash'] = false;
else if(isset($_SESSION[$this->sess_name]['flashdata']) && $_SESSION[$this->sess_name]['flashdata'])
$this->destroy_flashdata();
}
}
// Verify Session Part 2
if(!isset($_SESSION[$this->sess_name]) || !$_SESSION[$this->sess_name])
{
$_SESSION[$this->sess_name] = array();
// Add expire time
$_SESSION[$this->sess_name]['expire'] = time() + $this->sess_expire_time;
// Flashdata
$_SESSION[$this->sess_name]['flashdata'] = array();
// Keep Flash
$_SESSION[$this->sess_name]['keep_flash'] = false;
}
}
// VERIFY EXPIRE TIME
private function verify_expire_time()
{
if((time() - $_SESSION[$this->sess_name]['expire']) < $this->sess_expire_time)
return true;
else
return false;
}
// SET SESSION
public function set($key, $value = null)
{
if($key)
{
// Verify if is an array
if(is_array($key))
$this->_setarray($key);
else
$_SESSION[$this->sess_name][$key] = $value;
return true;
}
else
return null;
}
// SET SESSION ARRAY
private function _setarray($data)
{
foreach ($data as $key => $value)
$_SESSION[$this->sess_name][$key] = $value;
}
// GET A SESSION INSTANCE
public function get($key)
{
if(isset($_SESSION[$this->sess_name][$key]))
return $_SESSION[$this->sess_name][$key];
else
return null;
}
// GET ALL SESSION
public function all_session()
{
if($this->verify_expire_time())
return $_SESSION[$this->sess_name];
else
{
$this->destroy();
return false;
}
}
// DESTROY SESSION
public function destroy()
{
session_destroy();
}
/*
FLASH DATA
*/
// SET FLASH DATA
public function set_flashdata($key, $value = null)
{
if($key)
{
// Verify if it is an array
if(is_array($key))
$this->_setflashdataarray($key);
else
$_SESSION[$this->sess_name]['flashdata'][$key] = $value;
$this->keep_flashdata();
return true;
}
else
return null;
}
// GET AN FLASH DATA INSTANCE
public function get_flashdata($key)
{
if(isset($_SESSION[$this->sess_name]['flashdata'][$key]))
return $_SESSION[$this->sess_name]['flashdata'][$key];
else
return null;
}
// KEEP FLASH DATA
public function keep_flashdata()
{
$_SESSION[$this->sess_name]['keep_flash'] = true;
}
// SET FLASH DATA ARRAY
private function _setflashdataarray($data)
{
foreach ($data as $key => $value)
$_SESSION[$this->sess_name]['flashdata'][$key] = $value;
}
// DESTROY FLASH DATA
public function destroy_flashdata()
{
$_SESSION[$this->sess_name]['flashdata'] = array();
$_SESSION[$this->sess_name]['keep_flash'] = false;
}
}
/* End of file Native_Session.php */