0

This is code for getting site's IP

$ip = gethostbyname(parse_url('http://www.wikipedia.com', PHP_URL_HOST));
print_r($ip);

but, what is the proper way to check with php does the website's IP redirects to site's domain?

EDITED: It seems I wasn't clear enough in my first question. The question is based on seositecheckup.com test. One of the warning there says:

IP Canonicalization Test
Your site's IP 198.35.26.96 does not redirect to your site's domain name. This could cause duplicate content problems if a search engine indexes your site under both its IP and domain name.

So, I'm asking how would I check by myself if the site's IP redirects to site's domain, in PHP

codexy
  • 413
  • 1
  • 5
  • 18
  • Possibly others will have other ideas, but I think you'd need to use CURL function to check response headers. A quick google on "curl response headers" and... http://stackoverflow.com/questions/9183178/php-curl-retrieving-response-headers-and-body-in-a-single-request – Duane Lortie Nov 24 '16 at 17:00
  • @DuaneLortie so you are saying to check IP's headers or? – codexy Nov 24 '16 at 17:03
  • I'm not really sure what you try to do – Charlotte Dunois Nov 24 '16 at 17:04
  • To be sure the domain name and the IP result in the same content.. I'd fetch via curl the domain name, check for redirects,and if response is 200, make a checksum of that content, then do the same for the IP.. I don't know any other way to detect a 301/302 redirect other than w/ CURL – Duane Lortie Nov 24 '16 at 17:07
  • I edited my question to be more clear – codexy Nov 24 '16 at 17:31

2 Answers2

0

The question says one thing, but the code implies another ...

  • If you're looking to do an IP-to-hostname lookup, there's another function for that: gethostbyaddr()
  • If you did indeed mean "redirect" when you said it, you'll have to use an HTTP client library (like cURL) to see whether http://ipAddress/ returns a 30x status code and a Location header pointing to the hostname-based URL.
Narf
  • 14,600
  • 3
  • 37
  • 66
0

What you are trying to do is a reverse IP lookup.

The gethostbyname do forward IP address and gethostbyaddr do the reverse IP lookup.

An code example would be:

$ip = gethostbyname(parse_url('http://www.wikipedia.com', PHP_URL_HOST));
print_r($ip);
$domain = gethostbyaddr($ip);
print_r($domain);
André Lima
  • 141
  • 5