0

I'm amateure in symfony. I want use Session_id in my code

when i change php.ini and set session.auto.start variable true i give a symfony error after false this parameter i should use

$session = new Session();

but now i have a new error, when I execute indexAction() :

An exception has been thrown during the rendering of a template ("Notice: A session had already been started - ignoring session_start() in C:\xampp\htdocs\artgirl\app\cache\dev\classes.php line 105") in "DotArtBundle:Basket:index.html.twig".
500 Internal Server Error - Twig_Error_Runtime
1 linked Exception: ErrorException »

BasketController :

class BasketController extends Controller {
    public function getStaticAction(){
        $session = new Session();
        $session->start();

        $em = $this->getDoctrine()->getManager();
        $sql  = "Select ... where basket_id = '".$session->getId()."'";
    }
    //###############################################
    public function indexAction(){
      $user = new User();
      $form = $this->createFormBuilder($user)
                   ->add('username', 'text')
                   ->add('password', 'text')
                   ->add('email', 'text')
                   ->getForm();
    return $this->render('DotArtBundle:Artist:register.html.twig', array('form' => $form->createView(l)));
    }
}

I use getStaticAction() in my base.html.twig

        {% set vPrice = render(controller('DotArtBundle:Basket:getStatic')) %}
3
  • Are you using Symfony 2.1 or higher? Commented Apr 20, 2013 at 12:47
  • Yes Oshawott, i use Symfony2.2.0 Commented Apr 20, 2013 at 12:56
  • This issue came up for me when I was given a new server from IT that had an old version of PHP version 5.3.3. Symfony won't run on PHP that old, so I upgraded to 5.6 and it works fine. Commented Feb 25, 2016 at 15:51

1 Answer 1

2

In version 2.1 they changed how sessions work, instead of auto starting they're started on demand.

As I understand it, Symfony 2 sessions are incompatible with PHP sessions and Symfony replaces a lot of the base PHP session functions, therefore you need to turn off auto starting in php.ini and initialize a session with something like:

use Symfony/Component/HttpFoundation/Session/Session;
$session = new Session();
$session->start();

Then you should be able to do something like this wherever you need the ID:

$session = $this->getRequest()->getSession()->get('id');

Symfony explains their sessions handling over here, might be worth a read.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.