How to get the previous url using PHP

How to get the previous url using PHP

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

Stefan Bogdanescu

Founder & Senior Architect

How to Get the Previous URL Using PHP

When a user clicks a hyperlink on an external site like a blog, Yahoo, or even Stack Overflow and lands on your application, you often need to know where they came from. This information is crucial for analytics, referral tracking, and preventing spam bots that might be scraping your content without coming from legitimate sources. In PHP, the mechanism to retrieve this data relies on the HTTP Referer header.

The server automatically sends a Referer header to your script when a client requests your page via a direct link or an embedded image. You can access this value in PHP using the superglobal array $_SERVER. Specifically, the variable $_SERVER['HTTP_REFERER'] contains the URL of the page that linked to the current document.

Here is a basic example of how to check for and display the referrer:

<?php
if (!empty($_SERVER['HTTP_REFERER'])) {
    echo "You came from: " . $_SERVER['HTTP_REFERER'];
} else {
    echo "The visitor arrived via direct link, bookmark, or a non-HTTP client.";
}

It is vital to handle cases where this header is missing. A user can navigate directly to your site using the address bar, type a URL into a browser, or access it via an API call that doesn't send headers. In these scenarios, $_SERVER['HTTP_REFERER'] will be empty. Your code must account for this to avoid breaking functionality or displaying confusing error messages.

However, relying solely on $_SERVER['HTTP_REFERER'] has significant security and accuracy limitations. The Referer header is not standardized strictly by the HTTP protocol; it is a convention that most modern browsers follow but can be disabled. Users often have privacy settings in their browsers that strip this information entirely to prevent tracking. Additionally, some corporate proxies or mobile network operators modify or remove the header.

From a security perspective, you cannot trust the value of $_SERVER['HTTP_REFERER'] for authentication or authorization logic. A malicious user can easily craft a request using tools like cURL or Postman with curl -H "Referer: https://trusted-site.com". They can make your script believe they came from a legitimate partner site just by setting the header manually. Never use this value to determine if a user has permission to access specific resources.

If you are building a modern PHP application using the Laravel framework, handling this data is often part of a larger request lifecycle. While $_SERVER['HTTP_REFERER'] is available in standard PHP scripts, Laravel provides a cleaner abstraction through the request() helper or the Request facade. You can access the referrer via $request->header('referer'). This approach decouples your logic from direct superglobal array access, making your code more testable and consistent across different environments.

<?php
use Illuminate\Http\Request;

$request = app(Request::class);

if ($referrer = $request->header('referer')) {
    // Store for analytics or logging
    Log::info("Referral source: {$referrer}");
}

In summary, while PHP provides a straightforward way to inspect the previous URL through the HTTP_REFERER header, developers must treat this data as informational only. It is excellent for displaying "Back to [Source]" buttons or populating analytics dashboards, but it should never influence security decisions or access controls due to its malleability and reliance on client configuration.

Our network