Part of the EllisLab Network
   
1 of 4
1
NGSession: a combination of CI’s Session in 1.6 and DBSession
Posted: 02 February 2008 11:14 AM   [ Ignore ]  
Grad Student
Rank
Total Posts:  76
Joined  06-17-2007

Hello,

started as a feature request for the new session lib in 1.6 to support storage of userdata and flashdata optional in a database, i have released NGSession bc the current session lib in CI v1.6 still stores any userdata or flash variables in the cookie.

NGSession can be used transparently using cookies or the database. When using a database, only the session_id is stored in the cookie.


Overview
- NGSession is based on a combination of Codeignitors Session.php in version 1.6 and DBSession.
- Fully compatible with Codeignitors Session.php in version 1.54 and 1.6 and DBSession.
- Designed as drop-in replacement for CI Session and/or DBSession.
- Any config option like encryption and any functionallity like flash session variables are fully supported.
- When using a database, only the session_id is stored in a cookie. Any other data is stored in the database.
- When using without a database, all data is stored in a cookie.
- Both modi work fully tansparent.

More details can be found in the wiki: NGSession

The lib should work with CI v1.54 and v1.60.

Any feedback is appreciated.

Wolfgang

P.S. Thank you to the CI / EE Team for providing such a great framework.

Profile
 
 
Posted: 02 February 2008 01:50 PM   [ Ignore ]   [ # 1 ]  
Grad Student
Rank
Total Posts:  56
Joined  08-09-2007

This looks interesting, you should post it in the “Ignited Code” forum.

Also, what are the advantages of your library over the DB_Session library ?

Profile
 
 
Posted: 02 February 2008 02:58 PM   [ Ignore ]   [ # 2 ]  
Research Assistant
RankRankRank
Total Posts:  447
Joined  05-21-2007

i think this is the fact, that you can use it AS cookie session AND database session.

keep me posted with test and bug, i’ll do some test on my side also.

 Signature 

-> None official irc channel [ irc.freenode.net #codeigniter ]

Profile
 
 
Posted: 03 February 2008 06:29 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Rank
Total Posts:  76
Joined  06-17-2007
Alex007 - 02 February 2008 01:50 PM

This looks interesting, you should post it in the “Ignited Code” forum.

I appologize, you are right.

If an Admin is reading this, i kindly ask to move this thread into the correct forum.

Also, what are the advantages of your library over the DB_Session library ?

As sikkle stated: The lib can handle transparently sessions using cookies AND database without changing any code. So you can use one lib regardless wether you want sessions in database or not.
Give it a try: start using cookies and then change the config to use database.
If you had a valid cookie session, a new database session will start and thats it.
(Since it is a drop in replacement you can simply replace the CI session lib, given you have added the required database field ‘session-data’ as you would for DB Session)

Pls let me know if you find any bugs. I use it in my current project (using JS EXT, CI 1.6) and it works flawless for me.

Using NGAuth (not released so far) i also keep the user_groups (memberships/roles of the logged-in user like admin, manager, user etc.) in the session data. That is something i would not want, if the session data were hold in a cookie.

Profile
 
 
Posted: 03 February 2008 06:52 AM   [ Ignore ]   [ # 4 ]  
Grad Student
Rank
Total Posts:  86
Joined  12-28-2005

Hello,

I am note sure how to “use” the custom session data saved in the DB.
What I am looking for is a way to tell if a user is logged in or not.
What I tried before was to simply add a new field (user) into the table
and then create a query checking for the username in the sessions table but this
I was not able to do with the original CI 1.6 session library.

From what I can see now, using the NGSession, the custom data is saved to the DB which is good, but how do I “extract” it to get the functionality that I was looking for (see above).

Saved data.

a:4:{s:2:"id";s:1:"1";s:8:"username";s:6:"hoorl";s:5:"email";s:17:"soolf@gmail.com";s:9:"logged_in";b:1;}

Kind Regards,
Daniel

Profile
 
 
Posted: 03 February 2008 07:04 AM   [ Ignore ]   [ # 5 ]  
Grad Student
Rank
Total Posts:  76
Joined  06-17-2007

You do not have to deal with the database - just get/set sessiondata as you would using CI’s session.
As i said before: The lib works transparent for cookies and database storage.

But here a simple example:

// setting userdata - can be a string, an array...
//
// setter - $mydata is an array
$this->session->set_userdata('user_memberships', $my_data);
// getter $ret is an array
$ret = $this->session->userdata('user_memberships')
//
// same for string / integer
//
$this->session->set_userdata('user_id', $user_id);
$user_id = $this->session->userdata('user_id');

This works when using cookies and when using database.

Really the point is to serialize all userdata (and flashdata) so they can be stored in _one_ database field. This allows to save any user/flashdata without actually having to modify the table structure.

The serialization itself works similar for using cookies and for database.

Profile
 
 
Posted: 03 February 2008 07:26 AM   [ Ignore ]   [ # 6 ]  
Grad Student
Rank
Total Posts:  76
Joined  06-17-2007

To help you, here an example of my login procedure.
The code won’t work by copy / paste but should give you the idea. You will see, that i use the session API rather than direct access to the session table.
Note: The db syntax matches the new CI 1.6 activerecord implementation (so won’t work on CI 1.5x)

/**
     * Validate login using credentials (typically email/password or username/password)
     * On succuess it sets the user_id field in the session userdata and returns the user object
     *
     * @access    public
     * @param    associative array example ('email'=>$email, 'password'=>dohash($password))
     * @return    mixed boolean:false or object with user record
     */
    
function login($where = array())
    
{
        $query
= $this->db->get_where($this->table_user, $where, 1, 0);

        if (
$query->num_rows != 1) return FALSE;

        
$row = $query->row();
        
$this->session->set_userdata('user_id', $row->id);

        return
$row;
    
}

    
/**
     * Get user information of current logged in user or a specific user by id
     *
     * @access    public
     * @param    int user_id, default = current session user_id
     * @return    mixed boolean:false or object with user record
     */
    
function get_user($id = FALSE)
    
{
        
if ($id === FALSE)
        
{
            
if (($id = $this->session->userdata('user_id')) === FALSE)
            
{
                
return FALSE;
            
}
        }

        $where
= array(($this->table_user .'.' .$this->field_user_id) =>$id);
        
$query = $this->db->get_where($this->table_user, $where, 1, 0);

        return (
$query->num_rows() == 1) ? $query->row() : FALSE;
    
}


     
/**
      * Logout current user
      *
      * No parameter. Logout is done by destroying the current user session.
      *
      *
      * @access    public
      * @return    void
      */
     
function logout()
     
{
         $this
->session->sess_destroy();
     
}
Profile
 
 
Posted: 07 February 2008 03:22 PM   [ Ignore ]   [ # 7 ]  
Summer Student
Total Posts:  2
Joined  02-07-2008

Hello.

I get the database error when i try to load the NGSsession library:

An Error Was Encountered

Error Number:

ERROR: syntax error at or near “)” at character 29

SELECT * FROM (“ci_sessions”) WHERE “session_id” = ‘1d4860f4de1d6656fd6cc8227ca40b77’ AND “ip_address” = ‘xxx.xxx.xxx.xxx’ AND “user_agent” = ‘Mozilla/5.0 (X11; U; Linux i686; PL; rv:1.7.12) Ge’


I use postgres database. My ci_sessions table looks like that:

ci_sessions (
  session_id character varying(40) DEFAULT 0 NOT NULL primary key,
  ip_address character varying(16) DEFAULT 0 NOT NULL,
  user_agent character varying(50) NOT NULL,
  last_activity integer DEFAULT 0 NOT NULL,
  session_data text DEFAULT ‘’::text NOT NULL
);

My config.php:

$config[‘sess_cookie_name’]      = ‘ci_session’;
$config[‘sess_expiration’]      = 7200;
$config[‘sess_encrypt_cookie’]  = FALSE;
$config[‘sess_use_database’]  = TRUE;
$config[‘sess_table_name’]      = ‘ci_sessions’;
$config[‘sess_match_ip’]      = TRUE;
$config[‘sess_match_useragent’]  = TRUE;
$config[‘sess_time_to_update’]    = 300;

And my controller:

class Main extends Controller {

  function __construct()
  {
      parent::Controller();
      $this->load->library(‘session’);
  }
}

Is there a problem with database library (sth with postgres)? Or am I doing sth wrong?
I’d appreciate any help…


edit: i’ve found a solution… -> http://codeigniter.com/forums/viewthread/70389/ smile

Profile
 
 
Posted: 08 February 2008 10:14 AM   [ Ignore ]   [ # 8 ]  
Grad Student
Rank
Total Posts:  76
Joined  06-17-2007

Thx for posting the link to the solution to fix the postgre AR driver.

Does the lib work for you?

Profile
 
 
Posted: 09 February 2008 07:56 AM   [ Ignore ]   [ # 9 ]  
Summer Student
Total Posts:  2
Joined  02-07-2008

Works great:)
Thanks for the lib.

Profile
 
 
Posted: 11 February 2008 03:52 PM   [ Ignore ]   [ # 10 ]  
Grad Student
Rank
Total Posts:  56
Joined  01-14-2008

This is my big concern about these session libraries.

Can I store an object in a session and can this object be written to the database?

 Signature 

Let the perfect will of Jehovah God Almighty be done!

Profile
 
 
   
1 of 4
1
 
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 819, on March 11, 2010 11:15 AM
Total Registered Members: 120520 Total Logged-in Users: 42
Total Topics: 126590 Total Anonymous Users: 5
Total Replies: 665501 Total Guests: 378
Total Posts: 792091    
Members ( View Memberlist )
Newest Members:  defrtymusclesseownikaiyuharry295tanyagrey31212sarah123CasERispukgendale