Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set secure flag on cookies in laravel

Tags:

I want to set secure flag for cookies data when accessing content over HTTPS.

The cookies is used on entire application so need to global configuration to secure all the cookies.

like image 637
Keshav Kothari Avatar asked Nov 15 '17 05:11

Keshav Kothari


People also ask

How do you set the secure flag for these cookies?

Launch Google Chrome and go to either WEB or CAWEB portal website. Press F12 (from Keyboard) to launch Developer Tools. Go to Application tab -> Cookies ( left Panel) and ensure the Secure column was ticked.

How set secure flag on cookies PHP?

Set HttpOnly cookie in PHP The following line sets the HttpOnly flag for session cookies - make sure to call it before you call session_start(): ini_set("session. cookie_httponly", True); This is the most common way to set cookies in PHP, empty variables will hold their default value.

How do you set a secure flag on cookies in Web XML?

To enable Secure flag for JSESSIONID session cookie, you can add attribute secure="true" to the <connector> you use in the web subsystem of your standalone(-*). xml or domain. xml .


2 Answers

You need to override the default setting using session_set_cookie_params, set the $secure flag to true,

void session_set_cookie_params ( int $lifetime [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]] ) 

more info at php.net

In laravel you need to alter the config/session.php configuration,set the secure flag to true

/* |-------------------------------------------------------------------------- | HTTPS Only Cookies |-------------------------------------------------------------------------- | | By setting this option to true, session cookies will only be sent back | to the server if the browser has a HTTPS connection. This will keep | the cookie from being sent to you if it can not be done securely. | */  'secure' => true, 

In newer versions of laravel you can simply set the SESSION_SECURE_COOKIE=true in the .env file

like image 151
madalinivascu Avatar answered Oct 16 '22 04:10

madalinivascu


You can set the value of secure true in your config/session.php file as illustrated below;

'secure' => env( 'SESSION_SECURE_COOKIE', true ), 
like image 41
Moises Serrano Avatar answered Oct 16 '22 03:10

Moises Serrano