Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How do I check the domain name that has been used to access my server using PHP?

I have several domains that I have changed the name servers for and now I want to be able to check the domain that is hitting my server?

Would be grateful for links to read up on this sort of thing and how to test.

Thanks all

share|improve this question
1  
DNS servers do not "hit" servers. An "nslookup" can be used to query a DNS server. –  user1797 Aug 27 '09 at 10:39

3 Answers 3

I have done something like the following (which redirected depending upon the requesting URL but you can do whatever you need to do)

<?php
     $url = $_SERVER['HTTP_HOST'];

     header("Content-type: text/html");
     // depending upon which 'Host' was in the URL,
     // we redirect to different places or nowhere at all
     if ( $url == 'www.example.org.uk'
                || $url == 'www.example.me.uk' ) {
             header("Location: http://www.example.co.uk/");
     }
     else if ( $url == 'www.example.net'
            || $url == 'www.example.org' ) {
             header("Location: http://www.example.com/");
     }
     // otherwise default
?>
share|improve this answer

I don't think you can tell which name server was used by the client if that is what you are asking. You can only see what the domain was that they requested because it is in the HTTP header. Doing this with PHP should probably be on stackoverflow.

If you are running Apache as your webserver, you can just look at the access_log file, which would be in something like /var/log/httpd/access_log to see which domain they are requesting.

share|improve this answer

Unless I am not understanding the question correctly, you can easily check for what domain is hitting your server using:

$_SERVER['SERVER_NAME']

Does seem more appropriate for Stack Overflow though...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.