BOTJr. BOTJr. - 3 months ago 17
PHP Question

Cookies Getting expired after two three page refreshes

I have a page which remembers an user,if they login the first time and for the subsequent times they don't have to login again and again.

To achieve this i made an authentication class which contains various functions according to my needs.

Howvever, when i look for the cookie in my console , i cannot find the one which i created while the same cookie is seen in the header( white icon present near the hyperlink)

I will provide various screen shots to provide the same.

enter image description here

This one is just after i made a cookie, see, the same cookie value is not present in console but it is present above.

enter image description here

This one shows the expiry time of the cookie which is 30 days from today.It shouldn't expire, but on page refresh , it just vanishes.

enter image description here

It's just after one or two page refreshes,it's clearly visible that the cookie has been expired.

I'm totally baffled by this bizarre behavior.

public final function on_log($user,$user_name){
$token=$this->generate_random_token($user);
$this->store_token_for_user($user,$token);
$cookie = $user . ':' . $token;
$mac = hash_hmac('sha256', $cookie, $this->key);
$cookie .= ':' . $mac.':'.$user_name;
setcookie('_ajh_food_quo', $cookie,time() + (30 *24 * 60 * 60));//user cookie
//$this->logUserIn($user,$user_name);
}


Above function sets the cookie.

Could anyone please explain me, what am i doing wrong here?

If anyone needs something more please ask in the comments,i will update my question accordingly.

Answer

The path for your cookie is /Google.

To make the session available on all the paths of your website add a 4th param to setcookie the / path (root)

So it should be like this: setcookie('_ajh_food_quo', $cookie, time() + (30 *24 * 60 * 60), '/')

This way will be for all paths of the site..