Result from $_SERVER['HTTP_REFERER'], when referer header is not sent to server

落爺英雄遲暮 提交于 2019-12-05 12:16:39

问题


When the browser sends header info to the server, $_SERVER['HTTP_REFERER'] should give us the previous page URL right?

What returns from $_SERVER['HTTP_REFERER'], when header info is not sent to server? empty string? false? null? or... ?


回答1:


If the HTTP referer request header is not sent then the $_SERVER['HTTP_REFERER'] is probably not set, although it could be an empty string. Whether it is set or not in this case could depend on the server.

As with all HTTP request headers, check for its existence when reading:

$httpReferer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;



回答2:


$_SERVER['HTTP_REFERER'] is not really reliable because particular setting on the user browser could break it. But yes it should contain the previous URL and it will return empty string or NULL when headers are not sent, depending on the server configuration.




回答3:


$_SERVER is a global array variable, and the referrer value is an element of the array with key HTTP_REFERER. If is no referrer header is sent by the browser, then the element is simply missing from the array. You can check whether an array has an element with array_key_exists; in this case:

array_key_exists('HTTP_REFERER', $_SERVER)



来源:https://stackoverflow.com/questions/13948267/result-from-serverhttp-referer-when-referer-header-is-not-sent-to-server

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