【问题标题】:Symfony2 A session had already been started - ignoring session_start()Symfony2 会话已经开始 - 忽略 session_start()
【发布时间】:2012-09-14 19:51:13
【问题描述】:

我无法在本地服务器中使用 Symfony2 会话。我收到“通知:会话已开始 - 忽略 session_start()”错误。

相同的脚本在我的生产服务器中运行良好。

我在 Windows 7 上使用 Xampp 和 PHP 5.3.5。会话 auto_start 在 php.ini 中已关闭。

任何提示都会有所帮助。谢谢

【问题讨论】:

    标签: session symfony symfony-2.1


    【解决方案1】:

    我想有点晚了,但如果有帮助的话:

    确保在 php.ini 中关闭 session.autostart (0)

    在Symfony 2中从控制器使用会话的方式如下:

    $session = $this->getRequest()->getSession();
    
    // store an attribute for reuse during a later user request
    $session->set('foo', 'bar');
    
    // in another controller for another request
    $foo = $session->get('foo');
    
    // use a default value if the key doesn't exist
    $filters = $session->get('filters', array());
    

    http://symfony.com/doc/current/book/controller.html#managing-the-session

    或从视图:

    {{ app.session.get('foo') }}
    

    你也应该调用 start(),即使它在你读/写会话数据时被自动调用(因为它是推荐的,例如 getId() 不调用它)

    $session->start();
    $id = $session->getId();
    

    http://symfony.com/doc/master/components/http_foundation/sessions.html

    您可能在生产服务器上没有收到错误的原因是它的优先级仅为“通知”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      • 2017-05-20
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2019-09-16
      相关资源
      最近更新 更多