PHP SameSite Session Cookie Starter: Initialize PHP sessions to use same site cookies

Recommend this page to a friend!
  Info   View files Example   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2020-04-02 (6 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 120 This week: 1All time: 9,205 This week: 190Up
Version License PHP version Categories
samesitesessionstart 1.0GNU General Publi...5HTTP
Description Author

This class can initialize PHP sessions to use same site cookies.

It can check if the current user browser supports same site cookies. If so it also checks the PHP version that is currently running to determine if it is PHP 7.3 or later, to enable the support to same site cookies.

If the current PHP version does not support same site cookies, it can modify the value of the PHP session cookie to set the same site flag before the cookie is returned to the user browser.

Picture of Ovunc Tukenmez
  Performance   Level  
Name: Ovunc Tukenmez <contact>
Classes: 14 packages by
Country: Turkey Turkey
Age: 34
All time rank: 73111 in Turkey Turkey
Week rank: 96 Up3 in Turkey Turkey Up
Innovation award
Innovation award
Nominee: 6x

Details

SameSiteSessionStarter

This PHP class enables samesite supported php session by modifying header created by session_start function. The browser agent is also checked against incompatible list of browsers.

USAGE

start samesite none php session

require_once 'SameSiteSessionStarter.php';
SameSiteSessionStarter::session_start();

start samesite strict secure php session

require_once 'SameSiteSessionStarter.php';
SameSiteSessionStarter::$samesite = 'Strict';
SameSiteSessionStarter::$is_secure = true;
SameSiteSessionStarter::session_start();

Composer

composer require ovunctukenmez/samesite-session-starter

NOTES

SameSite=None (default setting) works only with Secure cookies. So please make sure to use https protocol to start session correctly.

If you have previous started session with old settings, you need to clear its cookie first so new cookie can be set with the session_start() function. Here is an example to clear previous session cookie programmatically with SameSiteCookieSetter.

                                                           
// https://github.com/ovunctukenmez/SameSiteCookieSetter
require_once 'SameSiteCookieSetter/SameSiteCookieSetter.php';

session_start();

$params = session_get_cookie_params();
$samesite_session_cookie_params = array(
    'samesite' => 'None',
    'secure' => true,
    'expires' => time() - 3600,
    'path' => $params['path'],
    'domain' => $params['domain'],
    'httponly' => isset($params['httponly']) ? $params['httponly'] : false
);

SameSiteCookieSetter::setcookie(session_name(),session_id(),$samesite_session_cookie_params);

To set samesite cookies, you can use SameSiteCookieSetter.

  Files folder image Files  
File Role Description
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file example.php Example Auxiliary script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation
Plain text file SameSiteSessionStarter.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:120
This week:1
All time:9,205
This week:190Up

For more information send a message to info at phpclasses dot org.