n00b question – session start

Notice: This thread is very old.
xatrix
Member | 2
+
0
-

Hi Everybody

I have a simple question. Where and how can i start the session ?
Only in “bootstrap.php” ?

This :

<?php
...
$container->session->start();
?>

works correctly,but when i try start session from HomepagePresenter

<?php
  public function renderDefault()
	{
         $this->session->start();
         $this->session->name = "test";
	}
?>

it's doesn't .
How ,for example i can start the session ,when user is logged in ,when this method doesn't work ?
Sorry ,for question like this .

enumag
Member | 2118
+
0
-

By default session will be started only if it already exists and otherwise on demand (for example when the user is logged in). This will fail if that demand happens after sending headers to the browser, the most common reason is CSRF protection in forms. In that case you have to create the form component in the presenter (usually in the render method) manually.

I think starting the session by yourself in presenter is wrong. Tell us what happens if you don't (exception message + where it was thrown) so that we can suggest a better solution.

Aurielle
Member | 1281
+
0
-

I don't see a problem with starting session on every request, especially when you have forms with CSRF protection in every presenter. I'd say it's much cleaner way than manually creating the form in render method. You can set the following in config.neon, this will ensure that session is started with every request.

nette:
	session:
		autoStart: true

Alternatively, you can put the code similar to your to BasePresenter, like this:

protected function startup()
{
	if (!$this->session->isStarted()) {
		$this->session->start();
	}

	parent::startup();
}
xatrix
Member | 2
+
0
-

Ok ,thanks.
Everything works fine.

There's the reason : autoStart: true

Thx All !