31

I'm trying to get the Referer of my users. Like if they come from facebook, youtube, google or anything else.

Now I've tried something like that:

$referrer = $this->request->headers->get('referer');
$url = $referrer ? $this->to($referrer) : $this->getPreviousUrlFromSession();
return $url ?: $this->to('/'); // returns: Method referer does not exist.

This:

return $_SERVER["HTTP_REFERER"] // returns Undefined index: HTTP_REFERER

that:

session_start();
    
if ( !isset( $_SESSION["origURL"] ) ) {
    $_SESSION["origURL"] = $_SERVER["HTTP_REFERER"]; // returns Undefined index: HTTP_REFERER
}

But nothing worked like expected.

Does someone know a solution how I can check the referer?

I need that because I want to check if the user comes from some specific URL's and if so, I want to give the user some extra "clicks" to rank up. Something like a small affiliate system.

Pierre
  • 1,129
  • 2
  • 16
  • 30
PHPprogrammer42
  • 355
  • 1
  • 3
  • 7
  • 1
    Be aware - the referrer is subject to the client announcing it truthfully... it is not reliable in a "true" sense. – wally Aug 16 '17 at 12:27

8 Answers8

42

It seems like this will do what you are looking for :

Request::server('HTTP_REFERER').

You can read the Api DOC here :

https://laravel.com/api/8.x/Illuminate/Http/Request.html#method_server

feeela
  • 29,399
  • 7
  • 59
  • 71
Thibault Dumas
  • 1,060
  • 2
  • 10
  • 21
  • 1
    I've tried that out but this returns allways "null" – PHPprogrammer42 Aug 16 '17 at 12:37
  • @PHPprogrammer42 Then there probably isn't a referer sent. – Ian Aug 16 '17 at 12:38
  • 3
    This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted. – Thibault Dumas Aug 16 '17 at 12:43
  • mhh.. so what would you do, to check if the user klicked a link like: "http://mywebsite.com/unicorn/1111111 -- Without coming from my website itself? So I can give the "thread" with the id 1111111 some extra clicks – PHPprogrammer42 Aug 16 '17 at 12:48
  • 1
    404 on the link, here is the good one: https://laravel.com/api/8.x/Illuminate/Http/Request.html#method_server – Romain 'Maz' BILLOIR May 09 '21 at 20:48
38

Laravel way of getting referer:

request()->headers->get('referer');
Rejneesh
  • 1,574
  • 16
  • 16
7

You can use the request() helper:

request()->headers->get('referer');

or the Request facade:

Request::header('referer');
Request::server('HTTP_REFERER'); // same thing as above
Pierre
  • 1,129
  • 2
  • 16
  • 30
3

The reason you get Undefined index: HTTP_REFERER is because not all requests have a HTTP_REFERER header, only most of the requests that come from other websites. If you visit a website directly with the url, you wont be sending a HTTP_REFERER header.

So, you should check if the header is set first.

if (!isset($_SESSION["origURL"]) && array_key_exists('HTTP_REFERER', $_SERVER))
    $_SESSION["origURL"] = $_SERVER["HTTP_REFERER"];
Jerodev
  • 32,252
  • 11
  • 87
  • 108
  • well that works fine if I come from facebook. I mostly want to check if the user comes from whatsapp. If I post the URL that redirects to this code, I only get "null". Maybe it's because whatsapp is a app, not a website. But if I look into my logs, I can see that the referer is https://chat.whatsapp.com/ -- strange – PHPprogrammer42 Aug 16 '17 at 12:45
  • I get log files of a specific statistic programm that I use on my website. There I can see some user data like IP, browser, referer etc. – PHPprogrammer42 Aug 16 '17 at 12:56
3

We can use url() helper

url()->previous();

If you want to go back to the page that initialise the request, we simply write this (usually in the controller):

return redirect()->back();
LucianDex
  • 469
  • 2
  • 12
1

I hope this message finds you happy with your code. However, if you are facing any issue with referrer URL, you probably should read this article https://scotthelme.co.uk/a-new-security-header-referrer-policy/.

The Referrer Policy header is the key behind such an issue. I have been facing problem with referrer URL in my Laravel + NGINX application and tried all possible ways to fix that by code mentioned in above comments (back, previous, header etc.) but finally found this article to enrich my knowledge before fixing my issue appropriately.

1
Route::get('/send', function (Request $request) { 
    echo $request->headers->get('referer');
});
0

It's up to the client to send the referrer information int he HTTP request's header.

So if the client (browser, app, etc.) doesn't send the referrer, it won't be available to the server in the request headers. I guess the WhatsApp app has disabled sending the referrer, so there is no way to get it.

In some browsers, sending of the referrer information can be turned off in the settings or with a extension. It is also easily spoofed with curl for instance, so it cannot be relied on as a security measure.

More info in my answer here: https://stackoverflow.com/questions/49050268/does-document-referrer-equal-the-http-referer-header/49050494

If your unsure about the client sending the referrer, it's easy to check it with JavaScript (referrer is spelled here differently from the HTTP header, to add to the confusion):

console.log(
    document.referrer
);
lofihelsinki
  • 2,491
  • 2
  • 23
  • 35