Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set secure and httponly attributes on Symfony 4 session

I would like to create a secure PHP session in Symfony 4

I looked for informations in the Symfony docs and API documentation but could not find any reference to my problem.

In plain PHP I used to do the following to create a secure PHP session:

$session_name = 'session';
$secure = true;
$httponly = true;

$cookieParams = session_get_cookie_params();
session_set_cookie_params(
  $cookieParams["lifetime"], 
  $cookieParams["path"], 
  $cookieParams["domain"], 
  $secure, 
  $httponly
);

session_name($session_name);
session_start();

How do you do something similar in Symfony 4 ? the following code does not work:

$session->set('lifetime', 0);
$session->set('secure', true);
$session->set('httponly', true);
like image 572
Sam Avatar asked Oct 25 '25 12:10

Sam


1 Answers

As pointed by sam and with the correct documentation

In the Symfony framework, these options should be set in the framework.yaml

    session:
       handler_id: ~
       cookie_secure: true
       cookie_httponly: true
like image 166
Sam Avatar answered Oct 27 '25 04:10

Sam