如何在PHP中更改会话超时? [英] How to change the session timeout in PHP?

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

问题描述

我想延长php中的会话超时时间

I would like to extend the session timeout in php

我知道可以通过修改php.ini文件来实现. 但我无权访问.

I know that it is possible to do so by modifying the php.ini file. But I don't have access to it.

那么有可能仅使用php代码吗?

So is it possible to do it only with php code?

推荐答案

会话超时是一个概念,如果要严格保证,必须在代码中实现;这是唯一的方法,您可以绝对确定X分钟不活动后,任何会话都无法幸存.

Session timeout is a notion that has to be implemented in code if you want strict guarantees; that's the only way you can be absolutely certain that no session ever will survive after X minutes of inactivity.

如果稍微放松一下此要求是可以接受的,并且可以放置下界而不是对持续时间进行严格限制,那么您可以轻松地做到这一点,而无需编写自定义逻辑.

If relaxing this requirement a little is acceptable and you are fine with placing a lower bound instead of a strict limit to the duration, you can do so easily and without writing custom logic.

如果您的会话是使用Cookie(可能是cookie)实现的,并且如果客户端不是恶意的,则可以通过以下方式设置会话持续时间的上限:调整某些参数.如果您使用PHP的默认cookie处理会话,请设置 session.gc_maxlifetime 以及 session_set_cookie_params 应该对您有用:

If your sessions are implemented with cookies (which they probably are), and if the clients are not malicious, you can set an upper bound on the session duration by tweaking certain parameters. If you are using PHP's default session handling with cookies, setting session.gc_maxlifetime along with session_set_cookie_params should work for you like this:

// server should keep session data for AT LEAST 1 hour
ini_set('session.gc_maxlifetime', 3600);

// each client should remember their session id for EXACTLY 1 hour
session_set_cookie_params(3600);

session_start(); // ready to go!

这可以通过配置服务器以使会话数据保持至少一小时的不活动状态,并指示您的客户端在同一时间段后忘记"其会话ID来工作. 这两个步骤都必须达到预期的结果.

This works by configuring the server to keep session data around for at least one hour of inactivity and instructing your clients that they should "forget" their session id after the same time span. Both of these steps are required to achieve the expected result.

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