Redirects and Referers

这一生的挚爱 提交于 2019-12-05 23:53:04

Sorry, but it's out of your control, only the browser can send that header - and not all do. It can be easily faked, so don't rely on it.

More information is available on this php bug (which was marked not a bug).

Since HTTP_REFERER is not trustworthy (could easily be modified from outside), you could easily store the last page visited in session after every request. Then easily retrieve it when reloading.

Meaning, loading the referrer as $referring_url = $_SESSION["referring_url"]. Then saving it $_SESSION["referring_url"] = $current_absolute_url; when ending each request.

Though, note that this could be a concurrency issue. Having parallel requests (using eg AJAX) could easily make the session believe that it came from a page that it actually didn't.

Retrieving the absolute path of the current request

This question has popped up a number of times on SO (I can't find any dupes right now though), and I think always with the bottom line that it is not defined in a standard what a browser is to set the referrer to in this situation.

Is it an option to specify it explicitly using a GET parameter or something?

I would suggest something like this:

header('Location: end.php?from=' . urlencode($_SERVER['PHP_SELF'));

And then in the page where you want to know from where the redirect was issued:

echo 'redirected from ' . urldecode($_GET['from']);

What are you trying to use the Referrer header info for?

If it's for authentication/validation of the request's origin, then you might consider trying another approach. As El Yobo pointed out, the Referrer header isn't a foolproof way of determining where someone's request is coming from.

I went with the old meta refresh method of redirection. This keeps the referring URL in tact for the vendors that require it. Any vendor that doesn't require it still uses the header function, for speed and ease of use.

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