Securing the phpsessid Cookie

Whilst reviewing a website using one of the online services I found a reference to the phpsessid and how it ought to be protected.

You have a cookie that is http only, not https. The cookie name is “PHPSESSID”. Worth reporting to your website company and asking them to verify if action is required to resolve or if this is by design

The setting can be done via the php.ini file or the .htaccess file.

Note to make use of this it will need to be read early, before the cookie is actioned.

To check the status of the cookie I used the element inspector, selecting storage tab. From the list of item types I selected Cookies.

Click on the PHPSESSID item to see the details, for example:

PHPSESSID: "jsfkj4r89vsfsfsfs3253234fujvkaf
  Creation Time: "Thu, 24 Jan 2019 09:26:57 GMT"
  Domain: "www.example.com"
  Expires: "Session"
  HostOnly: true
  HttpOnly: false
  LastAccessed: "Thu, 24 Jan 2019 09:26:57 GMT"
  Path: "/"
  Secure: false
  sameSite: "Unset"

The two entries to be amended are

  • HttpOnly
  • Secure

Note: I have found hosting packages where the hosting provider doesn’t read and implement the php.ini file therefore I chose to implement via the .htaccess file.

Here’s the addition:

#Set secure session cookie
Header always edit Set-Cookie (.*) "$1; HttpOnly"
Header always edit Set-Cookie (.*) "$1; Secure"

and the change to the cookie

PHPSESSID: "jsfkj4r89vsfsfsfs3253234fujvkaf
  Creation Time: "Thu, 24 Jan 2019 09:26:57 GMT"
  Domain: "www.example.com"
  Expires: "Session"
  HostOnly: true
  HttpOnly: true
  LastAccessed: "Thu, 24 Jan 2019 09:26:57 GMT"
  Path: "/"
  Secure: true
  sameSite: "Unset"

With the addition of a couple of lines of text within the htaccess file the phpsessid cookie can be secured.