简体   繁体   English

会话在浏览器关闭时销毁

[英]Session destroy on browser close

I have heard many times that a session get destroy as our browser close. 我听过很多次,当我们的浏览器关闭时会话被破坏。

Then how I keep logged in after closing and reopening my browser. 然后我关闭并重新打开浏览器后如何保持登录状态。

Please help 请帮忙

You keep login because your sessions are not destroyed even when the browser is closed. 您保持登录,因为即使浏览器关闭,您的会话也不会被销毁。 Sessions destroying on the closing of the browser is default behaviour but but this does not mean its the only behaviour . 关闭浏览器时破坏的会话是默认行为 ,但这并不意味着它是唯一的行为 You can extend the expiry time of session. 您可以延长会话的到期时间。

This behaviour can be changed in the php.ini file by altering the line:

Keeping a session alive indefinitely 无限期地保持会话活着

 session.cookie_lifetime = 0

So just check when you have set the expiry time for the sessions. 因此,只需检查何时设置会话的到期时间。 Although using cookies will be a good option 虽然使用cookies将是一个不错的选择

Note:- Remember to restart your web server after making this change. 注意: - 请记住在进行此更改后重新启动Web服务器。

You have to use Cookies. 你必须使用Cookies。

You can use the setcookie() function and read the value with the $_COOKIE['cookiename'] variable. 您可以使用setcookie()函数并使用$_COOKIE['cookiename']变量读取值。

使用cookies,预定的到期时间,我喜欢1年

You can use cookies. 你可以使用cookies。 Cookies are data that is stored directly on the HDD so that even if the browser was closed, cookies still can be read if it haven't expired yet. Cookie是直接存储在HDD上的数据,因此即使浏览器已关闭,如果尚未过期,仍可以读取Cookie。

Here is an example of setting up a cookie. 以下是设置cookie的示例。

Paste this code BEFORE the tag. 在标记之前粘贴此代码。

<?php setcookie("$name", "$value", $time); ?>

Where $name is the cookie name, $value is the cookie value and $time is the time when your cookie will be expired. 其中$ name是cookie名称,$ value是cookie值,$ time是cookie过期的时间。 For example $time = time()+86400; 例如$time = time()+86400; will set your cookie to expire after 1 day. 将您的cookie设置为在1天后过期。 The 86400 value is the number of seconds in a day, 60seconds times 60minutes times 24hours, so 60x60x24 = 86400. 86400值是一天中的秒数,60秒是60小时乘以24小时,因此60x60x24 = 86400。

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

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