durduvakis
durduvakis

Reputation: 189

php gethostbyname() function

Using gethostbyname() does not always return an IP address. Instead it returns the same hostname back again. What are the reasons for this ? At first I thought that it was a fake hostname. Then using it in a function that is trying to catch some bots it caught baidu as using a fake hostname, which as I tested was a false.

example:

echo gethostbyname('baiduspider-123-125-71-12.crawl.baidu.com');

returns

baiduspider-123-125-71-12.crawl.baidu.com

but

echo gethostbyname('201-35-178-134.cslce701.dsl.brasiltelecom.net.br');

returns

201.35.178.134

Any help in understanding this better appreciated.

Upvotes: 1

Views: 4017

Answers (1)

Lorenz
Lorenz

Reputation: 2259

Have a look at the documentation: Returns the IPv4 address or a string containing the unmodified hostname on failure.

If it doesn't find the domain or there is no corresponding A-Record it returns the unmodified hostname.

In your example, Baidu has not set up an A-Record for their bots, in Linux the host command prints that:

Host baiduspider-123-125-71-12.crawl.baidu.com not found: 3(NXDOMAIN)

Upvotes: 2

Related Questions