简体   繁体   English

CakePHP的验证超时问题

[英]Auth timeout problems with CakePHP

This is really bugging me. 这真是困扰我。 Has been for years. 已经好几年了。 No matter what I do with core.php or php.ini, my logins timeout after about an hour - usually. 无论我用core.php还是php.ini做什么,我的登录超时都会在大约一个小时后-通常是这样。 Some deployments of identical code and configuration timeout after a respectable amount of time. 相同的代码和配置的某些部署会在相当长的时间后超时。

This is what I have at the moment on one site - timed out after about an hour: 这是我目前在一个站点上拥有的内容-大约一个小时后超时:

session.gc_divisor  1000
session.gc_maxlifetime  86400
session.gc_probability  1

Configure::write('Session.timeout', '28800');
Configure::write('Session.checkAgent', false);
Configure::write('Security.level', 'medium');

And another - lasted all night: 另一个-持续了一整夜:

session.gc_divisor  100
session.gc_maxlifetime  14400
session.gc_probability  0

Configure::write('Session.timeout', '315360000');
Configure::write('Session.checkAgent', false);
Configure::write('Security.level', 'medium');

Now, before you get excited and say, "Well, the answer is there in the Session.timeout value", let me tell you that this site usually times out after about twenty minutes! 现在,在您兴奋地说“好吧,Session.timeout值中就有答案”之前,让我告诉您该站点通常在二十分钟后超时!

Somewhere I read that on shared hosting, other applications can reset the session by clearing the php-defined session directory. 我在某处看到在共享主机上,其他应用程序可以通过清除php定义的会话目录来重置会话。 This was alluded to by Rowlf in his answer. Rowlf在他的回答中提到了这一点。

CakePHP offers the option to configure the way sessions are handled. CakePHP提供了配置会话处理方式的选项。 In core.php I changed this to 'cake' (by default it is 'php' ): core.php我将其更改为'cake' (默认为'php' ):

/**
 * The preferred session handling method. Valid values:
 *
 * 'php'            Uses settings defined in your php.ini.
 * 'cake'       Saves session files in CakePHP's /tmp directory.
 * 'database'   Uses CakePHP's database sessions.
 */
Configure::write('Session.save', 'cake');

I also ensured that the session timeout and the corresponding php.ini values are the same: 我还确保了会话超时和相应的php.ini值相同:

/**
 * Session time out time (in seconds).
 * Actual value depends on 'Security.level' setting.
 */
Configure::write('Session.timeout', '86400');

So far, the system hasn't logged out. 到目前为止,系统尚未注销。

I don't think this is a Cake-specific thing; 我不认为这是Cake特有的事情; I've seen it when no frameworks were involved - it's most likely an issue with your PHP config settings. 在没有框架参与的情况下,我已经看到了它-这很可能是您的PHP配置设置存在问题。

Things you should check/do to fix the issue: 您应该检查/执行的操作以解决此问题:

  1. Specify a dedicated path to store sessions in session.save_path if you don't already do so. 指定专用路径以将会话存储在session.save_path如果尚未这样做的话)。 Don't store them in /tmp - some other process may come along and wipe them for you. 不要将它们存储在/ tmp中-可能会执行一些其他过程来为您擦除它们。

  2. Make sure (and I mean really sure) that the value of session.gc_maxlifetime is what you think it is (86400 if you want your logins to time out after 24 hrs of inactivity, etc.). 确保(而且我的意思是真的)确保session.gc_maxlifetime的值就是您认为的值(如果希望不活动24小时后登录超时,则为86400等)。 Same with session.gc_divisor and session.gc_probability . session.gc_divisorsession.gc_probability相同。 Even though the PHP Manual specifies that session settings can be set on any level, depending on the dodginess of your PHP build (they're all slightly buggy in their subtle ways :)) you may find they don't actually take effect unless set in the global php.ini file as opposed to in the code, .htaccess, etc. Just output them in your actual app to be sure they are applied. 即使PHP手册指定可以在任何级别上设置会话设置,但取决于您的PHP构建的可疑程度(它们在微妙的方式上都存在一些问题:))您可能会发现,除非进行设置,否则它们实际上不会生效在全局php.ini文件中,而不是在代码,.htaccess等文件中。只需在您的实际应用中输出它们,以确保它们已被应用。

  3. Also, depending on your environment, check if the PHP CLI build is using the same php.ini file as the default PHP build - if the CLI build is using another config file and you have cron jobs using the CLI build, the cron job scripts could be invoking the session cleanup procedure. 另外,根据您的环境,检查PHP CLI构建是否使用与默认PHP构建相同的php.ini文件-如果CLI构建使用另一个配置文件,并且您具有使用CLI构建的cron作业,则cron作业脚本可能正在调用会话清除过程。

If you have many CakePHP apps on the same server, this can be the cause of you troubles. 如果您在同一服务器上有很多CakePHP应用程序,则可能是造成麻烦的原因。 Don't forget to : 不要忘记:

  1. Prefix each app differently ($prefix on core.php). 给每个应用程序加上不同的前缀(core.php上的$ prefix)。
  2. Change the name of each cookie path : 更改每个cookie路径的名称:

     Configure::write('Session', array( 'defaults' => 'php', 'timeout' => 4320, 'ini' => array( 'session.cookie_path' => '/name_app', // this for each app ))); 

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

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