How to get the http referer in laravel?
Asked Answered
L

7

34

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.

Lorenalorene answered 16/8, 2017 at 12:21 Comment(1)
Be aware - the referrer is subject to the client announcing it truthfully... it is not reliable in a "true" sense.Workhouse
C
47

Laravel way of getting referer:

request()->headers->get('referer');
Commeasure answered 11/12, 2019 at 9:56 Comment(0)
B
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

Baroscope answered 16/8, 2017 at 12:26 Comment(5)
I've tried that out but this returns allways "null"Lorenalorene
@Lorenalorene Then there probably isn't a referer sent.Classify
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.Baroscope
mhh.. so what would you do, to check if the user klicked a link like: "mywebsite.com/unicorn/1111111 -- Without coming from my website itself? So I can give the "thread" with the id 1111111 some extra clicksLorenalorene
404 on the link, here is the good one: laravel.com/api/8.x/Illuminate/Http/Request.html#method_serverBloat
B
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
Bondsman answered 12/2, 2021 at 13:53 Comment(0)
I
6

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();
Ier answered 3/8, 2022 at 9:29 Comment(0)
K
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"];
Katanga answered 16/8, 2017 at 12:32 Comment(2)
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 chat.whatsapp.com -- strangeLorenalorene
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.Lorenalorene
A
3
Route::get('/send', function (Request $request) { 
    echo $request->headers->get('referer');
});
Alcyone answered 22/3, 2022 at 13:38 Comment(0)
S
-1

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://mcmap.net/q/450788/-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
);
Summertime answered 4/12, 2019 at 8:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.