Diego Montero
Diego Montero

Reputation: 281

Symfony2 A session had already been started - ignoring 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.

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

Upvotes: 2

Views: 4979

Answers (1)

cheesemacfly
cheesemacfly

Reputation: 11762

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.

Upvotes: 12

Related Questions