php/html -http_referer
-
03-07-2019 - |
문제
웹 사이트를 만들고 있는데 특정 페이지에서 사용자를 이전 페이지로 다시 보내려고합니다. 저는 PHP/HTML을 처음 접했으며 아이디어와 도움을 위해 기존 코드를 사용하고 있습니다.
기존 코드는 다음 방법을 사용합니다.
if (! empty($HTTP_REFERER))
{
header("Location: $HTTP_REFERER");
} else
{
header("Location: $CFG->wwwroot");
}
그러나이 코드를 사용하면 http_referer는 항상 비어 있고 사용자는 루트 페이지로 리디렉션됩니다. 이 코드에서 명백한 결함이 있습니까?
해결책
사용해야합니다.
$_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.