PHP session cookies to expire when browser closes PHP session cookies to expire when browser closes wordpress wordpress

PHP session cookies to expire when browser closes


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.


You need to use session.cookie, problems is you have to modify php.ini,depending on your webhost configuration, you need to create a php.ini/php5.ini/.user.ini and put the following:

 session.cookie_lifetime = 0 

0 = means until the browser is closed.


You cannot detect if the browser was closed or not.

The best option that comes to my mind is to check for the referer value of the http request.

If the referer is empty then the user opened your website directly (either via the browser address field or by using a saved favorites-link, etc)If the referer is different than your own domain then the user came via some other site, e.g. google.

$recreate_cookie = false;$my_domain = '://example.com';$referer = $_SERVER['HTTP_REFERER'];if ( ! $referer ) {   // Website opened directly/via favorites  $recreate_cookie = true; }if ( false === strpos( $referer, $my_domain ) ) {   // User arrived from a link of some other website  $recreate_cookie = true; }if ( $recreate_cookie ) {  // Only name and value are required in your case.  setcookie( 'style', $style );}

But note that this method also is not 100% reliable as the http referer can be manipulated or disabled by the user (e.g. some browser add-on or maybe while using the browsers incognito mode)


Besides the difficulty to detect if the browser was closed I'd suggest to use PHP sessions for this.

Sessions have the advantage that you can store as much data as you want without slowing down the website: When you open your website all your cookies are sent to the server, so if you have lots of data stored in cookies then every page that is loaded transfers a lot of data back and forth. A session on the other hand will only transfer an ID value to the server, and the server will store the all data connected with that ID on the server, saving a lot of transfer volume.

if ( ! session_id() ) { session_start(); }// do the referer check hereif ( $recreate_cookie ) {   $_SESSION['style'] = $style; }  

Maybe it makes sense to add a timer that will not refresh the style for 15 minutes - so when the user closes the browser and opens your page again within 15 minutes he will have the same style as before.

// do the session start and referer check hereif ( $recreate_cookie ) {  $expires = intval( $_SESSION['style_expire'] );  if ( $expires > time() ) { $recreate_cookie = false; }}if ( $recreate_cookie ) {   $_SESSION['style'] = $style;   // Style will not change in the next 15 minutes  $_SESSION['style_expire'] = time() + 15 * 60; }  

(All this code is untested, so it might not work as-is, but I guess you get the idea)