|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #13443 gethostbyname returns the name not the IP addr.
Submitted: 2001-09-25 16:22 UTC Modified: 2002-06-18 18:42 UTC
From: brooks at netgate dot net Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 4.0.6 OS: BSDI 4.1
Private report: No CVE-ID: None
 [2001-09-25 16:22 UTC] brooks at netgate dot net
This is basically the same report as 8754, 12678, and 8856.  The gethostbyname function returns the name instead of the IP address.  The host is configured correctly and names can be resolved from the shell using nslookup, dig, etc.  This bug first appeared in 4.0.4 (although I didn't know it until now) and is present in 4.0.6 and 4.0.8-dev (php4-200109251335.tar.gz - latest from the snapshots dir as of 9/26/01).  I regressed the bug back to my previous version of 3.0.14 and the bug is not present in that version.  I have tried both gethostbyname (returns the name) and gethostbynamel (returns 0 items in the array).


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2001-09-25 16:45 UTC]
duplicate of 13423, among the others listed in the report. gethostbyname() returns the hostname it is passed when the C-level gethostbyname() call fails.
 [2001-09-25 17:16 UTC] brooks at netgate dot net
IMHO this is not the same as #13423, which is why I didn't list it in the original report.  Although the data returned is the same (hostname instead of IP address) the reason for the failure is seems very different.
 [2001-09-26 17:33 UTC] brooks at netgate dot net
Does PHP require a local bind running?  Everything seems to point in that direction.  The resolver libs are doing a lookup to the address of vs. the servers as configured in resolv.conf.

;; Querying server (# 1) address =
 [2001-09-26 17:43 UTC]
php does not require a local dns server to be running unless your system's C library gethostbyname() call does (which seems extraordinarily unlikely).

here's a small c program you can use to test your system's gethostbyname(). if it says that no ip address is found, that's the exact same situation in which php's gethostbyname() will return the original hostname.

#include <stdlib.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char **argv) {
  struct hostent *host;
  struct in_addr in;

  if (argc != 2) {
    fprintf(stderr,"usage: %s hostname\n", argv[0]);

  host = gethostbyname(argv[1]);

  if (host->h_length) {
    memcpy(&in.s_addr, *(host->h_addr_list), sizeof(in.s_addr));
  else {
    fprintf(stderr,"no ip address found for %s\n", argv[1]);


 [2001-09-27 02:22 UTC]
PHP uses the resolver library(ies) installed on your
system, just as the other software installed. It is
possible that you have several libraries and nslookup
uses something else though. If you have nsswitch.conf
or similar you should make sure that is okay, but if
for instance ping hostname works (where hostname is
not in /etc/hosts or NIS...), it should be okay.
Please check that resolv.conf is readable for all. You
aren't running PHP in chroot environment, are you?

 [2001-09-27 12:45 UTC] brooks at netgate dot net
I'm with you...  The program below works fine, as does dig, nslookup, perl, etc.  So, I'm at a loss as to why PHP doesn't work.  I sent an email to the BSDI users list and everyone is convinced that you MUST run a local named server for PHP to work correctly.  This makes no sense (to me).

vp3: {4} % ldd ../cgi-bin/php => /shlib/ (0x48169000) => /shlib/ (0x4816d000) => /shlib/ (0x4817f000) => /shlib/ (0x4818b000)

vp3: {5} % ldd gethostbyname => /shlib/ (0x48052000) => /shlib/ (0x4805e000)

 [2002-06-18 18:42 UTC]
Thank you for taking the time to report a problem with PHP.
Unfortunately your version of PHP is too old -- the problem
might already be fixed. Please download a new PHP
version from

If you are able to reproduce the bug with one of the latest
versions of PHP, please change the PHP version on this bug report
to the version you tested and change the status back to "Open".
Again, thank you for your continued support of PHP.

PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Fri Oct 07 03:05:56 2022 UTC