Note
For all Http features, Ubiquity uses technical classes containing static methods. This is a design choice to avoid dependency injection that would degrade performances.
The UCookie class provides additional functionality to more easily manipulate native $_COOKIES php array.
Cookies are part of the HTTP header, so cookie creation must be called before any output is sent to the browser.
use Ubiquity\utils\http\UCookie;
$cookie_name = 'user';
$cookie_value = 'John Doe';
UCookie::set($cookie_name, $cookie_value);//duration : 1 day
Creating a cookie that lasts 5 days:
UCookie::set($cookie_name, $cookie_value,5*60*60*24);
On a particular domain:
UCookie::set($cookie_name, $cookie_value,5*60*60*24,'/admin');
Sending a cookie without urlencoding the cookie value:
UCookie::setRaw($cookie_name, $cookie_value);
Testing the cookie creation:
if(UCookie::setRaw($cookie_name, $cookie_value)){
//cookie created
}
$userName=UCookie::get('user');
if(UCookie::exists('user')){
//do something if cookie user exists
}
If the page cookie does not exist, the default value of 1 is returned:
$page=UCookie::get('page',1);
Deleting the cookie with the name page:
UCookie::delete('page');
Deleting all cookies from the entire domain:
UCookie::deleteAll();
Deleting all cookies from the domain admin:
UCookie::deleteAll('/admin');