PHP 会话何时结束? [英] When does a PHP session end?

查看:47
本文介绍了PHP 会话何时结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法在互联网上找到明确的答案,所以我在这里问.

I can't seem to find a definitive answer on the internet, so I'm asking here.

当在 .php 脚本中使用 session_start(); 并保存一些值时,会话何时结束?那么什么时候这些值将无法再次访问?

When one uses session_start(); in a .php script and saves some values, when does the session end? So when would those values not be accessible again?

我发现刷新页面或以代码方式停止会话会停止它,并且可能的超时也会停止会话.但是如果离开站点并在一分钟后返回呢?然后关闭浏览器?

I've found that refreshing the page or stopping the session code-wise would stop it, and a possible time-out would stop the session as well. But what about navigating away from the site and returning a minute later? And closing the browser?

至于最后一个,在移动设备上,关闭浏览器"是什么意思?关闭标签甚至最小化网站?

As for the last one, on mobile, what does 'closing the browser' mean? Closing the tab or even minimalising the site?

推荐答案

如果您的会话值未链接到任何 cookie,会话将在 Windows 浏览器关闭时结束.

If your session values are not linked to any cookie, the session will end when the windows browser will be closed.

如果您的会话变量来自 cookie,则会话将在 cookie 文件中指定的时间后结束.

If your session variable comes from a cookie, the session will end after time specified in the cookie file.

在 PHP 中,会话使用 session 类型的 cookie.服务器端,会话信息不断被删除.

In PHP, sessions work with a cookie of type session. Server-side, the session information is constantly deleted.

要在 php 中设置 cookie 的生命周期,可以在 session_start 之前使用函数 session_set_cookie_params:

To set the lifetime of a cookie in php, you can use the function session_set_cookie_params, before the session_start:

session_set_cookie_params(3600,"/");
session_start();

例如,3600 秒是一小时,2 小时是 3600*2 = 7200.

For ex, 3600 seconds is a one hour, for 2 hours 3600*2 = 7200.

但它是一个会话cookie,浏览器可以自己让它过期,如果你想保存更长的会话(比如记住登录),你需要在服务器上保存数据,在客户端保存一个标准的cookie.

But it's a session cookie, the browser can make it expire by himself, if you want to save longer sessions (like remember login), you need save the data in the server and a standard cookie on the client side.

在使用 cookie 时离开网站不会中断会话.

Navigating away from a site when using cookies will not break the session.

这篇关于PHP 会话何时结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆