Cookie Helper¶
The Cookie Helper file contains functions that assist in working with cookies.
Loading this Helper¶
This helper is loaded using the following code:
<?php
helper('cookie');
Available Functions¶
The following functions are available:
set_cookie($name[, $value = ''[, $expire = 0[, $domain = ''[, $path = '/'[, $prefix = ''[, $secure = false[, $httpOnly = false[, $sameSite = '']]]]]]]])¶
-
Parameters
- $name
array|Cookie|stringCookie name or associative array of all of the parameters available to this function or an instance ofCodeIgniter\Cookie\Cookie - $value
stringCookie value - $expire
intNumber of seconds until expiration. If set to0, the cookie will only last as long as the browser is open - $domain
stringCookie domain (usually: .yourdomain.com) - $path
stringCookie path - $prefix
stringCookie name prefix. If'', the default from app/Config/Cookie.php is used - $secure
boolWhether to only send the cookie through HTTPS. Ifnull, the default from app/Config/Cookie.php is used - $httpOnly
boolWhether to hide the cookie from JavaScript. Ifnull, the default from app/Config/Cookie.php is used - $sameSite
stringThe value for the SameSite cookie parameter. Ifnull, the default from app/Config/Cookie.php is used
- $name
-
Return type:
void
Note
Prior to v4.2.7, the default values of $secure and $httpOnly were false due to a bug, and these values from app/Config/Cookie.php were never used.
This helper function gives you friendlier syntax to set browser cookies. Refer to the Response Library for a description of its use, as this function is an alias for CodeIgniter\HTTP\Response::setCookie().
Note
This helper function just sets browser cookies to the global response instance that Services::response() returns. So, if you create and return another response instance (e.g., if you call redirect()), the cookies set here will not be sent automatically.
get_cookie($index[, $xssClean = false[, $prefix = '']])¶
-
Parameters
- $index
stringCookie name - $xssClean
boolWhether to apply XSS filtering to the returned value - $prefix
string|nullCookie name prefix. If set to'', the default value from app/Config/Cookie.php will be used. If set tonull, no prefix is used
- $index
-
Returns: The cookie value or null if not found
-
Return type:
mixed
Note
Since v4.2.1, the third parameter $prefix has been introduced and the behavior has been changed a bit due to a bug fix. See Upgrading for details.
This helper function gives you friendlier syntax to get browser cookies. Refer to the IncomingRequest Library for a detailed description of its use, as this function acts very similarly to CodeIgniter\HTTP\IncomingRequest::getCookie(), except it will also prepend the Config\Cookie::$prefix that you might've set in your app/Config/Cookie.php file.
Warning
Using XSS filtering is a bad practice. It does not prevent XSS attacks perfectly. Using esc() with the correct $context in the views is recommended.
delete_cookie($name[, $domain = ''[, $path = '/'[, $prefix = '']]])¶
-
Parameters
- $name
stringCookie name - $domain
stringCookie domain (usually: .yourdomain.com) - $path
stringCookie path - $prefix
stringCookie name prefix
- $name
-
Return type:
void
Lets you delete a cookie. Unless you've set a custom path or other values, only the name of the cookie is needed.
<?php
delete_cookie('name');
This function is otherwise identical to set_cookie(), except that it does not have the value and expire parameters.
This also just sets the deletion cookies on the global response instance that Services::response() returns.
Note
When you use set_cookie(), if the value is set to an empty string and the expire is set to 0, the cookie will be deleted. If the value is set to a non-empty string and the expire is set to 0, the cookie will only last as long as the browser is open.
You can submit an array of values in the first parameter, or you can set discrete parameters.
<?php
delete_cookie($name, $domain, $path, $prefix);
has_cookie(string $name[, ?string $value = null[, string $prefix = '']])¶
-
Parameters
- $name
stringCookie name - $value
string|nullCookie value - $prefix
stringCookie prefix
- $name
-
Return type:
bool
Checks if a cookie exists by name in the global response instance that Services::response() returns. This is an alias of CodeIgniter\HTTP\Response::hasCookie().