Address bar input vs. Link
Is there a way to interrogate a request to determine the method of navigation ?
(开发者_如何学编程using a LAMP configuration, PHP)
In other words... I would like to know whether the entry was hand typed into the address bar, or a link/bookmark was used.
You can check the $_SERVER['HTTP_REFERER']
variable which will contain the URL the person has navigated from. I'm not sure what happens if they choose a bookmark or enter the URL manually, though I suspect that this variable would be blank.
Though, as it says in the manual:
The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.
Reliably, you can not.
One method you could try is referer headers (which is how it's spelled in the spec), usually sent by the browser to indicate where the user is coming from. In PHP, this is available in $_SERVER['HTTP_REFERER']
.
Most modern browsers (I've tested Google Chrome and Firefox) send a referer header on link clicks and not on direct entry - that is, if the user's settings say to, which they usually do by default.
Another option, depending on just how much you really care, is to append some sort of randomly-generated session variable to the URL in the link and, on receiving a request, see if the token in the URL matches the token in the user's session. If so, there you go. If not, this link was copy-pasted.
Of course, all of these methods can easily be beaten, and your server can be fooled. Don't depend on them.
If they click a link the browser will set a referrer, you could detect the presence of a referrer header by using
$_SERVER['HTTP_REFERER']
and checking to see if that has been set. If it's blank the person has either typed in the URL or has a browser extension to clear referrers. It's about as close as you'll get to a solution on this one.
About all you can do is look at the $SERVER['HTTP_REFERER']
[sic]. If you were directly linked, there is a good chance you will find the URL of the page that linked to you there. There are many circumstances where you won't get it though, so you can't rely on it.
In IE it is possible to detect when you are loaded from a bookmark, if you previously used a saveFavorite behaviour to persist some data before the user bookmarked you. But it's pretty ugly.
精彩评论