PHP Classes

PHP SameSite Cookie Setter: Set cookie values with the SameSite flag enabled

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: 132 This week: 1All time: 9,314 This week: 560Up
Version License PHP version Categories
samesitecookiesetter 1.0GNU General Publi...5HTTP, PHP 5, User Management
Description 

Author

This class can set cookie values with the SameSite flag enabled.

It can take as parameters the name and value for the cookie to be set, as well an array of options that define several optional values. The options can be for setting flags like SameSite, Secure, HttpOnly and Expires.

The class modifies the header created by the PHP setcookie() function sending a HTTP header values compatible to the one that that PHP function sends.

The class also checks if the user browser so it only sends the SameSite and other cookie flags to browsers that support it.

Innovation Award
PHP Programming Innovation award nominee
March 2020
Number 11
SameSite cookies are a means to avoid leaking information about the current user accessing a site in a way that only the site that sets the cookie has access to the cookie values.

PHP 7.3 introduced built-in support to SameSite cookies. This class allows setting same site cookies in a way that works in a compatible way in previous PHP versions.

Manuel Lemos
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: 106 Up4 in Turkey Turkey Up
Innovation award
Innovation award
Nominee: 6x

Example

<?php
require_once 'SameSiteCookieSetter.php';

//set samesite none php cookie
SameSiteCookieSetter::setcookie('samesite_test','testvalue', array('secure' => true, 'samesite' => 'None'));
//SameSiteCookieSetter::setcookie('cookie2','testvalue', array('expires' => time() + 3600, 'httponly' => true, 'secure' => true, 'samesite' => 'None'));

// access cookie value
// echo 'samesite_test:' . $_COOKIE['samesite_test'];


Details

SameSiteCookieSetter

This PHP class enables samesite supported cookies by modifying header created by setcookie() function. As of php version 7.3.0, new signature of setcookie() function exists. To support all php versions, this class use same parameters of new setcookie() signature. The browser agent is also checked against incompatible list of browsers.

Composer

composer require ovunctukenmez/samesite-cookie-setter

  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 SameSiteCookieSetter.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:132
This week:1
All time:9,314
This week:560Up