diff --git a/system/HTTP/ResponseInterface.php b/system/HTTP/ResponseInterface.php index 878d1488c25d..9d3c8a9d64ae 100644 --- a/system/HTTP/ResponseInterface.php +++ b/system/HTTP/ResponseInterface.php @@ -322,7 +322,7 @@ public function sendBody(); * * @param array|string $name Cookie name or array containing binds * @param string $value Cookie value - * @param string $expire Cookie expiration time in seconds + * @param int $expire Cookie expiration time in seconds * @param string $domain Cookie domain (e.g.: '.yourdomain.com') * @param string $path Cookie path (default: '/') * @param string $prefix Cookie name prefix @@ -335,7 +335,7 @@ public function sendBody(); public function setCookie( $name, $value = '', - $expire = '', + $expire = 0, $domain = '', $path = '/', $prefix = '', diff --git a/system/HTTP/ResponseTrait.php b/system/HTTP/ResponseTrait.php index 7c0741317bc0..21d9abec8bac 100644 --- a/system/HTTP/ResponseTrait.php +++ b/system/HTTP/ResponseTrait.php @@ -554,7 +554,7 @@ public function redirect(string $uri, string $method = 'auto', ?int $code = null * * @param array|Cookie|string $name Cookie name / array containing binds / Cookie object * @param string $value Cookie value - * @param string $expire Cookie expiration time in seconds + * @param int $expire Cookie expiration time in seconds * @param string $domain Cookie domain (e.g.: '.yourdomain.com') * @param string $path Cookie path (default: '/') * @param string $prefix Cookie name prefix ('': the default prefix) @@ -567,7 +567,7 @@ public function redirect(string $uri, string $method = 'auto', ?int $code = null public function setCookie( $name, $value = '', - $expire = '', + $expire = 0, $domain = '', $path = '/', $prefix = '', @@ -581,14 +581,11 @@ public function setCookie( return $this; } - /** @var CookieConfig|null $cookieConfig */ $cookieConfig = config(CookieConfig::class); - if ($cookieConfig instanceof CookieConfig) { - $secure ??= $cookieConfig->secure; - $httponly ??= $cookieConfig->httponly; - $samesite ??= $cookieConfig->samesite; - } + $secure ??= $cookieConfig->secure; + $httponly ??= $cookieConfig->httponly; + $samesite ??= $cookieConfig->samesite; if (is_array($name)) { // always leave 'name' in last place, as the loop will break otherwise, due to ${$item} @@ -700,7 +697,7 @@ public function deleteCookie(string $name = '', string $domain = '', string $pat } if (! $found) { - $this->setCookie($name, '', '', $domain, $path, $prefix); + $this->setCookie($name, '', 0, $domain, $path, $prefix); } return $this; diff --git a/system/Helpers/cookie_helper.php b/system/Helpers/cookie_helper.php index de8fb6146a63..764669326632 100755 --- a/system/Helpers/cookie_helper.php +++ b/system/Helpers/cookie_helper.php @@ -26,7 +26,7 @@ * * @param array|Cookie|string $name Cookie name / array containing binds / Cookie object * @param string $value The value of the cookie - * @param string $expire The number of seconds until expiration + * @param int $expire The number of seconds until expiration * @param string $domain For site-wide cookie. Usually: .yourdomain.com * @param string $path The cookie path * @param string $prefix The cookie prefix ('': the default prefix) @@ -41,7 +41,7 @@ function set_cookie( $name, string $value = '', - string $expire = '', + int $expire = 0, string $domain = '', string $path = '/', string $prefix = '', diff --git a/user_guide_src/source/changelogs/v4.5.0.rst b/user_guide_src/source/changelogs/v4.5.0.rst index fb0ab21a3734..f5ba1c236ed1 100644 --- a/user_guide_src/source/changelogs/v4.5.0.rst +++ b/user_guide_src/source/changelogs/v4.5.0.rst @@ -57,6 +57,8 @@ Interface Changes or implemented these interfaces, all these changes are backward compatible and require no intervention. +- **ResponseInterface:** The default value of the third parameter ``$expire`` of + the ``ResponseInterface::setCookie()`` has been fixed from ``''`` to ``0``. - **Logger:** The `psr/log `_ package has been upgraded to v2.0.0. @@ -65,6 +67,15 @@ Interface Changes Method Signature Changes ======================== +Setting Cookies +--------------- + +The third parameter ``$expire`` in :php:func:`set_cookie()` and +:php:meth:`CodeIgniter\\HTTP\\Response::setCookie()` has been fixed. + +The type has been changed from ``string`` to ``int``, and the default value has +been changed from ``''`` to ``0``. + FileLocatorInterface -------------------- diff --git a/user_guide_src/source/helpers/cookie_helper.rst b/user_guide_src/source/helpers/cookie_helper.rst index 10002e2a9eb5..296f1a3dd471 100755 --- a/user_guide_src/source/helpers/cookie_helper.rst +++ b/user_guide_src/source/helpers/cookie_helper.rst @@ -21,7 +21,7 @@ Available Functions The following functions are available: -.. php:function:: set_cookie($name[, $value = ''[, $expire = ''[, $domain = ''[, $path = '/'[, $prefix = ''[, $secure = false[, $httpOnly = false[, $sameSite = '']]]]]]]]) +.. php:function:: set_cookie($name[, $value = ''[, $expire = 0[, $domain = ''[, $path = '/'[, $prefix = ''[, $secure = false[, $httpOnly = false[, $sameSite = '']]]]]]]]) :param array|Cookie|string $name: Cookie name *or* associative array of all of the parameters available to this function *or* an instance of ``CodeIgniter\Cookie\Cookie`` :param string $value: Cookie value diff --git a/user_guide_src/source/outgoing/response.rst b/user_guide_src/source/outgoing/response.rst index 078d9d11042a..8d9acb39d126 100644 --- a/user_guide_src/source/outgoing/response.rst +++ b/user_guide_src/source/outgoing/response.rst @@ -453,7 +453,7 @@ The methods provided by the parent class that are available are: followed by the response body. For the main application response, you do not need to call this as it is handled automatically by CodeIgniter. - .. php:method:: setCookie($name = ''[, $value = ''[, $expire = ''[, $domain = ''[, $path = '/'[, $prefix = ''[, $secure = false[, $httponly = false[, $samesite = null]]]]]]]]) + .. php:method:: setCookie($name = ''[, $value = ''[, $expire = 0[, $domain = ''[, $path = '/'[, $prefix = ''[, $secure = false[, $httponly = false[, $samesite = null]]]]]]]]) :param array|Cookie|string $name: Cookie name *or* associative array of all of the parameters available to this method *or* an instance of ``CodeIgniter\Cookie\Cookie`` :param string $value: Cookie value @@ -481,7 +481,7 @@ The methods provided by the parent class that are available are: .. literalinclude:: response/023.php Only the ``name`` and ``value`` are required. To delete a cookie set it with the - ``expire`` blank. + ``value`` blank. The ``expire`` is set in **seconds**, which will be added to the current time. Do not include the time, but rather only the number of seconds diff --git a/user_guide_src/source/outgoing/response/023.php b/user_guide_src/source/outgoing/response/023.php index 3d0855739e96..dd3d905add0e 100644 --- a/user_guide_src/source/outgoing/response/023.php +++ b/user_guide_src/source/outgoing/response/023.php @@ -3,7 +3,7 @@ $cookie = [ 'name' => 'The Cookie Name', 'value' => 'The Value', - 'expire' => '86500', + 'expire' => 86500, 'domain' => '.some-domain.com', 'path' => '/', 'prefix' => 'myprefix_',