I would like to "identify" anonymous user when they come on a website page to fill up a form. Indeed, for some stats, we need to know how many connections depending on how far the users are filling up the form.
To identify those users I thought about using the key created by Apache to identify a session after a connection.
So I would use the sessionInterface, new in Symfony 3.
I have no problem injecting this object in my Controllers Action. The thing is that no session is started, so no information to get but some general things on the session Object and this is clear :
protected 'started' => boolean false
if I write :
public function indexAction(SessionInterface $session) {
$session->start();
$session_id = $this->get('session')->getId();
....
}
I can get an identification key (which is probably different from Apache session key) but a new session will be created everytime the user press F5.
Maybe the answer is in some configurations.
I would like to get this special key (Apache or Symfony) for each user connecting to the site and the session should remain the same as the one on Apache, being detroyed if the user close the browser or remains inactive for more than ...(cf. apache and php configuration file).
where should I start the session or could it be started automatically when a user connects ?
Thanks for the help.
Note : Nothing to do with the other post. Better read the question before saying it is the same as another one.
NOTE :
What I tried to do after that.
config.yml :
session:
enabled: true
handler_id: session.handler.native_file
save_path: '%kernel.project_dir%/var/sessions/%kernel.environment%'
name: soda
cookie_lifetime: 0
Controller :
public function indexAction(Request $request, SessionInterface $session)
{
$session_id = null;
$cookies = $request->cookies;
if(!$session->isStarted()) {
print("session not started");
} else {
print("session started");
}
if($cookies->has('soda')) {
print("cookie here");
$session_id = $cookies->get('soda');
} else if(!$session->isStarted()) {
print("cookie not here...starting session");
$session->start();
$session_id = $session->getId();
} else {
print("cookie not here");
}
$response = $this->render('@my.twig', array(
'session_id' => $session_id
));
print_r($session_id);
print_r($session->getId());
$response->headers->setCookie(new Cookie('soda', $session_id));
return $response;
}
First time going on the site :
session not started
cookie not here...starting session
9hec8bd0t7qjr29ji6fuf5 / 9hec8bd0t7qjr29ji6fuf5
I press F5 :
session started
cookie here
9hec8bd0t7qjr29ji6fuf5 / m6alskkqmlf8pt6e1vulj3c8o6
So this time the session is started entering the controller but obviously it has been restarted !!! on every request it seems.
I hope this will help people if it happens to them. I found the solution but I still dont know why it works like this :
config.yml:
framework:
session:
enabled: true
handler_id: session.handler.native_file
save_path: '%kernel.project_dir%/var/sessions/%kernel.environment%'
name: myCookie
cookie_lifetime: 0
in the controller :
public function indexAction(Request $request, SessionInterface $session) {
$cookies = $request->cookies;
$session_id = null;
if($cookies->has('myCookie')) {
$session_id = $cookies->get('myCookie');
} else if (!$session->isStarted() {
$session->start();
$session_id = $session->getId();
$session->set('myCokie', new Cookie('myCookie', $session_id);
}
$response = $this->render ......
$response->headers->setCookie(new Cookie('myCookie', $session_id));
return $response;
}