How to check if a request if coming from the same server or different server?

How to check if a request if coming from the same server or different server?

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

Stefan Bogdanescu

Founder & Senior Architect

Distinguishing Internal Traffic from External Requests in PHP

When building a backend system for a domain like www.domain.com, you often need to differentiate between legitimate internal requests and external attempts to access your processing logic. This requirement typically arises when hosting form handlers or administrative scripts that must only execute if the request originates from the same server infrastructure, while rejecting traffic coming from other domains.

The most direct method to achieve this in a PHP environment is inspecting the HTTP_HOST header combined with logic that validates the source of the request relative to your internal routing rules. However, simply checking if the host matches your domain name isn't always sufficient if you are running multiple services on different servers within the same infrastructure or using load balancers that modify headers.

To enforce a strict rule where processing occurs only if the request is "sent from within the domain" (implying it originates from the same server or an internal proxy), you must rely on specific server variables that reveal the origin of the HTTP request. The HTTP_HOST header is set by the client browser and reflects the URL typed into the address bar. If a user visits www.domain.com, the header will contain that value. If they are accessing your script via an internal IP or a local port on the same machine, the browser might not send the request in the standard way unless you are specifically routing it through a reverse proxy or a specific endpoint designed for internal consumption.

A more robust approach involves checking the REMOTE_ADDR or looking for specific headers injected by your web server configuration when traffic comes from an internal network segment. If your infrastructure consists of multiple servers handling different parts of the application, relying solely on the domain name is risky because users might reach your internal endpoints via public IPs if not properly restricted.

Implementing Host Validation in PHP

In a standard LAMP or LEMP stack, you can validate the request source by comparing the HTTP_HOST against a whitelist of known internal hosts and the public domain. Here is a practical example of how to gate logic based on this:

<?php
$allowedInternalHosts = ['localhost', '127.0.0.1'];
$publicDomain = 'www.domain.com';

$requestHost = $_SERVER['HTTP_HOST'] ?? null;

// If the request is not from a known internal host or the public domain, discard it
if ($requestHost === null || !in_array($requestHost, array_merge([$publicDomain], $allowedInternalHosts))) {
    // Log the attempt and exit
    error_log("Unauthorized external access attempt to script.");
    exit('Access Denied');
}

// Proceed with processing only if the host check passes
echo "Processing request from: $requestHost";
?>

This code snippet ensures that if a request comes from an unexpected domain, the script terminates immediately. However, this approach assumes that the HTTP_HOST header is unmodified. In environments using load balancers or reverse proxies like Nginx or Apache, these appliances often strip or normalize headers before passing them to the backend PHP-FPM process.

The Role of Reverse Proxies

If you are running multiple servers where some handle public traffic and others handle internal processing, you might be forwarding requests internally via a specific port that browsers never see directly. In such cases, checking HTTP_HOST alone fails because the browser sent the request to the load balancer's public IP, not the internal server's private IP or localhost.

To accurately determine if a request is coming from within your own server cluster versus an external domain, you should inspect the X-Forwarded-Host header or configure your web server to pass the original host through a trusted proxy marker. Alternatively, if you have control over the internal routing, you can force internal requests to include a specific query parameter or header that external browsers cannot easily spoof without knowing your internal architecture.

For high-security environments where you strictly require traffic to originate from the same physical or logical server instance (bypassing public DNS resolution entirely), checking the REMOTE_ADDR against your own internal IP range is essential. If your web server runs on an internal interface, external requests will arrive with a public IP address, whereas internal traffic might show up as 127.0.0.1 or a private RFC 1918 address like 10.x.x.x.

Trade-offs and Security Considerations

Relying on domain name matching has a significant limitation: it is susceptible to Host header injection attacks if your web server configuration allows arbitrary host values in the header. Always ensure your Nginx or Apache configuration explicitly sets the server name, preventing malicious clients from spoofing the HTTP_HOST.

Furthermore, if you are using Laravel or similar frameworks, you should leverage the framework's request validation tools rather than raw superglobals. For instance, in Laravel, you can check the host against a trusted list within your middleware:

use Illuminate\Http\Request;

class InternalOnlyMiddleware
{
    protected $trustedHosts = ['www.domain.com', 'localhost'];

    public function handle(Request $request, Closure $next)
    {
        if (!in_array($request->host(), array_merge([$this->trustedHosts], config('app.trusted_hosts')))) {
            abort(403);
        }
        return $next($request);
    }
}

This pattern centralizes the logic and makes it easier to update trusted hosts without modifying every controller. Remember that "coming from the same server" is often a proxy for "coming from an internal network segment." If your goal is to prevent cross-origin form submissions or unauthorized access, ensure you are not accidentally trusting headers that could be manipulated by clients on different machines trying to mimic internal traffic.

Our network