Decoding Variables Passed via header() in PHP
When you execute header("Location: temp.php?user=" . $user), you are technically performing a redirection, but the mechanism by which data travels between those two requests is what often confuses developers. The variable $user isn't being passed through the HTTP header itself; rather, it is being appended to the URL as a query string parameter. Understanding this distinction is crucial because it dictates how you must retrieve and sanitize the value on the destination page.
On temp.php, you cannot access $user by referencing the GET request directly in a way that bypasses PHP's input handling. Instead, you must explicitly read from the $_GET superglobal array. If your code attempts to use $user without first assigning it from $_GET, you will encounter an "Undefined variable" notice or error depending on your error_reporting settings.
The standard implementation looks like this:
$temp_user = $_GET['user'];
echo $temp_user;
However, simply retrieving the value is rarely sufficient in a production environment. The moment you see a variable appended to a URL via header(), security should immediately become a primary concern. Query strings are visible to anyone who can inspect the network tab of their browser or view the page source. This makes them an obvious target for Cross-Site Scripting (XSS) attacks, especially if that data is later rendered into HTML without escaping.
If temp_user contains malicious JavaScript code and you echo it directly into a <script> tag or inside an HTML element without sanitization, your application becomes vulnerable. You must ensure that the value is treated strictly as data, not executable code, whenever it interacts with the DOM. Use functions like htmlspecialchars() if you plan to output this variable back to the browser within HTML contexts:
echo htmlspecialchars($_GET['user'], ENT_QUOTES, 'UTF-8');
Beyond security, there is a significant architectural trade-off to consider regarding state management. Passing sensitive data or session identifiers via URL parameters is generally an anti-pattern. If you are passing user IDs or tokens this way, ensure that the destination page immediately validates this value against your database or session store. Do not trust the value passed in the query string as canonical identity; treat it merely as a request to look up specific data.
Furthermore, relying on $_GET implies that the variable exists only for the duration of that specific request-response cycle. If you need to persist this user context across multiple subsequent requests within the same session, you must store it in the server-side session immediately upon retrieval:
$_SESSION['current_user'] = $_GET['user'];
This decouples the URL from the internal state of your application, allowing you to clean up query parameters later if necessary. It also protects against accidental data leakage where a user might accidentally share a link containing sensitive user data without realizing it.
While header() redirection is a valid tool for routing logic, always verify that the target page handles input validation rigorously. If you are building a larger application using frameworks like Laravel, the framework's request abstraction layer often handles this retrieval and sanitization automatically, reducing the risk of manual errors. However, understanding the underlying mechanics ensures you can debug issues effectively even when moving away from frameworks or writing raw PHP scripts.