PHP: Redirect same page after click the submit button

PHP: Redirect same page after click the submit button

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

Stefan Bogdanescu

Founder & Senior Architect

PHP: Redirect Same Page After Form Submission

When a user submits a form, the browser expects a response from the server. If that response is a new HTML page, you've likely already handled it. However, if your goal is to process the data and immediately display a refreshed view of the current page—perhaps showing updated results in a GridView or a list—you need a specific mechanism: an internal redirect back to the same URL.

The standard approach for this is using the header() function with a relative path. You might have tried writing header('Location: main.php');, but if that feels like it's reloading the entire site or behaving unexpectedly, you're likely fighting against how HTTP redirects work versus simple page reloads.

To redirect to the same page from within your own script, you must pass the current URL to the header() function. The syntax looks like this:

<?php
// Ensure no other output has been sent before calling header()
if (!empty($_POST)) {
    // Redirect back to the current script's location
    header("Location: " . $_SERVER['PHP_SELF']);
    
    // It is crucial to exit after redirecting
    exit();
}

// If we reach this line, either no POST data was sent or the redirect failed
// Proceed with your logic, such as refreshing a GridView

The key here is $_SERVER['PHP_SELF']. This server variable contains the path of the current script relative to the document root. By echoing it into the Location header, you tell the browser: "Go back exactly where you just came from." The subsequent exit() call is non-negotiable. Without it, PHP continues executing and might render output that conflicts with the redirect headers, causing mixed content warnings or blank screens.

If you are working within a framework like Laravel, the logic remains similar, but the implementation differs slightly due to the request lifecycle management frameworks employ. In a traditional PHP script, $_SERVER['PHP_SELF'] is reliable. In a framework, relying on global variables can sometimes bypass the framework's routing or middleware, which might be why you felt header('Location: main.php') didn't work as expected. If main.php represents the entry point for your application, simply redirecting to that file without context often strips away the session state or database connection that your form handler relies on.

The Problem with Absolute Paths

A common pitfall is attempting to hardcode the path. If you write header('Location: /path/to/main.php');, you are forcing an absolute redirect. This can break if the request originated from a subfolder (e.g., example.com/dashboard/main.php). An absolute path ignores the current directory context, sending the user back to the root level instead of refreshing their specific view. Always use the relative path provided by the server environment to maintain context.

Handling Frameworks and Routing

If you are using Laravel or similar frameworks, internal redirects are often handled through the router's helper functions rather than raw header() calls. However, the principle of staying on the same page is identical. If your form submission validates data and updates a grid, you want the user to see that update immediately. In many MVC architectures, this is naturally achieved by rendering the same view after processing logic.

If you must force a redirect in a framework context, ensure you are referencing the current route or controller action. Using return redirect()->back() in Laravel effectively achieves the same goal as $_SERVER['PHP_SELF'] in vanilla PHP: it sends the response code 302 and points the browser to the previous URL without losing the session or request context.

Trade-offs and Security Considerations

While redirecting back to the current page is safe for standard form submissions, be wary of "open redirect" vulnerabilities if you are ever constructing URLs based on user input. Never append $_GET['redirect'] to a header string without strict validation. In the context of refreshing a grid or updating a local view, the destination is always the script itself, so no external URL manipulation occurs.

The most robust solution avoids manual header manipulation if possible. By designing your controller or action to simply return the same view after processing, the framework handles the status codes and headers for you. This prevents issues where output buffering interferes with the redirect, ensuring the browser receives the correct 302 response and refreshes the grid exactly as intended.

Our network