tomkirsch/samesite

CI4 的 SameSite 饼干

1.0.3 2021-01-27 03:43 UTC

This package is auto-updated.

Last update: 2024-09-27 12:07:24 UTC


README

Samesite 仅支持 PHP > 7.3.0!如果你正在运行更早的版本,这个库不会破坏任何东西,但 samesite 不会设置。

对于较旧的 PHP 版本,将其作为 .htaccess 重写中的最后一个项目添加

<IfModule mod_rewrite.c>
	.
	.
	.
	# SameSite cookie - ensure this comes AFTER your rewrites!
	Header always edit Set-Cookie (.*) "$1; SameSite=Lax"
</IfModule>

要控制 samesite 值,你需要在 .htaccess 文件中添加逻辑。

安装

App\Config\Services 中设置响应类

	public static function response(App $config = null, bool $getShared = true){
		if ($getShared) return static::getSharedInstance('response', $config);
		if (!is_object($config)) $config = config(App::class);
		return new \Tomkirsch\Samesite\SamesiteResponse($config);
	}

现在你可以使用 $response->setCookie() 指定不同的设置

$this->response->setCookie('foo', 'bar', 60 * 60 * 24, '/', '', FALSE, FALSE, 'Strict');
or
$this->response->setCookie([
	'name'=>'foo',
	'value'=>'bar',
	'samesite'=>'Strict',
]);

(可选) 如果你想将默认的 samesite 设置为 Lax 以外的值,你可以在 App\Config\App 的 cookie 部分添加此内容

public $cookieSameSite = 'Lax';

还有一个注意事项...不要直接使用 setcookie()!