php/html - http_referer

倖福魔咒の 提交于 2019-12-01 17:06:51

问题


I am creating a website and on one particular page, am wanting to send the user back to the previous page. I am fairly new to PHP/HTML and have been using some existing code for ideas and help.

The existing code uses the following method:

if (! empty($HTTP_REFERER)) 
{
    header("Location: $HTTP_REFERER");
} else 
{
    header("Location: $CFG->wwwroot");
}

However, when I use this code the HTTP_referer is always treated as empty and the user redirected to the root page. Any obvious flaws in this code?


回答1:


You need to use:

$_SERVER['HTTP_REFERER']



回答2:


Don't rely on the HTTP Referrer being a valid or even non-empty field. People can choose to not have this set leaving any checks for that variable going to the empty side of the IF-ELSE clause.

You can guard against this by sending along a parameter in either the URL or POST parameters that would hold a value that you can use to redirect the user back to.




回答3:


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




回答4:


If you wanted to send the person back to the previous page and have it work regardless of the referrer being set correctly, you can append a GET parameter to the URL (or POST).. you will need to encode the URL.. Something like

http://www.domain.com.au/script.php?return=http%3a%2f%2fwww.domain.com.au%2fthis-is-where-i-was%2f

You can use PHP's urlencode() function.




回答5:


Also note that the referer header might be empty or missing anyway, so you shouldn't rely on it at all..




回答6:


You should use

$_SERVER['HTTP_REFERER']

However look at the register_globals configuration in php.ini, it should be turned off due to security reasons. You can read more on PHP Manual site.



来源:https://stackoverflow.com/questions/606288/php-html-http-referer

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