Notice: Undefined index: HTTP_REFERER

Notice: Undefined index: HTTP_REFERER

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

Stefan Bogdanescu

Founder & Senior Architect

The Missing HTTP_REFERER and the Silent Failing Form

You are encountering a classic PHP warning that often trips up developers moving from tutorials to production environments: Notice: Undefined index: HTTP_REFERER. This happens because your code attempts to access $_SERVER['HTTP_REFERER'] without checking if the key actually exists.

In your specific case, you have confirmed via print_r($_SERVER) that HTTP_REFERER is indeed missing from the array. This typically occurs when a user navigates directly to your script via a bookmark or by typing the URL into the address bar, rather than arriving there from another page. When this happens, $_SERVER['HTTP_REFERER'] returns an empty string in older PHP versions or triggers a notice in strict modes because the index is absent.

While fixing that specific warning is straightforward—using isset() or checking if the key exists before accessing it—the real issue hidden in your code is more significant: your form validation logic is failing silently, even when fields are filled.

The Logic Trap in Your Conditionals

Your current registration logic relies on a nested structure that creates an "and also" problem. You have this block:

if(!loggedIn()) {
    // ... check POST variables ...
} elseif (loggedIn()) {
    echo 'you \'re already logged in';
}

The problem lies in the outer condition if(!loggedIn()). If a user is not logged in, you enter the block. Inside that block, you have an inner if statement checking for POST data. However, if the inner if condition fails (e.g., fields are empty or the specific logic check you added with "e"), the code falls through to the else block inside the same scope.

But wait, look closer at your HTML form:

<form action="register.php" method="POST">

You are including this file via require 'core.inc.php';. If core.inc.php contains a session start or other logic that alters the flow, you might be seeing unexpected results. More critically, look at your error handling. You have:

} elseif (loggedIn()) {
    echo 'you \'re already logged in';
}

This elseif is attached to the outer if(!loggedIn()). This creates an impossible scenario for a single request. A user cannot be both logged in (loggedIn() returns true) AND not logged in (!loggedIn() returns true) at the exact same time. Therefore, the elseif (loggedIn()) block will never execute if the first condition is false.

If you are seeing "All fields are required" even when the form is filled, it means the code is hitting the else branch inside your inner if. You mentioned adding "e" to force an error message. This confirms that the condition checking for empty fields (!empty()) is likely returning false incorrectly, or the variables $username, $password, etc., are not actually populated in the superglobal $_POST.

Debugging Variable Population

When you inspect the page source after submitting the form with filled data, check if the POST variables exist. Sometimes, JavaScript on the client side (though none is shown here) or server-side sanitization in core.inc.php might be clearing $_POST before your script runs.

A more robust pattern for handling registration logic avoids deep nesting which obscures control flow. Here is a cleaner approach that separates concerns:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Ensure we have the data first
    if (isset($_POST['username'], $_POST['password'], $_POST['password_again'], 
            $_POST['firstname'], $_POST['surename'])) {
        
        $data = [
            'username' => $_POST['username'],
            'password' => $_POST['password'],
            // ... map other fields
        ];

        if (!empty($data['username']) && !empty($data['password']) 
            && !empty($data['firstname']) && !empty($data['surename'])) {
            
            // Proceed with validation logic (e.g., password match)
            if ($data['password'] === $data['password_again']) {
                // Success path
            } else {
                echo 'Passwords do not match';
            }
        } else {
            echo 'All fields are required';
        }
    } else {
        // Handle case where POST data is missing entirely
        // This often happens if the form didn't submit or was submitted without a body
    }
} elseif (loggedIn()) {
    echo 'you \'re already logged in';
}

// If neither condition matches, we are on an empty GET request (form not submitted)

Handling HTTP_REFERER Safely

Regarding the initial warning about HTTP_REFERER, you should never access it directly without a guard. Use the null coalescing operator or isset() to provide a default value:

$http_referer = $_SERVER['HTTP_REFERER'] ?? 'Direct Access';
// Or strictly checking existence before use if strict error reporting is on
if (isset($_SERVER['HTTP_REFERER'])) {
    $http_referer = $_SERVER['HTTP_REFERER'];
} else {
    $http_referer = null; // Handle the absence gracefully
}

This prevents the Notice: Undefined index warning. If you are using Laravel, the framework handles superglobals internally and often abstracts this away, but in vanilla PHP or legacy frameworks, defensive coding is mandatory.

Summary of Fixes

  1. Guard HTTP_REFERER: Always check isset($_SERVER['HTTP_REFERER']) before accessing it to avoid notices.
  2. Simplify Logic Flow: Avoid nesting if statements inside else blocks when checking for login state. Separate the "Form Submitted" logic from the "Already Logged In" logic using $_SERVER['REQUEST_METHOD'].
  3. Verify Variable State: Ensure that $_POST is actually populated before attempting to check !empty().

By restructuring your conditional logic and safely accessing server variables, you will resolve both the validation errors and the notices without needing complex workarounds.

Our network