PHP Classes

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    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 161 This week: 2All time: 8,946 This week: 94Up
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: 37
All time rank: 71911 in Turkey Turkey
Week rank: 109 Up4 in Turkey Turkey Up
Innovation award
Innovation award
Nominee: 6x

Example

<?php
require_once 'SameSiteSessionStarter.php';

//start samesite none php session
SameSiteSessionStarter::session_start();

// set session variable as usual
$_SESSION['test'] = '12345';


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:161
This week:2
All time:8,946
This week:94Up