PHP:如何获取引用 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16374704/
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
PHP: How to get referrer URL?
提问by sorinu26
I have a page (only index) where customers can send me emails and I want to see from what website that visitor is coming from?
我有一个页面(只有索引),客户可以在其中向我发送电子邮件,我想查看访问者来自哪个网站?
How can I get Referrer URL
with PHP? I tried with
$_SERVER['HTTP_REFERER']
but it's empty all the time. How can I do this? Thank you!
我怎样才能Referrer URL
使用 PHP?我试过,
$_SERVER['HTTP_REFERER']
但它一直是空的。我怎样才能做到这一点?谢谢!
Here is what my $_SERVER
contains ("---- CENSORED ----" is censored by me)
这是我的$_SERVER
内容(“---- CENSORED ----”由我)
array(31) {
["DOCUMENT_ROOT"]=>
string(33) "/home/anything/public_html/design"
["GATEWAY_INTERFACE"]=>
string(7) "CGI/1.1"
["HTTP_ACCEPT"]=>
string(129) "text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1"
["HTTP_ACCEPT_ENCODING"]=>
string(13) "gzip, deflate"
["HTTP_ACCEPT_LANGUAGE"]=>
string(14) "en-US,en;q=0.9"
["HTTP_CACHE_CONTROL"]=>
string(8) "no-cache"
["HTTP_CONNECTION"]=>
string(10) "Keep-Alive"
["HTTP_COOKIE"]=>
string(189) "__utma=76630272.1468291432.1367655794.1367669576.1367674157.3; __utmb=76630272.1.10.1367674157; __utmc=76630272; __utmz=76630272.1367655794.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)"
["HTTP_HOST"]=>
string(25) " ---- CENSORED ---- "
["HTTP_USER_AGENT"]=>
string(57) "Opera/9.80 (Windows NT 6.1) Presto/2.12.388 Version/12.15"
["PATH"]=>
string(13) "/bin:/usr/bin"
["QUERY_STRING"]=>
string(0) ""
["REDIRECT_STATUS"]=>
string(3) "200"
["REMOTE_ADDR"]=>
string(10) "5.15.68.79"
["REMOTE_PORT"]=>
string(5) "57897"
["REQUEST_METHOD"]=>
string(3) "GET"
["REQUEST_URI"]=>
string(12) "/referer.php"
["SCRIPT_FILENAME"]=>
string(45) "/home/anything/public_html/design/referer.php"
["SCRIPT_NAME"]=>
string(12) "/referer.php"
["SERVER_ADDR"]=>
string(13) " ---- CENSORED ---- "
["SERVER_ADMIN"]=>
string(35) " ---- CENSORED ---- "
["SERVER_NAME"]=>
string(25) " ---- CENSORED ---- "
["SERVER_PORT"]=>
string(2) "80"
["SERVER_PROTOCOL"]=>
string(8) "HTTP/1.1"
["SERVER_SIGNATURE"]=>
string(189) "
Apache/2.2.19 (Unix) mod_ssl/2.2.19 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 Server at ---- CENSORED ---- Port 80
"
["SERVER_SOFTWARE"]=>
string(125) "Apache/2.2.19 (Unix) mod_ssl/2.2.19 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635"
["UNIQUE_ID"]=>
string(24) "UYUNcUPeiDsAABkR2eYAAAAj"
["PHP_SELF"]=>
string(12) "/referer.php"
["REQUEST_TIME"]=>
int(1367674225)
["argv"]=>
array(0) {
}
["argc"]=>
int(0)
}
回答by Ejaz
$_SERVER['HTTP_REFERER']
will give you the referrer page's URL if there exists any. If users use a bookmark or directly visit your site by manually typing in the URL, http_referer will be empty. Also if the usersare posting to your page programatically (CURL) then they're not obliged to set the http_referer as well. You're missing all _
, is that a typo?
$_SERVER['HTTP_REFERER']
如果存在,会给你推荐页面的 URL。如果用户使用书签或通过手动输入 URL 直接访问您的站点,则 http_referer 将为空。此外,如果用户以编程方式 (CURL) 发布到您的页面,那么他们也没有义务设置 http_referer。你错过了所有_
,这是一个错字吗?
回答by bearfriend
Underscore. Not space.
下划线。不是空间。
$_SERVER['HTTP_REFERER']
$_SERVER['HTTP_REFERER']
回答by 5ervant
If $_SERVER['HTTP_REFERER']
variable doesn't seems to work, then you can either use Google Analyticsor AddThis Analytics.
如果$_SERVER['HTTP_REFERER']
变量似乎不起作用,那么您可以使用Google Analytics或AddThis Analytics。
回答by habib
$_SERVER['HTTP_REFERER'];
But if you run a file (that contains the above code) by directly hitting the URL in the browser then you get the following error.
但是,如果您通过直接在浏览器中点击 URL 来运行文件(包含上述代码),则会出现以下错误。
Notice: Undefined index: HTTP_REFERER
注意:未定义索引:HTTP_REFERER