Welcome Guest, Not a member yet? Register   Sign In
delete_cookie not working
#11

[eluser]TheFuzzy0ne[/eluser]
Try checking for the cookie on the next request instead. I suspect that the cookies have been sent by the browser, and the data still exists server-side even though it won't on the client side. Just a guess, but it makes sense to me.

EDIT: D'oh! I didn't see your edit.
#12

[eluser]Samutz[/eluser]
I did try that. After the first time I ran logout() (using /index.php/user/logout), I would reload the page and it would still show the cookies as being set.

Edit: D'oh! I didn't see your edit about my edit.
#13

[eluser]TheFuzzy0ne[/eluser]
Glad you got it working. Big Grin
#14

[eluser]Jhourlad Estrella[/eluser]
Guys, I'm not sure if I'm exactly right here but I think it's got to do something about how CI's constructs sessions and cookies. Consulting config.php you'll find an entry that says 'cookie_prefix'. I guess simply instructing CI to

Code:
delete_cookie('my_cookie_name')

tells it to just delete a cookie named 'my_cookie_name', which I think is, non-existent because the actual cookie name for the cookie might be 'my_cookie_prefix_my_cookie_name'.

I believe the one below is the more appropriate approach:

Code:
delete_cookie('user', null, null, $this->config->item('cookie_prefix'));

Can anybody please confirm? If my speculation is right, it might help a lot of coders out there on this issue.

Thanks.
#15

[eluser]InsiteFX[/eluser]
The correct way to delete a cookie do not pass a string to expire:
Code:
$cookie = array(
           'name'   => 'autologin',
           'value'  => $hash,
           'expire' => time() - 86400,    // the - 86400 is setting the cookie to expire.
           'domain' => 'mydomain.com.au' ,
           'path'   => '/',
           'prefix' => 'usnap_',
       );

set_cookie($cookie);

delete_cookie("usnap_autologin");

A lot of users make the mistake of passing a string value to expire, which is wrong!
PHP.net setcookie:
Code:
bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )

int $expire = 0
// do not use strings!

And if you take a look at the Input Class set_cookie it is not using a string value!

InsiteFX
#16

[eluser]Jhourlad Estrella[/eluser]
Good one, InsiteFX. I'm sure to check out on that. Would

Code:
delete_cookie('user', null, null, $this->config->item('cookie_prefix'))

work then? I just tried it out just a while ago and I think it's working just fine.
#17

[eluser]InsiteFX[/eluser]
Here is the code from the Input Class.
Code:
if ( ! is_numeric($expire))
{
    $expire = time() - 86500;
}
else
{
    $expire = ($expire > 0) ? time() + $expire : 0;
}

As you can see if it is not numeric it sets $expire to a numeric value.

In all cases to save yourself problems use a numeric value not a string value.

InsiteFX
#18

[eluser]Jhourlad Estrella[/eluser]
I thought setting expiry to zero makes cookie's lifetime co-terminus with the browser?
#19

[eluser]InsiteFX[/eluser]
Did you read the else statement ?
Code:
$expire = ($expire > 0) ? time() + $expire : 0;  // see the 0

The whole point is to use a numeric value not a string value!

InsiteFX
#20

[eluser]Jhourlad Estrella[/eluser]
Yup, I saw that. Thanks for sharing the info.




Theme © iAndrew 2016 - Forum software by © MyBB