Symfony2 A session had already been started - ignoring session_start()

我只是一个虾纸丫 提交于 2019-12-11 01:58:08

问题


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.

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


回答1:


I guess it's a bite late but if it can help:

Make sure your session.autostart is turned off (0) in your php.ini

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

Or from the view:

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

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/sessions.html

The reason you may not get the error on the production server is because it's priority is 'Notice' only.



来源:https://stackoverflow.com/questions/12431067/symfony2-a-session-had-already-been-started-ignoring-session-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!