$_SERVER['HTTP_REFERER'] How can I match it against all possibilities of a single domain?
This is probably quite hard to explain, so I'll try to make it as simple as possible:
Here's my code:
<?php
$ref = $_SERVER['HTTP_REFERER'];
switch($ref) {
case "http://www.facebook.com":
$ref_name = "Facebook";
break;
case "http://www.twitter.com":
$ref_name = "Twitter";
break;
}
?>
From what I know, HTTP_REFERER
pulls the entire referrer url (e.g. www.facebook.com/abc/xyz/mno=prq
) as oppose to the top-level domain. I'd like to be able to match $ref
against something so that all referrer's whether from say http://static.facebook.com
(a sub-domain) o开发者_JAVA百科r http://www.facebook.com/profile_id/bla
(a url with additional folders and parameters after the top-level domain) are given the value of "http://www.facebook.com"
.
What's the most simple and effective way to do so?
Any comments/answers etc will be greatly appreciated :)!!
See: parse_url
$ref = 'http://static.facebook.com';
$host = implode('.', array_slice(explode('.', parse_url($ref, PHP_URL_HOST)), -2));
switch ($host) {
case 'facebook.com':
break;
case 'twitter.com':
break;
}
Update: Have a look at Root Zone Database if you're dealing with special TLDs.
精彩评论