How to pass variables received in GET string through a php header redirect?

How to pass variables received in GET string through a php header redirect?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Handling State After a Header Redirect in PHP

When you execute a header('Location: ...') directive in PHP, the server immediately sends an HTTP redirect response to the client and terminates the current script execution if you follow it with exit. This behavior is by design; once the headers are sent and the script exits, any subsequent code on that same file is ignored. Consequently, attempting to pass data from a GET string directly into the next page via a PHP superglobal like $_GET or $_POST on the second page will result in empty values. The original parameters exist only in the memory of the first request, and that memory is discarded once the redirect occurs.

To maintain data integrity across this transition, you must serialize the variables into a format that can be reconstructed on the destination page. The standard approach for stateless HTTP requests is to append these values as query parameters to the target URL. However, there are significant architectural implications to consider when doing so, particularly regarding URL length limits and security.

The most straightforward implementation involves constructing a new URL that includes every variable you need to preserve. If your source page collects $fname, $femail, and $fphone, you would modify the redirect logic to look something like this:

$destination = 'confirmed.php?name=' . urlencode($fname) . 
              '&email=' . urlencode($femail) . 
              '&telephone=' . urlencode($fphone);

header('Location: ' . $destination);
exit;

The urlencode() function is critical here. It escapes special characters (like spaces or ampersands) that might otherwise break the query string syntax or cause parsing errors on the receiving end. On the confirmed.php file, you then simply retrieve these values from $_GET as usual:

$name   = $_GET['name'];
$email  = $_GET['email'];
$phone  = $_GET['telephone'];

This method works well for a small number of variables with short string lengths. However, it introduces two major pitfalls that developers often overlook. First, URLs are technically limited in length by browser constraints and HTTP protocol specifications. While modern browsers can handle several kilobytes of query data, exceeding these limits leads to truncated URLs or failed requests. Second, exposing sensitive information—such as email addresses or phone numbers—in the URL makes them visible in server logs, browser history, referrer headers, and potentially via proxy logs if not handled with strict privacy controls.

For scenarios involving sensitive user data or a large payload of variables, appending to the query string is often considered an anti-pattern. Instead, you should leverage server-side sessions. Before performing the redirect, start a session and store the necessary data in $_SESSION. Since sessions are stored on the server and identified by a cookie on the client side, the URL remains clean and does not expose the data to unintended parties or length restrictions.

session_start();

// Store data in session before redirecting
$_SESSION['name'] = $fname;
$_SESSION['email'] = $femail;
$_SESSION['telephone'] = $fphone;

header('Location: confirmed.php');
exit;

On the destination page, you simply retrieve these values from $_SESSION. This approach is generally preferred in frameworks like Laravel or Symfony, where session management is built-in and handled automatically within the global scope. If you are using a framework that wraps PHP core, ensure that session initialization happens before any output is sent, as failing to do so will trigger a "headers already sent" error, preventing the redirect from working correctly.

If you are integrating with third-party services like Aweber or SMS gateways, be mindful of how they handle redirects. Some marketing platforms expect a specific landing page URL without query strings and may flag campaigns if the destination URL changes dynamically based on submission data. In such cases, using session storage is often the safer route to avoid breaking external integrations while keeping your own user experience intact.

Our network