Part of the EllisLab Network
   
 
Session “security_zone”
Posted: 07 July 2008 05:00 PM   [ Ignore ]  
Summer Student
Avatar
Total Posts:  28
Joined  03-27-2008

all for client is very dangerous and some data I want to save in backend:

database table:

create table ci_sessions (
session_id varchar(40) default '0' not null primary key,
ip_address varchar(16) default '0' not null,
user_agent varchar(50) not null,
last_activity integer default 0 not null,
security_zone text default '' not null
);

system/application/library/MY_Session.php:

class MY_Session extends CI_Session {

    
var $security_data = array();
    
    
/**
        Security data write
    */
    
function security_write() {
        
if ($this->use_database)
            
$this->CI->db->update($this->session_table,
                array(
'security_zone' => serialize($this->security_data)),
                array(
'session_id' => $this->userdata['session_id']));
    
}
    
    
/**
        Security data read
    */
    
function security_read() {
        
if ($this->use_database) {
            $this
->CI->db->select('security_zone')->from($this->session_table)->where('session_id',$this->userdata['session_id']);
            
$query = $this->CI->db->get();
            if (
$query->num_rows() && is_array($data = @unserialize($query->row()->security_zone)))
                
$this->security_data = $data;
        
}        
    }

    
/**
        Set security data
    */
    
function set_security_userdata($newdata = array(), $newval = '')
    
{
        
if (is_string($newdata))
            
$newdata = array($newdata => $newval);
    
        if (
count($newdata) > 0)
            foreach (
$newdata as $key => $val)
                
$this->security_data[$key] = $val;

        
$this->sess_write();
    
}
    
    
/**
        Get security data
    */        
    
function security_userdata($item) {
        
return ( ! isset($this->security_data[$item])) ? FALSE : $this->security_data[$item];
    
}
    
    
/**
        Delete a session variable
    */        
    
function unset_security_userdata($newdata = array())
    
{
        
if (is_string($newdata))
            
$newdata = array($newdata => '');
    
        if (
count($newdata) > 0)
            foreach (
$newdata as $key => $val)
                unset(
$this->security_data[$key]);
                
        
$this->sess_write();
    
}

    
//----------------------- Override parent
    
    /**
        Fetch the current session data if it exists
    */
    
function sess_read() {
        $result
= parent::sess_read();
        if (
$result)
            
$this->security_read();
        return
$result;        
    
}
    
    
/**
        Write the session
    */
    
function sess_write() {
        parent
::sess_write();
        
$this->security_write();
    
}
...


in session (or other) class:

...
/**
    Save captcha security code
*/
function setCaptchaKey($key) {
    $this
->set_security_userdata('captcha',$key);        
}
    
/**
    Verify captcha & kill it
*/
function captchaCode() {
    $key
= $this->security_userdata('captcha');
    
$this->unset_security_userdata('captcha');
    return
$key;
}
...

 Signature 

quality JavaScript, PHP5, PostgreSQL
developer & creator http://www.kyky.su
CodeIgniter is my second brain f**k (in good way) - i love it

Profile
 
 
Posted: 10 July 2008 08:48 PM   [ Ignore ]   [ # 1 ]  
Administrator
Avatar
RankRankRankRankRankRank
Total Posts:  6593
Joined  03-23-2006

Looks nice ReGeDa, you should build into a library and post to ignited code.  I’m sure others would find it valuable as well.

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design
BambooInvoice - Open Source, CodeIgniter powered invoicing.

Profile
MSG
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 62600 Total Logged-in Users: 24
Total Topics: 77073 Total Anonymous Users: 0
Total Replies: 416272 Total Guests: 164
Total Posts: 493345    
Members ( View Memberlist )
Newest Members:  eudj1nsehartEasyMLance SloanandrewjhscottShuvopatricovghazalravi_ghostblade_kaqfa