CodeIgniter Forums
return cookie value - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: return cookie value (/thread-50723.html)



return cookie value - El Forum - 04-05-2012

[eluser]ibnclaudius[/eluser]
I'm setting the cookie like this:
Code:
$cookie = array(
         'name'   => $this->remember['name'],
         'value'  => $token,
         'expire' => '86500',
         'domain' => '',
         'path'   => '/',
         'prefix' => '',
         'secure' => TRUE
       );

     setcookie($cookie);

And returning it's value like this, but it output nothing:
Code:
get_cookie($this->remember['name'])

What is wrong?


return cookie value - El Forum - 04-05-2012

[eluser]Denzel[/eluser]
Please provide us with a little bit more information. First, what exactly do you mean by "output nothing." That appears to be the expected behavior from a fleeting glance at your code; there are no statements made that output anything.

I don't know what you intended, maybe it was just a typo, but you are calling setcookie, PHP's built-in function, not set_cookie, CodeIgniter's alias for Input:Confusedet_cookie. Maybe that's what you wanted, but we can't tell from what you have provided here.

Provide some more context so we can help you better.


return cookie value - El Forum - 04-06-2012

[eluser]ibnclaudius[/eluser]
Running:
Code:
echo get_cookie($this->remember['name'])
Don't output anything...

I want to set a cookie and check if it exists and returns it value...



return cookie value - El Forum - 04-06-2012

[eluser]ibnclaudius[/eluser]
I'm doing this, if the user login with the remember me checked, create a cookie and insert it in database, the database part is ok, but I don't if the cookie is being set. Confused
Code:
public function login($email, $password, $remember)
{
  $email = trim($email);
  $password = sha1(trim($password));
  
  $user = $this->CI->auth_mod->login($email, $password);
  
  if ($user)
  {
   if ($this->roles['active'] === TRUE && $user['active'] === FALSE)
   {
    echo $this->errors['not_active'];
   }
   else
   {
    $session = array(
      'logged' => TRUE,
      'id' => $user['id'],
      'name' => $user['name'],
      'email' => $user['email'],
      'active' => $user['active']
    );
    
    $this->create_user_session($session);
    
    if ($this->remember['active'] === TRUE && $remember === $this->remember['name'])
    {
     $token = sha1(uniqid(rand(), TRUE));
    
     $this->CI->auth_mod->create_user_session($user['id'], $token);
    
     $cookie = array(
         'name'   => $this->remember['name'],
         'value'  => $token,
         'expire' => '86500',
         'domain' => '',
         'path'   => '/',
         'prefix' => '',
         'secure' => TRUE
       );

     $this->CI->input->set_cookie($cookie);
    }
    
    redirect($this->redirects['logged'], 'location');
   }
  }
  else
  {
   echo $this->errors['invalid_login'];
  }
}

So, when the user logout, delete his session and if there's a cookie, delete it and from the database, but again, everything works, except the cookie part, it's not deleted, also from the database... I don't know if the cookie is being set..
Code:
public function logout()
{
  $this->CI->session->sess_destroy();
  
  $session_token = $this->CI->input->cookie($this->remember['name']);
  
  if ($session_token)
  {
   $this->auth_mod->delete_user_session($session_token);
  }
  
  redirect($this->redirects['logged_out'], 'location');
}

Sorry for the english Confused


return cookie value - El Forum - 04-06-2012

[eluser]InsiteFX[/eluser]
This should be FALSE!
Code:
'secure' => FALSE

Only set to true if your running the server in SSL secure!