简体   繁体   English

浏览器关闭时PHP会话cookie将过期

[英]PHP session cookies to expire when browser closes

I am working in Wordpress and have the following in my functions.php page. 我在Wordpress中工作,并在我的functions.php页面中有以下内容。

I have an array of 3 different styles. 我有3种不同风格的阵列。 My cookie is doing the randomizing. 我的cookie正在进行随机化。

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. 我想设置它,以便我的cookie 在浏览器关闭时到期。 However, it seems that on page refresh(F5) the cookie expires. 但是,似乎在页面刷新(F5)上cookie过期。

Is there a way to set it so that my cookie only expires on browser close? 有没有办法设置它,以便我的cookie只在浏览器关闭时到期?

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

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,问题是你必须修改php.ini,根据你的webhost配置,你需要创建一个php.ini / php5.ini / .user.ini并输入以下内容:

 session.cookie_lifetime = 0 

0 = means until the browser is closed. 0 =表示浏览器关闭之前。

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. 我想到的最佳选择是检查http请求的引用值。

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, eg 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 (eg some browser add-on or maybe while using the browsers incognito mode) 但请注意,此方法也不是100%可靠,因为用户可以操纵或禁用http引用程序(例如某些浏览器插件或可能在使用浏览器隐身模式时)


Besides the difficulty to detect if the browser was closed I'd suggest to use PHP sessions for this. 除了检测浏览器是否关闭的困难之外,我建议使用PHP会话。

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. 会话的优势在于您可以根据需要存储尽可能多的数据而不会降低网站速度:当您打开网站时,所有Cookie都会发送到服务器 ,因此如果您有大量数据存储在Cookie中,那么每个加载的页面都会来回传输大量数据。 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. 另一方面,会话只会将ID传输到服务器,服务器会将与该ID相关的所有数据存储在服务器上,从而节省了大量的传输量。

if ( ! session_id() ) { session_start(); }

// do the referer check here

if ( $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. 也许添加一个不会刷新样式15分钟的计时器是有意义的 - 所以当用户关闭浏览器并在15分钟内再次打开页面时,他将拥有与以前相同的样式。

// do the session start and referer check here

if ( $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) (所有这些代码都是未经测试的,所以它可能无法正常工作,但我想你明白了)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM