Swoole\Http\Response->cookie()

Declaration

<?php
Swoole\Http\Response->cookie(string $key, string $value = '', int $expire = 0, string $path = '/', string $domain = '', bool $secure = false, bool $httponly = false, string $samesite = '', string $priority = '')

Parameters

key

The key/name of the cookie

value

The value/contents of the cookie

expire

The expire time of the cookie, a Unix timestamp in seconds

path

The path of cookie of which it will be available on, the default means it will be available on the entire domain

domain

The (sub)domain of the cookie which it is valid on

secure

If the cookie is secure, using HTTPS

httponly

If the cookie is HTTP only, no JavaScript is allowed to access it

samesite

Set the samesite value for the cookie

priority

Set the cookie priority

Return

successful

No value is returned

fails

If setting the header fails, then false will be returned

Description

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 Swoole\Http\Response->cookie to set cookies.

This method must be called before the $response->end method.

Example

<?php

$server->on('Request', function(Swoole/Server/Request $request, Swoole/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
  );
});

Notes

  • Swoole will automatically encode the cookie value with urlencode, if you do not want this behavior, use rawCookie() instead