-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemp_Session.php
More file actions
64 lines (58 loc) · 1.95 KB
/
Temp_Session.php
File metadata and controls
64 lines (58 loc) · 1.95 KB
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
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* We sub-class CI's Session library here in order to add support for browser-session-expiring userdata
*/
class Temp_Session extends CI_Session
{
var $session_key = null;
//encapsulate token generation
function generate_token(){
$token = md5(uniqid(rand(),true));
return $token;
}
//set userdata that will expire at the end of the current browser session
public function set_temp_userdata($name, $data, $expire = 0){
$this->CI->load->helper('cookie');
if($this->session_key != null){
$key = $this->session_key;
}else{
$key = $this->CI->input->cookie('temp_session_key');
if($key != FALSE){
$this->session_key = $key;
}
}
if($key == FALSE){
//basically, we set another cookie to expire after the browser session ends
//and add this key to the userdata array, which we will use for session validation
$session_key = $this->generate_token();
$data['key'] = $session_key;
$this->session_key = $session_key;
$cookie = array(
'name'=>'temp_session_key',
'value'=>$session_key,
'path'=>'/',
'expire'=>$expire
);
set_cookie($cookie);
}else{
$data['key'] = $key;
}
$this->userdata[$name] = $data;
$this->sess_write();
}
//get temp userdata (to unset, just call regular unset_userdata)
public function temp_userdata($name){
$key = $this->CI->input->cookie('temp_session_key');
if($key && isset($this->userdata[$name]) && isset($this->userdata[$name]['key'])){
if($key == $this->userdata[$name]['key']){
return $this->userdata[$name];
}else{
//session expired, invalidate
$this->unset_userdata($name);
return FALSE;
}
}else{
return FALSE;
}
}
}