Cookies

The Cookie_Handler class provides methods for setting, getting and deleting cookies. Instead of using the setcookie() function directly you are better off using the Cookie_Handler class.

Setting a cookie

Below is how you would set a cookie

Cookie::set("cookie_name", "cookie_value", 86400); //86400 is life of cookie in seconds
//OR
Cookie::set("cookie_name", "cookie_value", Cookie::one_day());

Fourth and fifth parameter of set() are the path and the domain respectively.

For specifying duration you may use any of the below which returns integer values

  • Cookie::one_day() which returns 86400
  • Cookie::seven_days() which returns 604800
  • Cookie::thirty_days() which returns 2592000
  • Cookie::six_months() which returns 15811200
  • Cookie::one_year() which returns 31536000
  • Cookie::lifetime() which returns -1
  • Cookie::session() which returns null. This cookie will exists as long as browser is not closed

Getting a cookie value

Below is how you would get a cookie value

Cookie::get("cookie_name"); //if cookie_name doesn\t exist then null is returned
//OR
Cookie::get("cookie_name", "default_value"); //if cookie_name doesn't exist then default_value will be returned

Deleting a cookie

Below is how you would delete a cookie.

Cookie::remove("cookie_name");

Second and third param to remove() function are path and domain respectively.