get the page a user viewed before current
instead of
login.php?ref=http://mysite.com/lastpage
could I get the before page somehow else? tried HTTP_REFERER but cant get it to work开发者_开发问答
Notice: Undefined index: HTTP_REFERER in C:\test\login.php on line 18
// Newbie
HTTP_REFERER
(and almost every other $_SERVER var) are not a reliable means of obtaining previous pages as they can be easily faked.
Setting a session variable could be slightly safer but will not work if they came from an external url. Afaik there is not a fool-proof method of accomplishing this.
The HTTP_REFERER is not always set, that's why you're getting the notice. The browser may opt not to send it or the user may have entered the URL directly into his browser. You can put that value into the session, which has other drawbacks (multiple tabs in the browser have the same referrer page, for example.) I think the url is still the best way to go, if you don't have a form, which you can append a hidden field to.
EDIT By the way: don't use the URL in the get parameter blindly, that can be faked again by others as explained in the answers to this other question.
Use $_SERVER['HTTP_REFERER'] like this:
$ref=$_SERVER['HTTP_REFERER']
精彩评论