ZF3会话超时问题 [英] ZF3 session timeout issue

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

问题描述

我一直在遇到与使用Zend Framework 3的会话超时有关的问题.会话在5到10分钟内过期.我使用了会话的默认代码,如下所示,Zf3框架在 global.php 中提供了该代码.

I have been facing issue related to Session timeout using Zend Framework 3. Session expired within 5-10 min. I had used the default code for the session, which Zf3 skeleton provides in global.php as below.

// Session configuration.
'session_config' => [   
  'cookie_lifetime' => 60*60*1, // Session cookie will expire in 1 hour.
  'gc_maxlifetime' => 60*60*1,  // Store session data on server maximum for 1 hour. 
],

// Session manager configuration. 
'session_manager' => 
[
   'validators' => [
      RemoteAddr::class,
      HttpUserAgent::class,
    ]
],

// Session storage configuration.
'session_storage' => [
   'type' => SessionArrayStorage::class 
],

使用上述代码后,会话仍在5-10分钟内过期.我希望会话过期时间超过30分钟.如何在Zf3中进行配置.

After using above code still session expired within 5-10 minutes.I want session expired time more than 30 minutes.How to configure it in Zf3.

请提供解决方案.

推荐答案

您为会话管理器设置了正确的设置,但这不足以将这些会话设置用作默认设置.

You have the correct settings for the session manager, but this is not enough for these session settings to be used as the default one.

我的假设是您不会将此会话管理器设为默认会话管理器.为了实现它,您需要尽早实例化它. 一种解决方案是在 Module.php

My assumption is that you do not make this session manager your default one. In order to make it, you need to instantiate it as early as possible. One solution would be to do this is in module Module.php

use Zend\Mvc\MvcEvent;
use Zend\Session\SessionManager;

class Module
{
    //...

    /**
     * This method is called once the MVC bootstrapping is complete. 
     */
    public function onBootstrap(MvcEvent $event)
    {
        $application = $event->getApplication();
        $serviceManager = $application->getServiceManager();

        // The following line instantiates the SessionManager and automatically
        // makes the SessionManager the 'default' one.
        $sessionManager = $serviceManager->get(SessionManager::class);
    }
}

参考

我的第二个假设是您对会话使用全局路径(例如/var/lib/php/sessions).

My 2nd assumption is that you use the global path for your sessions(eg /var/lib/php/sessions).

在Debian中,有一个cron可以根据您的php.ini会话设置(/etc/cron.d/php)清除会话.

In Debian, there is a cron that may clear sessions according to your php.ini session settings(/etc/cron.d/php).

此cron使用您的php.ini"gc_maxlifetime"并可能清除您的会话.

This cron uses your php.ini "gc_maxlifetime" and probably clears your sessions.

要了解您的会话保存在哪里,请使用 session_save_path() .检查该目录以进行会话.

To find out where your sessions are saved, use session_save_path(). Check that directory for your sessions.

要解决此问题,您应该设置"save_path" ,并且不应将此路径与服务器上的其他应用程序或脚本共享(您不希望其他脚本使用全局gc设置或使用自己的gc设置,删除您的会话).

To overcome this, you should set "save_path" and this path should not be shared with others applications or scripts on your server(you do not want another script using the global gc settings or its own, deleting your sessions).

添加

'save_path'           =>   '/path/to/app/data/sessions'

在"session_config"数组中.

in your 'session_config' array.

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

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