PHP Pass variable to next page

PHP Pass variable to next page

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

Stefan Bogdanescu

Founder & Senior Architect

Passing Data Between PHP Pages Without Overkill

When building a multi-step form or a simple wizard, developers often face the immediate need to carry state from one script to the next. In your specific scenario, you have defined $myVariable = "Some text"; on Page1.php and submitted that page's form to Page2.php. The goal is to access that string on the destination page without immediately resorting to a full-blown session mechanism.

While sessions are indeed robust for maintaining state across requests, they introduce overhead. They require server-side storage and cookie management, which feels like using a sledgehammer to crack a nut when you only need to pass a single file name or short string. For lightweight data transfer between two specific pages in a linear flow, the HTTP GET query string is usually the most appropriate tool.

The standard approach relies on the $_GET superglobal array. You must modify your form's submission method and action attribute to explicitly include the variable in the URL parameters. If Page1.php contains a form that posts or gets data to Page2.php, you cannot simply pass $myVariable directly within the PHP logic of the first page unless you are using a framework like Laravel that handles routing automatically. In raw PHP, the data must be part of the request payload at the moment of submission.

To achieve this with GET parameters, update your form in Page1.php to include the variable as a hidden field or an input element, and ensure the action points to Page2.php:

// Page1.php
<?php
$myVariable = "Some text"; 
?>

<!-- Option A: Hidden Input (Preferred for security with sensitive data) -->
<form action="Page2.php" method="get">
    <input type="hidden" name="myfile" value="<?php echo htmlspecialchars($myVariable); ?>">
    
    <!-- Other form fields here -->
    <button type="submit">Go to Page 2</button>
</form>

<!-- Option B: Direct URL (Simplest for public data) -->
<!-- <a href="?myfile=<?php echo urlencode($myVariable); ?>">Go to Page 2</a> -->

On Page2.php, you can retrieve the value using $_GET['myfile']. However, it is critical to handle cases where the variable might be missing. If a user accesses Page2.php directly without passing the parameter, your script will throw a notice or undefined variable error unless you initialize the variable first.

// Page2.php
<?php
$myVariable = isset($_GET['myfile']) ? $_GET['myfile'] : '';

if ($myVariable) {
    // Logic using $myVariable
    echo "Received file: {$myVariable}";
} else {
    echo "No file name provided.";
}
?>

This method works well for short-lived interactions where the data is not required after the current session ends. However, there are distinct trade-offs you must consider before finalizing this architecture. The primary concern with GET parameters is that the data becomes visible in the browser's address bar and server logs. If $myVariable contains a file path or name that could be interpreted as a command injection vector, or if it represents user input, exposing it via URL requires careful sanitization to prevent open redirects or path traversal attacks. Always use urlencode() when setting the value and urldecode() (implicitly handled by PHP) when reading it to ensure special characters are safely transmitted.

If your application grows beyond a simple two-page flow, or if you need to preserve this data while the user navigates away from the current page entirely, sessions become the necessary evolution of this pattern. But for a linear process where Page1 strictly leads to Page2, passing via query parameters remains the most performant and straightforward solution.

Our network