Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5
<?php OpenSwoole\Http\Response->cookie(string $key, string $value = '', int $expire = 0, string $path = '/', string $domain = '', bool $secure = false, bool $httponly = false, string $samesite = '', string $priority = '')
The key/name of the cookie
The value/contents of the cookie
The expire time of the cookie, a Unix timestamp in seconds
The path of cookie of which it will be available on, the default means it will be available on the entire domain
The (sub)domain of the cookie which it is valid on
If the cookie is secure, using HTTPS
If the cookie is HTTP only, no JavaScript is allowed to access it
Set the samesite value for the cookie
Set the cookie priority
No value is returned
If setting the header fails, then false
will be returned
Set a HTTP cookie which is added to the response. There is an alias setCookie
as well.
This method closely follows the PHP setcookie
method, so reading that documentation can explain more in detail but you should only use OpenSwoole\Http\Response->cookie
to set cookies.
This method must be called before the
$response->end
method.
<?php
$server->on('Request', function(OpenSwoole\Server\Request $request, OpenSwoole\Server\Response $response)
{
// If you do not set some parameters, their default values will be set
$response->cookie(
$key = 'Cookie-Example',
$value = 'Cookie-Value',
$expire = 0 ,
$path = '/',
$domain = 'Swoole.co.uk',
$secure = false ,
$httponly = true
);
});
urlencode
, if you do not want this behavior, use rawCookie()
instead