Passing multiple variables to another page in url

Passing multiple variables to another page in url

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

Stefan Bogdanescu

Founder & Senior Architect

Concatenating Multiple Variables in URL Query Strings

Attempting to pass multiple variables via a URL query string is a common requirement when building multi-step workflows or redirecting users based on session context. The specific issue you are encountering stems from a fundamental syntax error in how the query string is being constructed. In your current implementation, you are missing the & delimiter between parameters.

When you execute the line $url = "http://localhost/main.php?email=" . $email_address . $event_id;, PHP concatenates the values directly into a single string. If $email_address is "test@example.com" and $event_id is "123", the resulting URL becomes:

http://localhost/main.php?email=test@example.com123

Notice there is no separator between test@example.com and 123. To the web server and PHP's $_GET superglobal, this entire string looks like a single value for the key email. Consequently, when Page 2 tries to access $_GET['event_id'], that key does not exist in the array because it was never registered as a separate parameter.

Correcting the Query String Construction

To pass multiple variables, you must explicitly define the boundary between them using the ampersand (&) character. This is the standard delimiter for HTTP query strings.

Here is the corrected approach:

$url = "http://localhost/main.php?email=" . $email_address . "&event_id=" . $event_id;

With this change, the URL becomes http://localhost/main.php?email=test@example.com&event_id=123. Now, $_GET['email'] will contain the email address and $_GET['event_id'] will contain the ID. Your conditional check on Page 2 (if (isset($_GET['event_id']))) will then correctly identify the variable and prevent the "Undefined variable" error.

Understanding the Limitations of Query Strings

While fixing the syntax error resolves your immediate problem, relying solely on session data for URLs introduces significant architectural weaknesses that experienced developers should avoid in production environments.

Security Risks

Your current code exposes sensitive session data ($event_id) directly in the URL. This is problematic for several reasons:

  1. Logging and Referrer Headers: Every time a user visits Page 2, their browser sends the full URL to the server's logs and often to the referrer header of subsequent requests. If you later redirect to an external domain (like a social media login or a payment gateway), that sensitive event ID is leaked to third parties.
  2. Caching: Some browsers and intermediate proxies cache GET requests. If Page 2 renders content specific to a user's session, caching the URL with their event_id can lead to privacy breaches where users see other people's data.

Scalability Issues

Query strings have length limits (typically around 2048 characters in most browsers). If you attempt to pass large amounts of data or numerous variables, the URL will break. Furthermore, relying on sessions for routing logic creates "session coupling," making it difficult to build stateless APIs or handle concurrent users efficiently without hitting session storage limits.

The Robust Alternative: Direct Link Generation

In modern web development frameworks like Laravel, you rarely manually concatenate strings with . and []. Instead, you use helper functions that handle encoding, security, and parameter parsing automatically.

If you are working within a framework context, the recommended pattern is to pass an associative array of parameters. This ensures special characters in values (like @ or spaces) are properly URL-encoded (e.g., %40) without manual intervention.

// Recommended approach using Laravel's url() helper or similar
$url = route('main', [
    'email' => $email_address,
    'event_id' => $event_id
]);

Or if you are using a raw URL string with an array:

$url = "http://localhost/main.php?email=" . urlencode($email_address) . "&event_id=" . urlencode($event_id);

Using urlencode() is critical when values contain spaces, ampersands, or other special characters. Without it, the browser interprets parts of the value as new parameters. For instance, if $email contained a space, the URL would break at that space unless encoded.

Summary of Best Practices

  1. Always separate parameters: Never concatenate values directly; always use & between key-value pairs.
  2. Encode your data: Use urlencode() or framework helpers to handle special characters safely.
  3. Avoid leaking session IDs in URLs: Keep sensitive identifiers like event_id out of the URL if possible, or ensure they are short-lived and not logged externally.
  4. Leverage routing: If you have a complex application, use a router to generate links rather than manually constructing strings. It provides better control over parameters and cleaner code.

By correcting the concatenation syntax and adopting these safer patterns, you ensure that both variables are retrieved correctly on Page 2 without introducing security vulnerabilities or data leakage.

Our network