webCOMAND

request::set_cookie()

Create or update an HTTP (or HTTPS) cookie.  A cookie may be retrieved in subsequent requests with cookie().

Prototype

string set_cookie(string $key, string $value = '', string $expire = '', string $domain = '', string $path = '/', string $secure = FALSE)

Parameters

  • key - Name of the cookie to set.
  • value - Optional value to set for the cookie.  If not specified, the value will be '' (empty string).
  • expire - Optional number of seconds cookie parameters should be maintained before the cookie expires.  No value or empty string will default to 24 hours.
  • domain - Optional (sub)domain that the cookie will be available to. Setting this to a subdomain (such as 'www.example.com') will make the cookie available to that subdomain and all other sub-domains of it (i.e. w2.www.example.com). To make the cookie available to the whole domain (including all subdomains of it), set the value to '' (empty string) or the domain name ('example.com', in this case).
  • path - Optional path under which the cookie parameter will be made available in subsequent requests. If not specified or set to '/', the cookie will be available within the entire domain. If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain.
  • secure - Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client.  When TRUE, the cookie will only be set if a secure connection exists.

Return

If output exists prior to calling this function, it will fail and return FALSE. If it successfully sends the cookie, it will return TRUE. This does not indicate whether the user accepted the cookie.

Example

$request = new \io_comand_web\request();

// set "session_id" cookie to value 123, which will be
// sent with subsequent HTTP and HTTPS requests to this
// domain and any path for 24 hours.
$success = $request->set_cookie('session_id', 123);

// set "name" cookie to value 'John', which will be
// sent with subsequent HTTPS requests to www.example.com
// subdomain and /user/ path for 1 week (604800 seeconds).
$success = $request->set_cookie('name', 'John', 604800, 'www.example.com', '/user/', TRUE);