Skip to content

Clear stale HTTP_* headers from $_SERVER between persistent PHP dispatches#119

Merged
simonhamp merged 1 commit into
mainfrom
inertia-json-fresh-load
May 8, 2026
Merged

Clear stale HTTP_* headers from $_SERVER between persistent PHP dispatches#119
simonhamp merged 1 commit into
mainfrom
inertia-json-fresh-load

Conversation

@simonhamp
Copy link
Copy Markdown
Member

@simonhamp simonhamp commented May 8, 2026

Summary

Fixes raw Inertia JSON being rendered after a top-level navigation following an Inertia visit (reported on iOS, latent on Android).

In the persistent PHP runtime, $_SERVER is a regular superglobal that survives across zend_eval_string() dispatches. The dispatch eval layered new env vars on top with foreach (getenv() as $__k => $__v) { $_SERVER[$__k] = $__v; } but never removed keys from prior requests. Once an Inertia XHR set HTTP_X_INERTIA = 'true', that key remained in $_SERVER for every subsequent request — even after the C-level env var was unset — so a plain window.location.href = '/home' navigation hit Inertia's middleware and got JSON back.

This patch strips HTTP_*, CONTENT_TYPE, and CONTENT_LENGTH keys from $_SERVER at the top of every dispatch in both the iOS (PHP.c) and Android (php_bridge.c) eval blocks, addressing the root cause for any stale request header rather than a single Inertia-specific symptom.

The existing Kotlin band-aid in PHPBridge.kt (which zeroes the five Inertia-specific env vars before each dispatch) becomes redundant but is left in place as defense-in-depth — happy to remove on request.

Test plan

  • Run an Inertia visit, then window.location.href = '/home' on iOS — confirm full HTML loads, not raw JSON
  • Same scenario on Android (already worked via Kotlin workaround; confirm still works)
  • Confirm regular Inertia XHR navigation still receives JSON correctly
  • Confirm POST requests with Content-Type continue to parse $_POST correctly after the clearing step

🤖 Generated with Claude Code

Inertia visits left HTTP_X_INERTIA in $_SERVER, so a subsequent
window.location.href navigation returned JSON instead of HTML.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@simonhamp simonhamp marked this pull request as ready for review May 8, 2026 16:38
@simonhamp simonhamp merged commit 5c7cd10 into main May 8, 2026
0 of 5 checks passed
@simonhamp simonhamp deleted the inertia-json-fresh-load branch May 8, 2026 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant