php Laravel 5.4 获取 Referer
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45713409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.4 get Referer
提问by PHPprogrammer42
I'm trying to get the Referer of my users. Like if they come from facebook, youtube, google or anything else.
我正在尝试获取用户的 Referer。就像它们来自 facebook、youtube、google 或其他任何东西一样。
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.
我需要它,因为我想检查用户是否来自某个特定的 URL,如果是,我想给用户一些额外的“点击”来排名。像一个小的附属系统。
回答by Thibault Dumas
It seems like this will do what you are looking for :
看起来这会做你正在寻找的:
Request::server('HTTP_REFERER').
You can read the Api DOC here :
您可以在此处阅读 Api DOC:
http://laravel.com/api/5.0/Illuminate/Http/Request.html#method_server
http://laravel.com/api/5.0/Illuminate/Http/Request.html#method_server
回答by Rejneesh Raghunath
Proper way is to use
正确的方法是使用
use Illuminate\Http\Request;
return request()->headers->get('referer');
回答by Jerodev
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.
你得到的原因Undefined index: HTTP_REFERER
是不是所有的请求都有HTTP_REFERER
标头,只有来自其他网站的大部分请求。如果您直接使用 url 访问网站,则不会发送HTTP_REFERER
标头。
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"];
回答by Syed Mobarak
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/.
我希望这条消息能让您对自己的代码感到满意。但是,如果您在引用 URL 方面遇到任何问题,您可能应该阅读这篇文章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.
Referrer Policy 标头是此类问题背后的关键。我在我的 Laravel + NGINX 应用程序中遇到了引用 URL 的问题,并尝试了所有可能的方法来通过上面评论中提到的代码(返回、上一个、标题等)来解决这个问题,但最终发现这篇文章在解决我的问题之前丰富了我的知识适当地。
回答by lofihelsinki
It's up to the client to send the referrer information int he HTTP request's header.
由客户端在 HTTP 请求的标头中发送引用信息。
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.
因此,如果客户端(浏览器、应用程序等)不发送引荐来源网址,则请求标头中的服务器将无法使用它。我猜 WhatsApp 应用程序已禁用发送引荐来源网址,因此无法获得它。
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.
在某些浏览器中,可以在设置中或使用扩展程序关闭引荐来源信息的发送。curl
例如,它也很容易被欺骗,因此不能将其作为安全措施。
More info in my answer here: https://stackoverflow.com/questions/49050268/does-document-referrer-equal-the-http-referer-header/49050494
我在这里回答的更多信息: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):
如果您不确定客户端发送引荐来源网址,可以使用 JavaScript 轻松检查它(引荐来源网址在此处的拼写与 HTTP 标头不同,以增加混淆):
console.log(
document.referrer
);