Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP session cookies to expire when browser closes

I am working in Wordpress and have the following in my functions.php page.

I have an array of 3 different styles. My cookie is doing the randomizing.

if (isset($_COOKIE['style'])){
    $style = $_COOKIE['style'];
}
else {
    $style = ($rand[$stylearray]);
    setcookie('style',$style,time(),COOKIEPATH,COOKIE_DOMAIN,false); 
}

I want to set it so that my cookie ONLY expires when browser closes. However, it seems that on page refresh(F5) the cookie expires.

Is there a way to set it so that my cookie only expires on browser close?

like image 215
Porkchopsandwich Avatar asked May 29 '15 15:05

Porkchopsandwich


1 Answers

The http://www.w3schools.com/php/func_http_setcookie.asp says

Optional. Specifies when the cookie expires. The value: time()+86400*30, 
will set the cookie to expire in 30 days. If this parameter is omitted 
or set to 0, the cookie will expire at the end of the session 
(when the browser closes). Default is 0

So

setcookie('style',$style, 0 , ...); 

or

setcookie('style',$style, '', ...); 

must work.

like image 138
Hovo Avatar answered Sep 18 '22 23:09

Hovo