php/html - http_referer

瘦欲@ 提交于 2019-12-01 18:08:21

You need to use:

$_SERVER['HTTP_REFERER']

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.

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

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.

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

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.

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