General Usage¶
Creating a new cookie¶
A cookie can be defined creating a new instance of Comodojo\Cookies\Cookie, or one of the other Cookie types.
Once defined, additional methods can be used to set/get cookie properties, but cookie is just a server-side object.
Nothing is passed to the client until the Cookie::save() method is invoked.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php
use \Comodojo\Cookies\Cookie;
// create a new cookie instance
$cookie = new Cookie('my-cookie');
// set cookie's properties
$cookie->setValue( "Lorem ipsum dolor" )
->setExpire( time()+3600 )
->setPath( "/myapp" )
->setDomain( "example.com" )
->setSecure()
->setHttponly();
// persist the cookie
$result = $cookie->save();
|
Alternatively, the static constructor Cookie::create is available to quikly create a cookie.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php
use \Comodojo\Cookies\Cookie;
// define a new cookie
$cookie = Cookie::create('my_cookie', array(
'value' => "Lorem ipsum dolor"
'expire' => time()+3600
'path' => "/myapp"
'domain' => "example.com"
'secure' => true
'httponly' => true
)
);
// persist the cookie
$result = $cookie->save();
|
Loading a cookie¶
An existent cookie can be easily loaded using the Cookie::load() method.
1 2 3 4 5 6 7 8 9 10 11 12 | <?php
use \Comodojo\Cookies\Cookie;
// create a new cookie instance
$cookie = new Cookie('my-cookie');
// load cookie
$cookie->load();
// read the cookie value
$value = $cookie->getValue();
|
The static constructor Cookie::retrieve can be used to speed up operation.
1 2 3 4 5 6 7 8 9 | <?php
use \Comodojo\Cookies\Cookie;
// create a new cookie instance
$cookie = Cookie::retrieve('my-cookie');
// read the cookie value
$value = $cookie->getValue();
|
Cookie size¶
By default maximum allowed lenght for a cookie is 4000 bytes, to allow it to work in all major browsers.
Note
General informations about cookie limits is available in the rfc6265. See also this site for more informations.
Maximum cookie size can be overridden when creating a cookie.
1 2 3 4 5 6 | <?php
use \Comodojo\Cookies\Cookie;
// create a new cookie instance with m-size of approximately 3k
$cookie = new Cookie('my-cookie', 3000);
|
Note
The real available size of cookie can <4KB due to serialization and (in case) encryption. In case of cookie > 4KB, a \Comodojo\Exception\CookieException is raised.
Content serialization¶
By default a cookie will serialize/unserialize it’s content prior to be setted/loaded.
This behaviour can be overridden in Cookie::setValue and Cookie::getValue methods.
1 2 3 4 5 6 7 8 9 | <?php
use \Comodojo\Cookies\Cookie;
// create a new cookie instance
$cookie = new Cookie('my-cookie');
// set a plain value (no serialization)
$cookie->setValue("Lorem ipsum dolor", false);
|