Laravel 5.4 get Referer

喜夏-厌秋 提交于 2019-12-18 07:43:49

问题


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.


回答1:


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

Request::server('HTTP_REFERER').

You can read the Api DOC here :

http://laravel.com/api/5.0/Illuminate/Http/Request.html#method_server




回答2:


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"];



回答3:


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.




回答4:


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
);



回答5:


Proper way is to use

use Illuminate\Http\Request;

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


来源:https://stackoverflow.com/questions/45713409/laravel-5-4-get-referer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!