Skip to content

fix: getGetPost() and getPostGet() when index is null #6675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions system/HTTP/IncomingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,9 @@ public function getPost($index = null, $filter = null, $flags = null)
*/
public function getPostGet($index = null, $filter = null, $flags = null)
{
if ($index === null) {
return array_merge($this->getGet($index, $filter, $flags), $this->getPost($index, $filter, $flags));
}
// Use $_POST directly here, since filter_has_var only
// checks the initial POST data, not anything that might
// have been added since.
Expand All @@ -630,6 +633,9 @@ public function getPostGet($index = null, $filter = null, $flags = null)
*/
public function getGetPost($index = null, $filter = null, $flags = null)
{
if ($index === null) {
return array_merge($this->getPost($index, $filter, $flags), $this->getGet($index, $filter, $flags));
}
// Use $_GET directly here, since filter_has_var only
// checks the initial GET data, not anything that might
// have been added since.
Expand Down
20 changes: 20 additions & 0 deletions tests/system/HTTP/IncomingRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,26 @@ public function testGetPostEmpty()
$this->assertSame($_GET, $this->request->getGetPost());
}

public function testPostGetSecondStream()
{
$_GET['get'] = '3';
$this->assertSame($_GET, $this->request->getPostGet());
}

public function testGetPostSecondStream()
{
$_POST['post'] = '5';
$this->assertSame($_POST, $this->request->getGetPost());
}

public function testGetPostSecondStreams()
{
$_GET['get'] = '3';
$_POST['post'] = '5';
$this->assertSame(array_merge($_GET, $_POST), $this->request->getPostGet());
$this->assertSame(array_merge($_POST, $_GET), $this->request->getGetPost());
}

public function testWithFalseBody()
{
// Use `false` here to simulate file_get_contents returning a false value
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/changelogs/v4.2.8.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ none.
Bugs Fixed
**********

none.
- Fixed a bug when the ``CodeIgniter\HTTP\IncomingRequest::getPostGet()`` and ``CodeIgniter\HTTP\IncomingRequest::getGetPost()`` methods didn't return values from the other stream when ``index`` was set to ``null``.

See the repo's `CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_ for a complete list of bugs fixed.
14 changes: 11 additions & 3 deletions user_guide_src/source/incoming/incomingrequest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ The methods provided by the parent classes that are available are:
found `here <https://www.php.net/manual/en/filter.filters.php>`__.
:param int $flags: Flags to apply. A list of flags can be found
`here <https://www.php.net/manual/en/filter.filters.flags.php>`__.
:returns: $_POST if no parameters supplied, otherwise the POST value if found, or null if not
:returns: $_POST and $_GET combined if no parameters specified (prefer POST value on conflict),
otherwise looks for POST value, if nothing found looks for GET value, if no value found returns null
:rtype: mixed|null

This method works pretty much the same way as ``getPost()`` and ``getGet()``, only combined.
Expand All @@ -348,22 +349,29 @@ The methods provided by the parent classes that are available are:

.. literalinclude:: incomingrequest/032.php

If no index is specified, it will return both POST and GET streams combined.
Although POST data will be preferred in case of name conflict.

.. php:method:: getGetPost([$index = null[, $filter = null[, $flags = null]]])

:param string $index: The name of the variable/key to look for.
:param int $filter: The type of filter to apply. A list of filters can be
found `here <https://www.php.net/manual/en/filter.filters.php>`__.
:param int $flags: Flags to apply. A list of flags can be found
`here <https://www.php.net/manual/en/filter.filters.flags.php>`__.
:returns: $_POST if no parameters supplied, otherwise the POST value if found, or null if not
:returns: $_GET and $_POST combined if no parameters specified (prefer GET value on conflict),
otherwise looks for GET value, if nothing found looks for POST value, if no value found returns null
:rtype: mixed|null

This method works pretty much the same way as ``getPost()`` and ``getGet()``, only combined.
It will search through both POST and GET streams for data, looking first in GET, and
It will search through both GET and POST streams for data, looking first in GET, and
then in POST:

.. literalinclude:: incomingrequest/033.php

If no index is specified, it will return both GET and POST streams combined.
Although GET data will be preferred in case of name conflict.

.. php:method:: getCookie([$index = null[, $filter = null[, $flags = null]]])
:noindex:

Expand Down