1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| <?php
abstract class SetCookie
{
/* Configurer "$dossier_eval" avec le nom du dossier du serveur d'évaluation retourné par var_dump(__FILE__) sans indiquer les trois première lettres caractérisant le lecteur.
Ex : si var_dump(__FILE__) = G:\wamp64\www\Site\Classes\SetCookie.php, configurer $dossier_eval = wamp64, ou encore $dossier_eval = wamp64\www
Suppose que le serveur distant est https. Si le serveur distant n'est pas https, il suffit de définir "private static $server_secure = false;" sans rien changer d'autre.
*/
private static $dossier_eval = 'wamp64\www';
private static $server_secure = null;
private static function init (){
if ( self::$server_secure === null ) self::$server_secure = (strpos(__FILE__,self::$dossier_eval) === 3) == false;
}
public static function cookie ($name, $value, $options = [])
{
self::init();
$defaut = ['expires' => 0, 'path' => '/', 'domain' => null, 'secure' => self::$server_secure, 'httponly' => true, 'samesite' => 'Strict'];
$options = array_merge($defaut, $options);
if (PHP_VERSION_ID < 70300) {
// Utilise le bug des versions < 7.3 qui n'échappe pas le ";" pour passer l'option samesite dans l'option domaine
setcookie($name, $value, $options['expires'], $options['path'], $options['domain'].'; samesite='.$options['samesite'].'', $options['secure'], $options['httponly']);
}
else {
setcookie($name, $value, [
'expires' => $options['expires']
,'path' => $options['path']
,'domain' => $options['domain']
,'secure' => $options['secure']
,'httponly' => $options['httponly']
,'samesite' => $options['samesite']
]
);
}
}
public static function sesStart ($secure = null, $samesite = null)
{
ini_set('session.use_strict_mode',true);
if (!session_id())
{
self::init();
$secure = $secure !== null ? $secure : self::$server_secure;
$samesite = $samesite !== null ? $samesite : 'Strict';
if(PHP_VERSION_ID < 70300) {
session_set_cookie_params(null, '/', null.'; samesite='.$samesite.'', $secure, true);
}
else {
session_set_cookie_params([
'path' => '/'
,'domain' => null
,'secure' => $secure
,'httponly' => true
,'samesite' => $samesite
]);
}
session_start();
}
}
}
?> |
Partager