Cookies

[Since 0.5.4]

Introduction

The Cookie Object is a very simple adapter of the HTTP-Cookies.

Note that cookies must be sent before any output from your script (this is a protocol restriction).

Create a Cookie

You may also use the cookie() function to access a cookie value:

$cookie = cookie();

Available Methods

Method Listing

delete()

You may remove a cookie using the delete method:

cookie()->delete($name, $path, $domain);
  • $path: The path on the server in which the cookie will be available on.
    • If 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.
    • The default value is the base directory of the application.
  • $domain: The (sub)domain that the cookie is available to. Setting this (such as 'example.com') will make the cookie available to that domain and all other sub-domains of it (i.e. www.example.com). The default is the current domain.

get()

The get method is used to get a cookie. If the item does not exist in the cache, null will be returned.

$value = cookie()->get('foo');

If you wish, you may pass a second argument to the get method specifying the default value you wish to be returned if the item doesn't exist:

$value = cookie()->get('foo', 'default');

has()

The has method may be used to determine if an cookie exists:

if (cookie()->has('foo')) {
    //
}

set()

The set method may be used to send a cookie to the browser:

$value = cookie()->set('foo', 'bar', 1440);

The third argument is the time the cookie expires. If set to 0, or omitted, the cookie will expire when the browser closes.

Note, this value is stored on the clients computer; do not store sensitive information!

You can add a few more arguments, that are pass to PHP's native setcookie method:

$value = cookie()->set($name, $value, $minutes, $path, $domain, $secure, $httpOnly);
  • $path: The path on the server in which the cookie will be available on.
    • If 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.
    • The default value is the base directory of the application.
  • $domain: The (sub)domain that the cookie is available to. Setting this (such as 'example.com') will make the cookie available to that domain and all other sub-domains of it (i.e. www.example.com). The default is the current domain.
  • $secure: When set to TRUE, the cookie will only be set if a secure connection exists. The default is FALSE.
  • $httpOnly: When TRUE the cookie will be made accessible only through the HTTP protocol. The default is FALSE.

(edit on GitHub)