PHP GetHostByName for non-existent domains returns server IP

Discussion in 'Programming/Scripts' started by Craig, Aug 27, 2009.

  1. Craig

    Craig New Member

    I can dig from the centos command line and DNS queries for non-existent domains return the expected results, basically nothing.

    But, if I use PHP functions, the IP of the server is returned.

    Also, if I try to use PHP Pear's HTTPRequest library to fetch a page from a non-existent domain, the root page of the server is again returned.

    I haven't tried PHP's cURL yet but I would guess that is the same.

    Googling this brings up a number of people who have had the same problem over the years, going back to 2001 but there is NOT ONE SINGLE mention of a solution.

    I thought that might mean that those with the problem "simply" reinstalled everything and it all just worked but I tried that, just now, it didn't work. :(

    I'm running Centos 5.3 with ISPConfig v.2 running and other than this PHP problem, it is all working fine.

    Any help would be greatly appreciated!

    I should add that a dev server running from the same NAT reacts correctly so it seems that it is something in the way the problem server is configured although all the network configuration was done using the GUIs so I don't think it could be a typo or something like that.

    I should also add that the IP address returned for non-existent domains is the server's external IP address, as opposed to its internal NAT address. I haven't found anywhere that the server's external IP is in any configuration files so PHP seems to be getting it through DNS one way or another.
    Last edited: Aug 27, 2009
  2. Craig

    Craig New Member

    More info about what is not the problem.

    Some references in Google mention permissions issues, namely /etc/resolv.conf not being accessible to user "apache" and therefore PHP can't find how to resolve things properly. The common test is to run a test script in CLI under user root to test this. This is not the problem because the test script also fails the same way, resolution of non-existent domains continue to point to the server's external IP address, when run as root in CLI.

    I thought that maybe there was a domain name conflict so I created a dummy domain name totally different than anything on the box and added it and the server's IP to my hosts file and everything ran as expected but, resolving non-existent domains still point to the server's external IP address.

    I also temporarily replaced the production server with a dev box, on which it works correctly, and the dev box continued to work correctly. This is probably redundant but worth the 5 minutes it took to test it.

    I'm at the point now of scripting a work around in PHP so that:

    1) The domain name to be resolved is first compared to the server name and if the same or one includes sub-domains of the site's domain, the site's external IP is returned.

    2) If they are not the same, gethostbyname() is then called on the query domain.

    3) If the call to gethostbyname() returns the site/server's IP, the test function returns the original query domain. This is what will happen on the box with broken DNS resolution in PHP and so the result of the function call is as it should be from a call to gethostbyname().

    4) If the call to gethostbyname() returns an actual IP address that is not the site/server's, it is returned, again, as it should be.

    5). If the call to gethostbyname() returns the query domain, as it should on a box with properly functioning PHP DNS resolution if the domain is a non-existent domain, the test function returns the original query domain.

    Fortunately there is only one site/service on this box that does php dns or it would really be a pain.

    Although I do have a kluge that works, if anyone can suggest how to track it down, I would be very happy.
  3. sel

    sel New Member

    I had the same problem. What I've done is i've removed this line:
    from /etc/resolve.conf file
    After this gethostbyname started to work.

Share This Page