简体   繁体   English

关于PHP会话的几个问题

[英]Few questions about PHP sessions

I have a few a few question about php sessions: 我对php会话有一些疑问:

  1. Since the default value for session.gc_maxlifetime is 24 mins then that means any session file that isn't modified for 24 mins will be deleted and the session will expire automatically. 由于session.gc_maxlifetime的默认值为24分钟,因此这意味着任何在24分钟内未修改的会话文件将被删除,并且该会话将自动过期。

  2. If I use session_destroy() in my code the session will be unset, but the session file itself won't be deleted until 24 mins passes since it was last modified. 如果我在代码中使用session_destroy() ,则将取消设置会话,但是直到上次修改会话文件24分钟后,会话文件本身才会被删除。

  3. The only way to extend the session's life time (more than 24 mins) is to extend session.gc_maxlifetime to a bigger value. 延长会话的生存时间(超过24分钟)的唯一方法是将session.gc_maxlifetime扩展到更大的值。

Are all these correct or did I get something wrong about it? 这些都是正确的还是我做错了什么?

Also if I store my sessions in a database (using session_set_save_handler() ) will all these rules apply to them ? 另外,如果我将会话存储在数据库中(使用session_set_save_handler() ),是否所有这些规则都适用于它们?

  1. Almost. 几乎。 The file (session) will not be deleted immediately, that is determined by session.gc_probability and session.gc_divisor . session.gc_probabilitysession.gc_divisor确定的文件(会话)不会立即被删除。

  2. No, the session will be expired, but the deletion of the session file is determined as stated in previous point 否,会话将过期,但会话文件的删除如前所述已确定

  3. That is correct ordinarily, but if you were to implement your own session handler, you could alter the behavior of session expiration even in such a way that session.gc_maxlifetime is ignored 通常,这是正确的,但是如果您要实现自己的会话处理程序,则即使忽略session.gc_maxlifetime,您也可以更改会话过期的行为

Storing session in db should not alter those rules, but could stretch them a little, if you wanted to. 将会话存储在db中不应更改这些规则,但是可以扩展它们(如果您愿意)。

edit: 编辑:

This is roughly how you can register your own session handler (handler being a class) and then do whatever you want with it 大致来说,这是您可以注册自己的会话处理程序(处理程序为类)然后对其执行任何操作的方式

First, suppose we have a class, that is going to be handling sessions for our application. 首先,假设我们有一个类,该类将为我们的应用程序处理会话。

class MySession {
  function open($save_path, $session_name) {
  }

  function close() {
  }

  function read($id) {
  }

  function write($id, $sess_data) {
  }

  function destroy($id) {
  }

  function gc($maxlifetime) {
  }
}

To register the handler in php, you only need to call session_set_save_handler function, like this in our case: 要在php中注册处理程序,只需要调用session_set_save_handler函数,在我们的例子中是这样的:

// register the session handler
$sess = new MySession();
session_set_save_handler(array($sess, 'open'),
                     array($sess, 'close'),
                     array($sess, 'read'),
                     array($sess, 'write'),
                     array($sess, 'destroy'),
                     array($sess, 'gc'));

Note that there are actually better ways to register the handler itself, you could even do this in the constructor of your class, or in numerous other ways. 请注意,实际上有更好的方法来注册处理程序本身,您甚至可以在类的构造函数中或以许多其他方式来执行此操作。 But I assume that is not the point here. 但是我认为这不是重点。

What is important is the fact that although PHP gives you the needed variables corresponding to standard behavior of it's session management mechanism, you don't have to respect it (not that I would recommend that). 重要的是,尽管PHP为您提供了与会话管理机制的标准行为相对应的所需变量,但您不必尊重它(我不建议这样做)。

To answer a comment below, to ignore the maxlifetime parameter, you ignore that in your gc method and use whatever you deem necessary/right, for example (using db pseudo code): 要回答以下评论,请忽略maxlifetime参数,请在gc方法中忽略该参数,并使用您认为必要/正确的任何内容,例如(使用db伪代码):

function gc($maxlifetime) { 
  $sql = "DELETE * FROM MySession WHERE lastAccess < NOW()-3600";
  // execute the query, say I have PDO instance in $dbh variable
  $dbh->execute($sql);
}

Voila, you just completely circumvented PHP session settings by doing it by yourself. 瞧,您只是自己来完全绕过PHP会话设置。

  1. Correct, session.gc_maxlifetime will delete session file when the session expires 正确, session.gc_maxlifetime将在会话过期时删除会话文件
  2. session_destroy doesn't delete the session file session_destroy不会删除会话文件
  3. yes, this the only way. 是的,这是唯一的方法。 After you can disable the garbage collection playing with the session.gc_divider and make a script to make your own garbage collection, Debian based distro actually does that by default. 在您可以禁用与session.gc_divider一起使用的垃圾收集并创建脚本以创建自己的垃圾收集之后,基于Debian的发行版实际上会默认执行该操作。

Storing the session in some database won't change those rules. 将会话存储在某些数据库中不会更改这些规则。

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

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