How to set $_GET variable

How to set $_GET variable

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

Stefan Bogdanescu

Founder & Senior Architect

How to set $_GET variable without a form submission

In PHP, $_GET is a superglobal array populated by the HTTP query string appended to the request URL. When you access it in your script, you are essentially reading data that the client (browser) has already sent. If you need to simulate this behavior—populating $_GET without relying on a user submitting an HTML form with a method of GET—you must manipulate the request environment directly. This is a common requirement for automated testing, mocking external services, or triggering logic based on URL parameters during development.

The most direct approach involves modifying the query string of the current request before your script processes it. You can achieve this by appending the desired parameter to the $_SERVER['REQUEST_URI'] and then refreshing the page, but if you are working within a single PHP execution flow (e.g., in a unit test or a CLI script), you might want to inject the data programmatically without restarting the HTTP cycle.

Modifying the Query String via URL Rewriting

If you cannot restart the request or modify the browser address bar interactively, you can use an internal rewrite or redirect to append the variable. This changes the $_GET array in the subsequent load of the script while keeping the original request context intact for other variables like $_POST or session data.

<?php
// Simulate a GET parameter by rewriting the URL internally
$currentUri = $_SERVER['REQUEST_URI'];
$newQuery = parse_url($currentUri, PHP_URL_QUERY);

if ($newQuery === '') {
    $newQuery = '';
} else {
    $newQuery .= '&';
}

// Append your desired variable
$newQuery .= 'action=process_data';

// Redirect to the new URL (internal)
header("Location: ?{$newQuery}", true, 302);
exit;

This approach triggers a new request cycle. The browser updates its address bar, and when the script loads again, $_GET['action'] will be populated as expected. This is semantically correct because it respects the HTTP protocol: data in $_GET must correspond to data in the URL query string.

Injecting Data for Unit Testing

In a testing environment, such as PHPUnit or when using tools like Laravel's testing suite, you might not want to issue an actual HTTP redirect. Instead, you can manipulate the superglobals directly before executing your logic. This bypasses the network layer entirely and is useful for verifying that your application handles specific query parameters correctly without user interaction.

<?php
// WARNING: Only do this in isolated testing environments or CLI scripts
// Never do this in production code intended to handle real users

$_GET['fake_param'] = 'test_value';
// Or overwrite the whole array if you want to isolate strictly
$_GET = ['fake_param' => 'test_value'];

// Proceed with your logic
if (isset($_GET['fake_param'])) {
    echo "Received: " . $_GET['fake_param'];
}

While this works technically, be aware of the implications. By manually assigning to $_GET, you are bypassing the server's validation mechanisms that might expect a specific query string format. If your application relies on libraries (like Laravel or Symfony) to parse routes based on the URL path and query parameters, direct assignment might conflict with their internal routing logic unless you are operating in a fully decoupled CLI test context.

Security and Validation Considerations

Manipulating $_GET variables manually introduces significant security risks if done carelessly. In a production environment, trusting client input—even if you control the code that sets it—is dangerous. If you are implementing this logic to handle specific workflow triggers, ensure that any action taken based on these variables is validated against strict rules.

For instance, if your application uses Laravel, you should leverage its built-in request validation features rather than relying solely on superglobal assignments. You can define route parameters or query constraints in routes/web.php which handle the parsing securely. If you are working with a specific library that offers helper functions for setting up test requests, prefer those over raw array manipulation to ensure all middleware and event listeners fire correctly.

Ultimately, the decision to set $_GET variables manually depends on your context. For testing, direct assignment is acceptable within isolated environments. For production workflows involving URL rewriting, using HTTP redirects or internal rewrites ensures that the data flow remains consistent with web standards and security best practices.

Our network