Skip to content

Commit

Permalink
Merge pull request #8080 from kenjis/fix-set_cookie-expire
Browse files Browse the repository at this point in the history
fix: set_cookie() $expire type
  • Loading branch information
kenjis authored Oct 27, 2023
2 parents 5e08ece + 67f2b78 commit 9f1e38d
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 17 deletions.
4 changes: 2 additions & 2 deletions system/HTTP/ResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -335,7 +335,7 @@ public function sendBody();
public function setCookie(
$name,
$value = '',
$expire = '',
$expire = 0,
$domain = '',
$path = '/',
$prefix = '',
Expand Down
15 changes: 6 additions & 9 deletions system/HTTP/ResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 = '',
Expand All @@ -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}
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions system/Helpers/cookie_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -41,7 +41,7 @@
function set_cookie(
$name,
string $value = '',
string $expire = '',
int $expire = 0,
string $domain = '',
string $path = '/',
string $prefix = '',
Expand Down
11 changes: 11 additions & 0 deletions user_guide_src/source/changelogs/v4.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://packagist.org/packages/psr/log>`_ package has
been upgraded to v2.0.0.

Expand All @@ -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
--------------------

Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/helpers/cookie_helper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions user_guide_src/source/outgoing/response.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/outgoing/response/023.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
$cookie = [
'name' => 'The Cookie Name',
'value' => 'The Value',
'expire' => '86500',
'expire' => 86500,
'domain' => '.some-domain.com',
'path' => '/',
'prefix' => 'myprefix_',
Expand Down

0 comments on commit 9f1e38d

Please sign in to comment.