Symfony2 会话已经开始 - 忽略 session_start() [英] Symfony2 A session had already been started - ignoring session_start()
问题描述
我无法在本地服务器中使用 Symfony2 会话.我收到通知:会话已经开始 - 忽略 session_start()"错误.
I can't use Symfony2 session in my local server. I'm getting a "Notice: A session had already been started - ignoring session_start()" error.
相同的脚本在我的生产服务器中运行良好.
Same script works fine in my production server.
我在 Windows 7 上使用 Xampp 和 PHP 5.3.5.会话 auto_start 在 php.ini 中关闭.
I'm using Xampp with PHP 5.3.5 over Windows 7. Session auto_start is off in php.ini.
任何提示都会有所帮助.谢谢
Any hint will be helpfull. Thanks
推荐答案
我想这有点晚了,但如果它可以帮助:
I guess it's a bite late but if it can help:
确保您的 php.ini 中的 session.autostart 已关闭 (0)
Make sure your session.autostart is turned off (0) in your php.ini
Symfony 2 中从控制器使用会话的方式如下:
The way to use the session in Symfony 2 from the controler is the following:
$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() 不会调用它)
You should also call start() even if it is automatically called when you read/write session data (because it is recommended and getId() doesn't call it for example)
$session->start();
$id = $session->getId();
http://symfony.com/doc/master/components/http_foundation/session.html
您可能不会在生产服务器上收到错误的原因是它的优先级仅为通知".
The reason you may not get the error on the production server is because it's priority is 'Notice' only.
这篇关于Symfony2 会话已经开始 - 忽略 session_start()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!