Created
October 2, 2013 16:17
-
-
Save devyfriend/6796298 to your computer and use it in GitHub Desktop.
Custom CI Session library to detect user_id
this file only extend and modified sess_update and sess_write function note: must add "user_id" field to ci_session table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
| class MY_Session extends CI_Session { | |
| public function __construct($params = array()) | |
| { | |
| parent::__construct($params); | |
| } | |
| function sess_write() | |
| { | |
| // Are we saving custom data to the DB? If not, all we do is update the cookie | |
| if ($this->sess_use_database === FALSE) | |
| { | |
| $this->_set_cookie(); | |
| return; | |
| } | |
| // set the custom userdata, the session data we will set in a second | |
| $custom_userdata = $this->userdata; | |
| $cookie_userdata = array(); | |
| // Before continuing, we need to determine if there is any custom data to deal with. | |
| // Let's determine this by removing the default indexes to see if there's anything left in the array | |
| // and set the session data while we're at it | |
| foreach (array('session_id','ip_address','user_agent','last_activity') as $val) | |
| { | |
| unset($custom_userdata[$val]); | |
| $cookie_userdata[$val] = $this->userdata[$val]; | |
| } | |
| // Did we find any custom data? If not, we turn the empty array into a string | |
| // since there's no reason to serialize and store an empty array in the DB | |
| if (count($custom_userdata) === 0) | |
| { | |
| $custom_userdata = ''; | |
| } | |
| else | |
| { | |
| // Serialize the custom data array so we can store it | |
| $custom_userdata = $this->_serialize($custom_userdata); | |
| } | |
| // Run the update query | |
| $this->CI->db->where('session_id', $this->userdata['session_id']); | |
| // hack to set user id if already login | |
| $me = $this->CI->session->userdata('me'); | |
| $me = (isset($me['id']) && $me['id'] != 0) ? $me['id'] : 0; | |
| $this->CI->db->update($this->sess_table_name, array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata,'user_id' => $me)); | |
| // Write the cookie. Notice that we manually pass the cookie data array to the | |
| // _set_cookie() function. Normally that function will store $this->userdata, but | |
| // in this case that array contains custom data, which we do not want in the cookie. | |
| $this->_set_cookie($cookie_userdata); | |
| } | |
| function sess_update() | |
| { | |
| // We only update the session every five minutes by default | |
| if (($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now) | |
| { | |
| return; | |
| } | |
| // Save the old session id so we know which record to | |
| // update in the database if we need it | |
| $old_sessid = $this->userdata['session_id']; | |
| $new_sessid = ''; | |
| while (strlen($new_sessid) < 32) | |
| { | |
| $new_sessid .= mt_rand(0, mt_getrandmax()); | |
| } | |
| // To make the session ID even more secure we'll combine it with the user's IP | |
| $new_sessid .= $this->CI->input->ip_address(); | |
| // Turn it into a hash | |
| $new_sessid = md5(uniqid($new_sessid, TRUE)); | |
| // Update the session data in the session data array | |
| $this->userdata['session_id'] = $new_sessid; | |
| $this->userdata['last_activity'] = $this->now; | |
| // hack to set user id if already login | |
| $me = $this->CI->session->userdata('me'); | |
| $me = (isset($me['id']) && $me['id'] != 0) ? $me['id'] : 0; | |
| $this->userdata['user_id'] = $me; | |
| // _set_cookie() will handle this for us if we aren't using database sessions | |
| // by pushing all userdata to the cookie. | |
| $cookie_data = NULL; | |
| // Update the session ID and last_activity field in the DB if needed | |
| if ($this->sess_use_database === TRUE) | |
| { | |
| // set cookie explicitly to only have our session data | |
| $cookie_data = array(); | |
| foreach (array('session_id','ip_address','user_agent','last_activity','user_id') as $val) | |
| { | |
| $cookie_data[$val] = $this->userdata[$val]; | |
| } | |
| $this->CI->db->query($this->CI->db->update_string($this->sess_table_name, array('last_activity' => $this->now, 'session_id' => $new_sessid, 'user_id' => $me), array('session_id' => $old_sessid))); | |
| } | |
| // Write the cookie | |
| $this->_set_cookie($cookie_data); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment