PHP 会话 cookie 在浏览器关闭时过期
PHP session cookies to expire when browser closes
我在 Wordpress 中工作,我的 functions.php 页面中有以下内容。
我有一组 3 种不同的样式。我的 cookie 正在随机化。
if (isset($_COOKIE['style'])){
$style = $_COOKIE['style'];
}
else {
$style = ($rand[$stylearray]);
setcookie('style',$style,time(),COOKIEPATH,COOKIE_DOMAIN,false);
}
我想设置它,以便我的 cookie ONLY 在浏览器关闭时过期。
但是,似乎在页面刷新(F5)时 cookie 过期了。
有没有办法设置它,以便我的 cookie 仅在浏览器关闭时过期?
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
所以
setcookie('style',$style, 0 , ...);
或
setcookie('style',$style, '', ...);
必须工作。
你需要使用session.cookie,问题是你必须修改php.ini,
根据您的虚拟主机配置,您需要创建一个 php.ini/php5.ini/.user.ini 并输入以下内容:
session.cookie_lifetime = 0
0 = 表示直到浏览器关闭。
您无法检测浏览器是否关闭。
我想到的最佳选择是检查 http 请求的 referer 值。
如果 referer 为空则用户直接打开了您的网站(通过浏览器地址字段或使用保存的 favorites-link 等)
如果 referer 与您自己的域不同,则用户来自其他网站,例如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 );
}
但请注意,此方法也不是 100% 可靠的,因为用户可以操纵或禁用 http referer(例如某些浏览器 add-on 或者可能在使用浏览器隐身模式时)
除了难以检测浏览器是否关闭之外,我建议为此使用 PHP sessions。
会话的优点是您可以存储任意多的数据而不会降低网站速度。当您打开您的网站时,您所有的 cookie 都会发送到服务器。因此,如果您在 cookie 中存储了大量数据,那么加载的每个页面都会来回传输大量数据。另一方面,session 将 仅将 ID 值传输到服务器。服务器会将与该ID关联的所有数据存储在服务器上,节省大量传输量。
if ( ! session_id() ) { session_start(); }
// do the referer check here
if ( $recreate_cookie ) {
$_SESSION['style'] = $style;
}
也许添加一个 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;
}
(所有这些代码都未经测试,所以它可能无法工作as-is,但我想你明白了)
我在 Wordpress 中工作,我的 functions.php 页面中有以下内容。
我有一组 3 种不同的样式。我的 cookie 正在随机化。
if (isset($_COOKIE['style'])){
$style = $_COOKIE['style'];
}
else {
$style = ($rand[$stylearray]);
setcookie('style',$style,time(),COOKIEPATH,COOKIE_DOMAIN,false);
}
我想设置它,以便我的 cookie ONLY 在浏览器关闭时过期。 但是,似乎在页面刷新(F5)时 cookie 过期了。
有没有办法设置它,以便我的 cookie 仅在浏览器关闭时过期?
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
所以
setcookie('style',$style, 0 , ...);
或
setcookie('style',$style, '', ...);
必须工作。
你需要使用session.cookie,问题是你必须修改php.ini, 根据您的虚拟主机配置,您需要创建一个 php.ini/php5.ini/.user.ini 并输入以下内容:
session.cookie_lifetime = 0
0 = 表示直到浏览器关闭。
您无法检测浏览器是否关闭。
我想到的最佳选择是检查 http 请求的 referer 值。
如果 referer 为空则用户直接打开了您的网站(通过浏览器地址字段或使用保存的 favorites-link 等) 如果 referer 与您自己的域不同,则用户来自其他网站,例如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 );
}
但请注意,此方法也不是 100% 可靠的,因为用户可以操纵或禁用 http referer(例如某些浏览器 add-on 或者可能在使用浏览器隐身模式时)
除了难以检测浏览器是否关闭之外,我建议为此使用 PHP sessions。
会话的优点是您可以存储任意多的数据而不会降低网站速度。当您打开您的网站时,您所有的 cookie 都会发送到服务器。因此,如果您在 cookie 中存储了大量数据,那么加载的每个页面都会来回传输大量数据。另一方面,session 将 仅将 ID 值传输到服务器。服务器会将与该ID关联的所有数据存储在服务器上,节省大量传输量。
if ( ! session_id() ) { session_start(); }
// do the referer check here
if ( $recreate_cookie ) {
$_SESSION['style'] = $style;
}
也许添加一个 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;
}
(所有这些代码都未经测试,所以它可能无法工作as-is,但我想你明白了)