开发者

how to check if a url is typed or coming from a click in php?

is there a way for php to tell if an address is directly typed by user or coming from a click or any other method that in the end will result a php-generated page?

this question is purely out of curiosity so the urgency is very, very low. but thank开发者_StackOverflow中文版 you if you want to spare sometime to answer. :D


You could use $_SERVER['HTTP_REFERER']. It should be set to the page that referred the user to your page. If the user typed the address in, it will be empty.

However, beware, it is not reliable, and can be easily modified by the user. As the PHP doc says, you can't really trust it.


In general, you can't.

0) typed (or copy-pasted!) links - REFERER will be empty

1) links clicked on a webpage - REFERER will be set

2) links clicked in an email client (not web based like gmail) - REFERER will be empty

3) links loaded as a home page - REFERER will be empty

4) links loaded from bookmarks - REFERER will be empty

So using PHP $_SERVER['HTTP_REFERER'] variable you can only distinguish case 1 from all the other cases...


You can look at the http_referrer, for a webpage loaded by directly typing in address bar it should not have any referrer but the page that was loaded by some click will have a referrer.


Using http_referrer for this purpose is very unreliable. HTTP_REFERRER is empty when the user types the URL in some browsers. It is NULL in Chrome 19, it is NOT NULL in IE8.


$direct = (bool)$_SERVER['HTTP_REFERER'];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜