• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Database session, remember user, cookies

#8
[eluser]blitzneo[/eluser]
I'd like to 'reactivate' this thread, since I am coding a similar website that will implement the "Remember me" functionality.
As for my previous example I also created a MY_Controller file to check if a user is logged in or not, which is working.
Now, if a user checks the Remember me checkbox, I create a cookie, this way:
Code:
if(isset($_POST['remember']))
{
    // create cookie
    $public_key = md5('test');
    $remember_cookie = array(
    'name'   => 'test',
    'value'  => $public_key,
    'expire' => '1209600',
    'path'   => '/'
);

set_cookie($remember_cookie);

Now I have several questions..
1.- Shall I update or insert a row in the ci_sessions table with the information the user entered ? If so, how to ?
Code:
$insert = array(
    'user_id' => $this->session->userdata('id'),
    'public_key' => $public_key,
    'private_key' => md5('private'),
    'ip_address' => ip2long($_SERVER['REMOTE_ADDR'])
);
$query = $this->db->insert('ci_sessions', $insert);
By doing that, I miss several ci_sessions table rows.. :S This is the table, shall I extend the sessions library or ?
Code:
CREATE TABLE `ci_sessions` (
  `session_id` varchar(40) NOT NULL,
  `user_id` bigint(20) unsigned NOT NULL,
  `public_key` varchar(32) NOT NULL,
  `private_key` varchar(32) NOT NULL,
  `ip_address` varchar(16) NOT NULL,
  `user_agent` varchar(50) NOT NULL,
  `last_activity` int(10) NOT NULL,
  `user_data` text NOT NULL,
  PRIMARY KEY (`session_id`),
  KEY `user_id` (`user_id`,`public_key`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

2.- Once I manage to fill up that info, how to check for that cookie and its values ? Should be prior or after checking if a user is logged in ?
Code:
class Main_controller extends CI_Controller {

    public $data = array();

    public function __construct()
    {
        parent::__construct();
        if ($this->session->userdata('logged_in') === TRUE)
        {
            // ...
        }
        else
        {
            // check cookie
            
            get_cookie('test');
            // ...
            redirect('login');
        }
        
    }

}

3.- After checking that info, I should update that cookie with new data, and update the ci_sessions table with same data as well, right ?

Thanks.


Messages In This Thread
Database session, remember user, cookies - by El Forum - 03-05-2011, 12:06 AM
Database session, remember user, cookies - by El Forum - 03-05-2011, 05:27 AM
Database session, remember user, cookies - by El Forum - 03-05-2011, 05:41 AM
Database session, remember user, cookies - by El Forum - 03-05-2011, 09:21 AM
Database session, remember user, cookies - by El Forum - 03-05-2011, 11:24 AM
Database session, remember user, cookies - by El Forum - 03-05-2011, 11:54 AM
Database session, remember user, cookies - by El Forum - 03-06-2011, 10:54 AM
Database session, remember user, cookies - by El Forum - 07-20-2011, 03:29 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.