0

I want to check the Referrer url is Main Domain or Sub domain, domain can be anything like .com , .net ,.co.in , in .. Sub Domain also can be different type… any one can suggest idea in PHP script …..

For e.g if referrer url is http://google.com is the Domain, http://in.google.net is the sub domain

  • Do you want to check against a list of domains (e.g. something like `if ( referrer_is('*.google.*') { ... }`) or extract a string from the domain (e.g. `echo get_referrer_domain_string()` giving you `google'`)? – IMSoP Sep 28 '13 at 05:02
  • No.. I want to indentify request is from domain Or subDomain ? – Mehul Prajapati Sep 28 '13 at 05:55
  • Perhaps you could give some examples of input and the output you want for each? – IMSoP Sep 28 '13 at 05:58
  • Guss the Referrer Url is **www.abc.com** so its a domain write ? and if the Referrer url is **www.mysubdomian.abc.com** its call sub domain .. write ? i want to identify the domain type, means Domain or sub domain..... – Mehul Prajapati Sep 28 '13 at 06:42
  • Sub-domain is a relative term: technically, `.com` is a "top-level domain", "example.com" is "a sub-domain of .com", and "www.example.com" is a "a sub-domain of example.com". So "co.uk" is a sub-domain of ".uk", but in practical terms is considered a "public suffix" because different people own the different sub-domains under it. This is a tricky rule to codify; one notable attempt is [the Mozilla Public Suffix List](http://publicsuffix.org/). – IMSoP Sep 28 '13 at 12:44
  • The Public Suffix List site led me back to StackOverflow. You might want to check out this question: http://stackoverflow.com/questions/288810/get-the-subdomain-from-a-url – IMSoP Sep 28 '13 at 12:47
  • IMSoP, Thanks For Reply, But i want PHP Script that can identify Url is Subdomain or Domain ? – Mehul Prajapati Sep 30 '13 at 05:20
  • Then you will need to work out exactly what you want "sub-domain" to mean... – IMSoP Sep 30 '13 at 08:14

2 Answers2

0

This should do the trick. (Haven't tested the code - so please take as a suggestion only)

<?php
$fromOtherDomain = !preg_match('/^https?:\/\/' . preg_quote($_SERVER['HTTP_HOST']) . '/i', $_SERVER['HTTP_REFERRER']);
?>
Bas Kuis
  • 748
  • 1
  • 7
  • 20
  • Bas Kuis , Thanks for the answer.. But i want to track the referrer url is Domain Or Sub Domain... the referrer url can be any from the world... – Mehul Prajapati Sep 28 '13 at 06:00
0

I got the Answer.
The requirement has changed and I now match all subdomains belonging to same domain (*.example.com).
So I have used the following code:

if(!preg_match('/[A-Za-z0-9].example.com/', $_SERVER['HTTP_REFERER'])){
    echo 'Sub Domain';
}else{
    echo 'Main domain OR sub domain not belongs to example.com'
}
Bram Hammer
  • 363
  • 4
  • 21