How to get the http referer in laravel?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: A Comprehensive Guide on Retrieving HTTP Referrer in Laravel Applications Introduction In today's digital world, understanding where your users are coming from can be valuable for various purposes such as marketing strategies or anti-bot measures. In Laravel applications, you might want to retrieve the HTTP referrer information to gather this data. This blog post aims to guide you through different methods and approaches to retrieve the referrer in a reliable manner using Laravel. 1. Using the Request Object The simplest way to get the HTTP referrer is by accessing the request object's headers. As shown in the code snippet below, it is done using the `request()->headers()` method and specifying 'referer'. ```php $referrer = $this->request->headers->get('referer'); ``` However, this approach might not work for all cases. If the referrer is missing or invalid, an exception would be thrown due to the absence of the 'referer' key in the array returned by `request()->headers()`. 2. Using the $_SERVER Superglobal Array Another way to get the HTTP referrer is through accessing the $_SERVER superglobal array. This array holds various server variables and information about the current request. The referrer can be obtained using the 'HTTP_REFERER' key from this array. ```php $referrer = $_SERVER['HTTP_REFERER']; ``` While this method seems straightforward, it may also give undesired results if the HTTP_REFERER key is undefined or empty in the $_SERVER array. Additionally, some browsers send an empty referrer when navigating with the back button or other methods which do not involve a redirect, thus making this approach less reliable for tracking user navigation. 3. Using Session Variables To tackle the limitations of the previous two approaches, you can use session variables to persistently store the referrer information. By setting the referrer value in a session variable during every request and checking it on subsequent requests, you can ensure that no matter what happens, the referrer will be available when needed. ```php session_start(); // Set the referrer in session variable if not already set if (!isset($_SESSION['origURL'])) { $_SESSION['origURL'] = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null); } $referrer = $_SESSION['origURL']; ``` This method is more robust and reliable than the previous approaches but still has some drawbacks. The additional round trip to the database and potentially higher memory usage may not be desirable in certain situations. 4. Combining Session Variables with Request Object To improve upon the previous approach, you can combine session variables with the request object to get the best of both worlds. This method ensures that the referrer information is available even when there is no valid HTTP_REFERER key in $_SERVER. ```php $requestReferer = $this->request->headers->get('referer'); $serverReferer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null; session_start(); // Set the referrer in session variable if not already set or use request object if available if (!isset($_SESSION['origURL'])) { $_SESSION['origURL'] = $requestReferer ?? $serverReferer; } $referrer = $_SESSION['origURL']; ``` This approach is a good middle ground and works in most scenarios. However, if your application requires 100% accuracy for referrer information, it may still be subject to some edge cases where the referrer appears as null or an invalid value. Conclusion In summary, there are various ways to retrieve the HTTP referrer in Laravel applications. Using a combination of the request object and session variables would provide a robust solution that covers most situations. Remember to always consider the context of your application before choosing an approach to ensure the best possible outcome for both functionality and performance.