Welcome Guest, Not a member yet? Register   Sign In
Re-loading library?
#1

[eluser]guidorossi[/eluser]
Hi!

I'm trying to make my own auth library with all the things I want and removing the things I don't want or need...

I based my code taking things from some others libraries like ion and wolf...

I'm having some trouble with the "remember me" option...

Basically I've got on my config.php:
Code:
$config['sess_expire_on_close'] = TRUE;

Then on my auth library I load the session library on the controller, but on my log_in() function of the auth library I want to make the "sess_expire_on_close" FALSE if "rememberme" is TRUE:
Code:
if ($remember == TRUE)
{
   $config['sess_expire_on_close'] = FALSE;
   $this->ci->load->library('session', $config);
   $this->remember_me($user->id);
}

but obviously as the session library was loaded in the controller, seems that I can't change the config...

How can I handle this?
have the session expire on close if the user don't want to be remembered and keep it if the user want to be remembered...

Thanks...
#2

[eluser]InsiteFX[/eluser]
You do not have to do that, all you need to do is create your own remember me cookie when the user comes to your site check and see if they have the remember me cookie if they do read the information from the database for that user and log them in!

If you create your remember me cookie make sure you encode and encrypt it.

InsiteFX
#3

[eluser]guidorossi[/eluser]
Thanks Insite, trat's the way I'm doing it, but I'm having some trouble creating the rememberme cookie...

my code is:
Code:
private function set_remember_me($id)
    {
        $this->ci->load->library('encrypt');

        $token = md5(uniqid(rand(), TRUE));
        $expire = 60*60*24*7; // Set the remember cookie for a week

        $remember_code = $this->ci->encrypt->encode(serialize(array($id, $token, $expire)));

        $cookie = array(
            'name' => 'rememberme',
            'value' => $remember_code,
            'expire' => $expire
        );

        $cookie_db_data = array(
            'id' => $id,
            'remember_code' => $token
        );

        set_cookie($cookie);
        
        $this->ci->auth_model->edit_user($cookie_db_data);
    }

If I set $expire = 60*60*24*7; the rememberme cookie is not even created, and if I set it to $expire = time()+60*60*24*7; it is created but it expire when I close the browser...

Any idea of what's wrong here?
#4

[eluser]InsiteFX[/eluser]
This is whats killing your cookies.
Code:
// or you need to turn it off!
$config['sess_expire_on_close'] = TRUE;
The only way around that is to use the native php cookies. but you have to go through a hoop to get them to save the $value as an array!

Code:
$cookie_data = your_array;

InsiteFX
#5

[eluser]guidorossi[/eluser]
I thought that the cookies created by set_cookie should be independent of the $config...

I've tryied turning expire on close to FALSE but it only affects the ci_session cookie and not the other one...

Anyway, I've solved this issue by using the native PHP setcookie() insted of set_cookie...

Code:
private function set_remember_me($id)
    {
        $this->ci->load->library('encrypt');

        $token = md5(uniqid(rand(), TRUE));
        $expire = time()+60*60*24*7; // Set the remember cookie for a week

        $remember_code = $this->ci->encrypt->encode(serialize(array($id, $token, $expire)));

        $cookie = array(
            'name' => 'rememberme',
            'value' => $remember_code,
            'expire' => $expire
        );

        // For DB insertion
        $cookie_db_data = array(
            'id' => $id,
            'remember_code' => $token
        );
        
        //$this->ci->input->set_cookie($cookie);
        
        setcookie('rememberme', $remember_code, $expire);
        
        $this->ci->auth_model->edit_user($cookie_db_data);
    }




Theme © iAndrew 2016 - Forum software by © MyBB