fix: make `getVar()` read merged superglobals instead of stale `$_REQUEST` #10587
Hi there, @rahul05ranjan! :wave:
Thank you for sending this PR!
We expect the following in all Pull Requests (PRs).
[!IMPORTANT]
We expect all code changes or bug-fixes to be accompanied by one or more tests added to our test suite to prove the code works.
If pull requests do not comply with the above, they will likely be closed. Since we are a team of volunteers, we don't have any more time to work
on the framework than you do. Please make it as painless for your contributions to be included as possible.
See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md
<!-- carson:welcome -->
Hi. See #10205
We deliberately did not add synchronization. You could add a comment about this.
Thanks for the pointer, @neznaika0 — I see now that #10205 took this same syncRequest() approach and was closed as "not the desired approach." I'll rework this PR accordingly.
Based on the discussion in #9872, the preferred direction is to avoid mutating $_REQUEST entirely and instead have getVar() return a merged view of $_GET, $_POST, and $_COOKIE (respecting request_order), or change withRequest() to stop relying on getVar(). That also moves us toward eventually deprecating getVar().
I'll drop the Superglobals::syncRequest() method and the SiteURIFactory changes, and rework the fix around getVar()/withRequest() instead. I'll update the PR shortly.
Reworked as discussed. The syncRequest() approach is gone.
getVar() now returns a merged view of $_GET, $_POST, and $_COOKIE (respecting request_order/variables_order) instead of reading the stale $_REQUEST, so $_REQUEST is never mutated. This also moves us toward eventually deprecating getVar().
Changes:
Superglobals::getRequestData() returns the merged data (no mutation).RequestTrait::fetchFromArray() extracts the shared filtering/index logic.getVar() uses getRequestData() + fetchFromArray().SiteURIFactory syncRequest() calls.@neznaika0 @paulbalandan — does this match the direction you had in mind from #9872?
Update: the rework is complete and all commits are now GPG-signed.
Summary of the final state:
getVar() reads a merged view of $_GET/$_POST/$_COOKIE (respecting request_order) instead of the stale $_REQUEST, so $_REQUEST is never mutated.Superglobals::getRequestData() returns the merged data; RequestTrait::fetchFromArray() extracts the shared filtering logic.SiteURIFactory syncRequest() calls.The Carson checks (signed-commits, no-merge-commits, pr-title-linter) are all passing. The remaining GitHub Actions workflows are showing action_required and need a maintainer to approve them to run.
@neznaika0 @paulbalandan — whenever you have a moment, could you take a look? Thanks!
I’m not very good at evaluating PR — I don’t know the exact direction in this matter. For myself, I’ve decided not to use $_REQUEST and getVar(). Do you know any specific cases where this is 100 % necessary? In most cases, it’s better to use an explicit data source (GET, POST, COOKIE).
Earlier, it was said that this is a system class (for tests), and for the user, you need to use Request.
Wait for the member answers.
By switching getVar() to the merged GET/POST/COOKIE arrays, this drops values deliberately supplied through the existing request channel ($request->setGlobal('request', ...) or Superglobals::setRequestArray()); those values are no longer consulted. The three Validation tests had to be changed from setRequestArray() to setPostArray(), which masks this regression. Please synchronize the request array at the SiteURIFactory update point and retain the existing request lookup, or otherwise preserve explicit request overrides.
This new merge path has separate cookie handling and order-sensitive overwrite behavior, but the added tests only use non-overlapping GET/POST keys and never populate cookies or exercise request_order/variables_order. A regression in the C branch or precedence would pass unnoticed; add focused assertions for cookies and overlapping keys under the configured order.
This issue also appears on line 416 of the same file.
<!-- ccr-overview-v2 -->
Copilot review overview
🟡 Changes recommended
Critical and moderate findings remain around request overrides and synchronizing $_REQUEST.
Review effort: Lite
Findings: 2 <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.png" alt="High severity" width="62" height="18" align="texttop"></picture>
<details open>
<summary><strong>Open (2)</strong></summary>
</details>
<details>
<summary><strong>What changed in this PR</strong></summary>
Updates request-variable retrieval so getVar() reflects current GET/POST/COOKIE data after URI parsing.
Changes:
| File | Summary |
|---|---|
| tests/system/Validation/ValidationTest.php | Adjusts validation fixtures to use POST data. |
| tests/system/SuperglobalsTest.php | Tests merged request-data behavior. |
| tests/system/HTTP/IncomingRequestTest.php | Updates request-variable fixtures. |
| system/Superglobals.php | Builds merged request data. |
| system/HTTP/RequestTrait.php | Extracts reusable array-fetching logic. |
| system/HTTP/IncomingRequest.php | Uses merged data in getVar(). |
</details>
---
💡 <a href="/codeigniter4/CodeIgniter4/new/develop?filename=.github/skills/code-review/SKILL.md" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Add a code-review agent skill</a> or configure MCP servers for context-aware, tailored reviews. <a href="https://docs.github.com/copilot/how-tos/use-copilot-agents/request-a-code-review/use-code-review?tool=webui#mcp-servers-and-agent-skills" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Learn more in the docs.</a>
@neznaika0 thanks for the review — and your instinct here is exactly right.
To be clear, this PR doesn't add any new reliance on $_REQUEST or getVar(). It fixes a real bug in existing code: Validation::withRequest() still calls $request->getVar(), and getVar() was reading the stale $_REQUEST. So the choice isn't "should we use getVar()" — it's "getVar() is still here and still used by withRequest(), so it must return correct data."
The direction matches what @paulbalandan suggested in #9872: instead of mutating $_REQUEST (which #10205 did and was rightly rejected), getVar() now returns a merged view of $_GET/$_POST/$_COOKIE respecting request_order. $_REQUEST is never touched. This also moves us toward eventually deprecating getVar(), since it no longer depends on $_REQUEST at all.
On "100% necessary" cases: I agree — there's no case where getVar() is strictly necessary, and explicit getGet()/getPost()/getCookie() are always clearer. But withRequest() is public API that currently routes through getVar(), and until that's changed it needs to work. Happy to open a follow-up switching withRequest() to explicit sources if maintainers prefer that direction.
Thanks for the thorough review. This is the intended direction from #9872 — @paulbalandan suggested returning a merged view of the three superglobals and leaving $_REQUEST untouched, which is exactly what this does. $_REQUEST is itself just a merged view of $_GET/$_POST/$_COOKIE per request_order, so reading the merged view directly is semantically equivalent in normal operation while fixing the staleness bug.
On the Validation test changes: those tests were setting POST data (id_user, name_user) through setRequestArray() as a shortcut because getVar() happened to read $_REQUEST. Switching them to setPostArray() is more accurate — the data is POST data — and doesn't mask a regression; it reflects the corrected data source. I've also added testGetVarReflectsGetChangesWhenRequestIsStale to lock in the actual fix.
Explicit $_REQUEST overrides via setRequestArray()/setGlobal('request', ...) are test-only conveniences; in production $_REQUEST is always derived from the other superglobals. The maintainers are moving toward deprecating getVar() entirely, so this is a step in that direction.
Good catch — addressed. I've added focused tests for the cookie branch, order-sensitive overwrite behavior, and unknown order types, and made getRequestData() accept an optional request_order override so precedence can be tested deterministically without mutating ini state:
testGetRequestDataMergesCookie — exercises the C branch.testGetRequestDataRespectsOrder — verifies later sources overwrite earlier ones (GP, GPC, PG).testGetRequestDataIgnoresUnknownOrderTypes — confirms unknown letters are skipped.The request_order override is test-only; production callers pass nothing and the ini fallback chain is unchanged.
@paulbalandan — gentle ping on this one. The rework is complete and follows the direction you outlined in #9872: getVar() now returns a merged view of $_GET/$_POST/$_COOKIE (respecting request_order) instead of reading the stale $_REQUEST, and $_REQUEST is never mutated.
All Carson checks pass (signed-commits, no-merge-commits, pr-title-linter). The remaining GitHub Actions workflows are showing action_required and need a maintainer to approve them to run (first-time contributor). Would you be able to take a look when you have a moment? Thanks!
Hi, thanks for picking this up. Just a few initial comments:
1. Since the approach changed, the PR title and body should be edited to reflect the new approach taken. This is because the PR would be squashed on merge with the PR title as the commit message.
2. The merging of superglobals seems to be not the way PHP would do it. First, when the data contains a numeric key, like when parsing 100=foo, the array_merge would just renumber it. Second, it is not recursive unlike what PHP do when it creates a $_REQUEST.
3. Docs should be updated to match new behavior. Plus a changelog line.
Also, could you remove those backslashes? 😅
@paulbalandan thanks for the review! All three points are addressed, and the backslashes are gone 😅
1. PR title/body — updated to reflect the new approach (no more syncRequest()). Title is now fix: make getVar() read merged superglobals instead of stale $_REQUEST.
2. Merge semantics — switched from array_merge() to array_replace_recursive(), which matches PHP's own $_REQUEST merge (php_autoglobal_merge): numeric keys are preserved and array values are merged recursively. Added testGetRequestDataPreservesNumericKeys and testGetRequestDataMergesRecursively to cover both.
3. Docs + changelog — updated the getVar() section in the user guide and added a "Bugs Fixed" entry to v4.7.5.rst.
Let me know if there's anything else you'd like adjusted. Thanks!
The parameter looks to me like explicitly supplied only for testing but depends on the INI setting on production. A cleaner alternative would be to ditch the param entirely and just use ini_set in tests.
can we just cast string for both ini_get so that just compare to empty string?
pls align the entry alphabetically. and i think it should be HTTP
Thanks for the changes. A few more comments.
@paulbalandan thanks for the follow-up! All three points addressed:
1. Dropped the $requestOrder parameter — getRequestData() now takes no arguments and reads the ini settings directly. Tests use ini_set('request_order', ...) to exercise the different orders deterministically.
2. Cast ini_get() to string — both request_order and variables_order are now (string) ini_get(...) and compared against '' only.
3. Changelog — the entry is now alphabetically ordered and labeled HTTP: (referencing IncomingRequest::getVar()).
Let me know if there's anything else. Thanks!
Can you check on the failing tests?
Thanks for checking that ini_set won't work for request_order. Minor comments, otherwise looks good:
1. incomingrequest.rst line 173 still reads that getVar can fallback to $_REQUEST. it should now be at the merged array
2. the phpstan-ignore at the test demonstrated that the array shapes at Superglobals are incorrectly typed. out of scope of this PR though
Request and IncomingRequest are non-final extension points, so adding this generic protected method on the develop patch line can make an existing application subclass that already defines fetchFromArray() with another signature fail at class loading. Keep this helper out of the inherited protected API (for example, use an internal/private collaborator or reuse the existing fetch path without introducing a new protected symbol).
PHP accepts lowercase directive letters and merges each G/P/C source only on its first occurrence; this loop only recognizes uppercase and re-applies duplicates. Consequently, request_order=gp makes getVar() return no request data, while GPG lets GET overwrite POST unlike PHP's $_REQUEST. Normalize case and de-duplicate while preserving the first occurrence, and cover both cases in the order test.
<!-- ccr-overview-v2 -->
Copilot review overview
🟡 Changes recommended
Request-order parsing diverges from PHP semantics, and the new protected method introduces a patch-line compatibility risk.
Review effort: Balanced
Findings: 1 <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.png" alt="High severity" width="62" height="18" align="texttop"></picture> · 1 <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture>
<details open>
<summary><strong>Open (2)</strong></summary>
</details>
<details>
<summary><strong>Resolved since last review (2)</strong></summary>
</details>
I see a big problem because this will not work anymore:
$request->setGlobal('request', ['foo' => 'bar']);
$request->getVar('foo');
@michalsn thanks for catching this. Fixed in 90199d6: setGlobal('request', ['foo' => 'bar']) takes precedence, including an explicit empty array. I added a regression test for your example. My earlier claim that these overrides were only test conveniences was too broad; this public API needs to keep working.
The same commit removes the added protected fetchFromArray() helper and reuses the existing fetchGlobal() path, with temporary merged data cleared in finally. A subclass with its own differently shaped fetchFromArray() now loads successfully, and repeated reads still see fresh data even after a filter throws.
request_order is now case-insensitive and each source is processed once (gp, GPG, and mixed-case repeated sources are covered). @paulbalandan the JSON note now says "merged superglobal data" instead of $_REQUEST. The PR description is updated too.
Focused PHP 8.2 checks: IncomingRequest 116 tests, Superglobals 55, Request 40, and the three affected Validation cases all pass (214 total). File-scoped PHPStan and CS checks pass. The previous head's CI was green. On this new head, Carson checks pass and GitHub verifies the commit signature, but the remaining workflows are action_required and need maintainer approval. Could a maintainer approve them? PHPUnit: https://github.com/codeigniter4/CodeIgniter4/actions/runs/37283280737
Hi there, @rahul05ranjan! :wave:
It looks like this pull request does not follow our template:
Please update the description to follow the template. The needs template label will be removed automatically once it does.
<!-- carson:template-enforcer -->
Fixed in 90199d6. The new protected helper is removed and RequestTrait is restored to its existing implementation. getVar() reuses fetchGlobal() with temporary merged data cleared in finally. Added a regression test using an IncomingRequest subclass with its own incompatible fetchFromArray() signature; it failed at class loading before the fix and now passes.
Fixed in 90199d6. The order string is normalized to uppercase and sources are deduplicated while retaining their first occurrence. Added assertions for gp, GPG, and gPcGpC; the focused Superglobals tests pass.
I still have two concerns:
1. Calling fetchGlobal('request', ...) before getVar() populates the request cache. getVar() then treats it as an explicit override and returns stale REQUEST data instead of merged GPC data. We should distinguish cached data from an explicit setGlobal() override.
2. An explicitly empty request_order="" should produce an empty result. PHP falls back to variables_order only when request_order is unset, but this implementation treats both cases the same.
Could we add regression tests for these cases?
@michalsn Thanks for flagging both cases. The cached-request case was real: fetchGlobal('request', ...) could fill the cache before getVar(). I now distinguish an explicit setGlobal('request', ...) override from that cache, and added a regression for the call order you described.
For the empty request_order case, I checked PHP's own php.ini documentation: an empty value falls back to variables_order; it does not make $_REQUEST empty. I kept that behavior and added a test for getRequestData('').
I also rebased away the merge commit that was failing Carson's check. All branch commits are signed. Focused PHP 8.2 tests pass: 117 IncomingRequest, 56 Superglobals, and 40 Request tests. Carson's no-merge-commits and signed-commits checks now pass. The remaining workflows on this new head are awaiting maintainer approval to run. Could a maintainer approve them when reviewing? PHPUnit run
For the emptyrequest_ordercase, I checked PHP's own php.ini documentation: an empty value falls back tovariables_order; it does not make$_REQUESTempty. I kept that behavior and added a test forgetRequestData('').
Ok, sounds good.
The two Coding Standards jobs flagged the same one-line docblock on the new request-override property. I changed it to the layout PHP CS Fixer requested. The focused PHP 8.2 fixer check now reports 0 files to fix.
The updated branch is at 59ff334a2. The new workflows are waiting for maintainer approval again; Coding Standards run. Could a maintainer approve that run when convenient?
I've spent quite a while thinking about this, and my main concern is the complexity of the proposed behavior.
getVar() now combines JSON handling, explicit REQUEST overrides, fresh GPC merging, and temporarily replacing and restoring the REQUEST cache. Each part has a reason, but together they make the method harder to understand and maintain.
This also introduces different data lifetimes within the same request object. After GET data changes through the Superglobals service, getVar() sees the updated value, while getGet() may still return a previously cached value. If setGlobal('request', ...) is called, getVar() uses that override instead of fresh GPC data. Understanding the result therefore requires knowing both which method is called and how the request object was previously modified.
The finally block handles cache restoration correctly, but future changes, custom fetchGlobal() implementations, and filter callbacks need to account for that temporary state.
I don't think these mechanisms are inherently wrong. I'm questioning whether this additional complexity is justified for a bug caused by GET normalization in SiteURIFactory. Could we reconsider a one-time REQUEST update at that point, while preserving application-defined REQUEST values? That would keep the fix closer to its cause and preserve getVar() existing lookup behavior.
The user guide says this method exists for backward compatibility, so I would prefer to keep this fix as narrow as possible.
If we decide that the behavior introduced by this PR is what we want long term, I think we should target the 4.8 branch instead of develop, since it changes the existing contract of getVar().
Thoughts?
@michalsn Thanks for the careful review. I agree the current getVar() path has grown complicated for a fix to URI normalization. The temporary cache swap and explicit-override flag are a lot of machinery for a backward-compatibility method.
I avoided changing SiteURIFactory because #10205 was closed, but I shouldn't treat that as ruling out your narrower proposal. That PR rebuilt $_REQUEST after each setGetArray() call; a one-time update that preserves application-defined REQUEST values is a different approach.
I'd be happy to rework this around that normalization point, keeping regression tests for withRequest() and explicit REQUEST values. @paulbalandan, does that align with why #10205 was closed? If the team prefers the fresh merged behavior in this PR, I agree it should target 4.8.
To answer directly: #10205 was closed because I preferred a merged view at the time, not because a one-time refresh was ruled out in #9872. Having seen where the merged view ends up, I agree with @michalsn. Let's do it as two steps. Please open a new PR against develop with the one-time $_REQUEST refresh in SiteURIFactory, refreshing only the keys that come from GET, POST and COOKIE so other values already in $_REQUEST stay untouched. Bring getRequestData() and its tests along since the refresh needs that merge, plus the withRequest() regression tests. Once that is up we can close this one. The remaining idea here, getVar() reading live data, is really a contract change, and the better 4.8 follow-up is to deprecate getVar() and have withRequest() read the merged data directly. Happy to review that as a separate PR if you want to take it.
That sounds good to me. Once the one-time refresh fixes the bug, I would prefer to keep getVar() existing behavior rather than change its contract unnecessarily.
---
For 4.8, we could formally deprecate both getVar() and withRequest() while preserving their behavior. The user guide already discourages using both methods, and explicitly passing the data to validate seems like a clearer migration path. We should also consider Controller::validate(), since it uses withRequest() internally.
That said, I would prefer to keep these changes gradual and avoid changing too much at once. Deprecation should give users a clear migration path while keeping existing applications working.
I'm also wondering whether we would need replacement methods with clearly defined rules for selecting input data, or whether the existing alternatives are sufficient and we should focus on promoting them.
Thanks, @paulbalandan and @michalsn. I opened #10606 from current develop with the one-time REQUEST refresh in SiteURIFactory. It carries getRequestData() and its merge tests, plus regressions for both URI parsing paths and withRequest(). getVar() itself is unchanged.
The focused tests and file-scoped checks pass, and the commit is verified. I'll close this PR in favor of #10606; the broader deprecation discussion can stay separate for 4.8.
neznaika0
rahul05ranjan
Description
Fixes #9872.
SiteURIFactoryupdates GET data after PHP initializes$_REQUEST, sogetVar()can return stale values toValidation::withRequest().This changes
getVar()to read a fresh merged view of GET, POST, and COOKIE data, respectingrequest_order/variables_order, without mutating$_REQUEST. ExplicitsetGlobal('request', ...)overrides and JSON requests retain their existing lookup behavior.The merge preserves numeric keys, merges nested arrays recursively, normalizes lowercase order letters, and processes each source only once. It reuses the existing
fetchGlobal()filtering path and clears temporary merged data infinally, avoiding a new protected helper that could conflict with application subclasses. The user guide and changelog describe the updated data source.Checklist:
Testing:
IncomingRequestTest.php116 tests,SuperglobalsTest.php55 tests,RequestTest.php40 tests, and the three affected Validation cases pass (214 tests total).