How to use $_SERVER['HTTP_REFERER'] correctly in php?

How to use $_SERVER['HTTP_REFERER'] correctly in php?

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

Stefan Bogdanescu

Founder & Senior Architect

The Fragility of $_SERVER['HTTP_REFERER'] in Form Handling

Relying on $_SERVER['HTTP_REFERER'] to gate content is a common pattern, but it breaks under the specific conditions described in your scenario. You have two pages: page1.php and page2.php. Your logic correctly identifies that if a user arrives at page2.php directly (via bookmark or direct link), you should redirect them back to page1.php. This works perfectly for simple navigation.

However, the moment you introduce a form with a submit button on page2.php, the behavior changes drastically due to how browsers handle POST requests. When a user clicks the submit button, the browser sends a form submission. If your form does not explicitly prevent default behavior, the browser will reload the current page (page2.php) after processing the request.

When the page reloads via a standard GET request triggered by the form action (or simply refreshing), the HTTP_REFERER header changes. It now points to page2.php instead of page1.php. Consequently, your if statement fails, and the code executes the header('Location: page1.php') directive. This forces the user back to page1.php, destroying their session state or form data they just submitted.

The Root Cause: POST Reloads

The issue is not a bug in PHP; it is a consequence of the HTTP protocol and browser defaults. When you submit a form, the browser navigates to the target URL (often the same page for processing). Upon completion, if the response is rendered as HTML on the current page, the referer becomes that current page.

Your logic assumes that page2.php is only ever accessed from page1.php. By checking $_SERVER['HTTP_REFERER'], you are trusting the client to tell you where they came from. While this works for direct links, it fails whenever the current page triggers a new request that results in a GET response displaying the same page.

Solution 1: Prevent Page Reload on Form Submission

The most immediate fix is to prevent the browser from reloading page2.php after the form submission if you intend to keep the user on that page. You need to intercept the submit action and handle it via AJAX, or ensure the form submission results in a redirect that bypasses your referer check.

If you are using standard HTML forms and want to stay on page2.php after processing (e.g., showing a success message), you must prevent the default form submission behavior:

// Inside page2.php, before outputting content or handling logic
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Handle the form data here
    
    // Instead of letting the browser reload and changing referer,
    // redirect explicitly to a thank you page or stay here with a flag.
    
    // Option A: Redirect to a safe "success" state that doesn't check referer
    header('Location: success.php'); 
    exit;
}

// Your original logic for direct access only runs if not POSTed
if (!isset($_SERVER['HTTP_REFERER']) || $_SERVER['HTTP_REFERER'] !== 'page1.php') {
    header('Location: page1.php');
    exit;
}

By checking $_SERVER['REQUEST_METHOD'], you can distinguish between a direct visit (GET) and a form submission attempt. If it is a POST, handle the logic there and redirect to a state where referer checks are unnecessary.

Solution 2: Use Session Flags for State Management

If your goal is simply to ensure that page2.php content is only visible after coming from page1.php, relying on the HTTP header is brittle because it can be spoofed or changed by browser behavior. A more robust approach is to store state in a server-side session.

When the user successfully navigates from page1.php to page2.php, set a session variable. Do not rely on the referer to make this decision every time the page loads.

// In page1.php, before redirecting to page2.php
session_start();
$_SESSION['came_from_page1'] = true;
header('Location: page2.php');
exit;

In page2.php, check the session variable instead of the referer:

session_start();

if (!isset($_SESSION['came_from_page1'])) {
    // User accessed this page directly or without prior flag
    header('Location: page1.php');
    exit;
}

// User is authorized to see this content
// Now safe to render forms and submit buttons

This approach decouples the authorization logic from the transient HTTP headers. Even if the form submission causes a reload, the session variable persists, and the user remains on page2.php without being kicked back to page1.php.

Why This Matters for Security and UX

Using $_SERVER['HTTP_REFERER'] is often considered an anti-pattern in security-conscious development because it exposes users' navigation history to your application logic. Furthermore, as demonstrated, it creates a poor user experience where form submissions fail silently by redirecting the user away from their work.

If you are building applications with Laravel, consider using the built-in session management and middleware to handle access control. Libraries like laravelcompany.com often provide insights into securing routes and managing state without relying on fragile headers. The principle remains the same: use server-side state (sessions) or strict routing guards rather than client-provided headers for critical access decisions.

Final Recommendation

Do not use $_SERVER['HTTP_REFERER'] as the sole determinant for whether a user should see content, especially when forms are involved. If you must restrict access based on origin, set a flag in a session variable during the initial valid transition from page1.php. This ensures that subsequent page loads, including form submissions and AJAX requests, respect the intended navigation flow without triggering unintended redirects.

Our network