CodeIgniter Forums
Problem with cookie - 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: Problem with cookie (/thread-49352.html)



Problem with cookie - El Forum - 02-16-2012

[eluser]Cálcio[/eluser]
Hi guys.

I have used the cookie from CI using the method $this->ci->load->helper('cookie'); and I'm setting the cookie like this:

Code:
set_cookie(array(
            'name' => "autologin",
            'value' => $login,
            'expire' => 5184000,
        ));

So, when I print the value using a simple method in my controller, the data return the correct value, however seems the expire time don't work. I think the value the expire is 60 days, but with a few minutes I lost my session.

Someone have a idea for this behavior.

Tnks.


Problem with cookie - El Forum - 02-16-2012

[eluser]InsiteFX[/eluser]
Why are you using $this->ci ? You only need that if your loading it in a libaray etc not in a controller.



Problem with cookie - El Forum - 02-17-2012

[eluser]Cálcio[/eluser]
So, I have a method in my controller, that verify if the cookie exist.
Something like that:

Code:
/**
     * Check if user logged in. Also test if user is activated or not.
     *
     * @param bool
     * @return bool
     */
    function is_logged_in($activated = TRUE) {
        // Try to autologin
if($this->autologin())
{
  return STATUS_ACTIVATED;
}
else
{
  return $this->ci->session->userdata('status') === ($activated ? STATUS_ACTIVATED : STATUS_NOT_ACTIVATED);
}
    }

This verification return true or false.


Problem with cookie - El Forum - 02-17-2012

[eluser]InsiteFX[/eluser]
If it is in a controller you do not need to use $this->ci
And your code should be like this...
Code:
return ($this->session->userdata('status') === $activated) ? STATUS_ACTIVATED : STATUS_NOT_ACTIVATED;



Problem with cookie - El Forum - 02-17-2012

[eluser]Cálcio[/eluser]
hum ok.
But my CI version is old (1.7.2). Is it a problem? Can I use the same way that you wrote?


Problem with cookie - El Forum - 02-17-2012

[eluser]InsiteFX[/eluser]
It should not be a problem and yes us it the way I showed you...