Welcome Guest, Not a member yet? Register   Sign In
Cookies not keeping their values
#1

[eluser]jvicab[/eluser]
Hi everyone:

Problem: I am not getting the values that are suppossed to be stored on cookies.

Notes:

- I am using CI session stored in the DB (settings in config.php):

Code:
$config['sess_cookie_name'] = 'session';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = true;
$config['sess_encrypt_cookie'] = true;
$config['sess_use_database'] = true;
$config['sess_table_name'] = 'sessions';

- I have a commom controller from which all my controllers inherit.

Logic:

I am setting two cookies in my login function (with native setcookie php function) to store email and password values if the checkbox "remember me" in the login form is checked.

Code:
$email = $this->input->post('email');
$password = $this->input->post('usrpass');
if ($this->input->post('RememberMe') !== false) {      
     setcookie('rmemail', $email, time() + 31536000);  // one year expiration
     setcookie('rmpswd', $password, time() + 31536000);
} else {
     //destroy cookies:
     setcookie('rmemail', '', time() - 3600);
     setcookie('rmpswd', '', time() - 3600);
}

- I checked that I am getting values for $email, $password and the if is already entering in its "true" section.

I read those cookies in my common controller but they are always empty.

Code:
$this->RememberedEmail = $this->input->cookie('rmemail');
$this->RememberedPswd = $this->input->cookie('rmpswd');

Any idea why?

Thanks in advance for the help.

#2

[eluser]InsiteFX[/eluser]
1) Check to make sure that your time is correct.

2) Cookie name:
Code:
$config['sess_cookie_name'] = 'unigue';  // do not use session.

3) Each cookie should have it's own name.

4) You do not need two cookies, you can store and array or json in a cookie.
Code:
$data = array(
    'rmemail' => $email,
    'rmpswd'  => $password
);

// Set Cookie
setcookie('rememberme', serialize($data), time() + 31536000);
setcookie('rememberme', json_encode($data), time() + 31536000);

// Retrive Cookie
$data = unserialize($this->input->cookie('rememberme'));
$data = json_decode($this->input->cookie('rememberme'));




Theme © iAndrew 2016 - Forum software by © MyBB