'How to get the http referer in laravel?
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.
Solution 1:[1]
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
Solution 2:[2]
Laravel way of getting referer:
request()->headers->get('referer');
Solution 3:[3]
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
Solution 4:[4]
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"];
Solution 5:[5]
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.
Solution 6:[6]
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
);
Solution 7:[7]
Route::get('/send', function (Request $request) {
echo $request->headers->get('referer');
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | feeela |
| Solution 2 | leek |
| Solution 3 | |
| Solution 4 | Jerodev |
| Solution 5 | Syed Mobarak |
| Solution 6 | lofihelsinki |
| Solution 7 | ???? ???? |
