9

I need to fetch the given website ip address using php, that is ip address of server in which website is hosted.

For that i've used gethostbyname('**example.com*'). It works fine when the site is not redirected. for example if i used this function to get google.com, it gives "74.125.235.20".

When i tried it for "lappusa.com" it gives "lappusa.com". Then i tried this in browser it is redirecting to "http://lappusa.lappgroup.com/" . I checked the http status code it shows 200.

But i need to get ip address even if site was redirected, like if lappusa.com is redirected to lappusa.lappgroup.com then i need to get ip for redirected url.

How should i get this? any help greatly appreciated, Thanks!.

hakre
  • 193,403
  • 52
  • 435
  • 836
vkGunasekaran
  • 6,668
  • 7
  • 50
  • 59

3 Answers3

14

The problem is not the HTTP redirect (which is above the level gethostbyname operates), but that lappusa.com does not resolve to any IP address and therefore can't be loaded in any browser. What your browser did was automatically try prepending www..

You can reproduce that behavior in your code. Also note that multiple IPs (version 4 and 6) can be associated with one domain:

<?php
function getAddresses($domain) {
  $records = dns_get_record($domain);
  $res = array();
  foreach ($records as $r) {
    if ($r['host'] != $domain) continue; // glue entry
    if (!isset($r['type'])) continue; // DNSSec

    if ($r['type'] == 'A') $res[] = $r['ip'];
    if ($r['type'] == 'AAAA') $res[] = $r['ipv6'];
  }
  return $res;
}

function getAddresses_www($domain) {
  $res = getAddresses($domain);
  if (count($res) == 0) {
    $res = getAddresses('www.' . $domain);
  }
  return $res;
}

print_r(getAddresses_www('lappusa.com'));
/* outputs Array (
  [0] => 66.11.155.215
) */
print_r(getAddresses_www('example.net'));
/* outputs Array (
  [0] => 192.0.43.10
  [1] => 2001:500:88:200::10
) */
phihag
  • 278,196
  • 72
  • 453
  • 469
  • It worked. i tried prepending "www" to gethostbyname it results the same output which your funtion does. but can explain a bit more please..also add closing php tag to you answer... Thanks. – vkGunasekaran Oct 15 '11 at 10:04
  • 1
    @Sekar You don't need to close the php tag if there is no more content in the file (If you close it, many editors will insist on one more empty line, which will be outputted by php). [`get_dns_record`](http://php.net/get_dns_record) allows you to not only get IPv4 addresses, but also IPv6 ones. If you're not interested in that, you can replace the `getAddresses` function with [`gethostbynamel`](http://php.net/gethostbynamel). – phihag Oct 15 '11 at 10:10
  • 1
    ok, thanks for your efforts also for giving gethostbynamel function link.it was nice. – vkGunasekaran Oct 15 '11 at 10:17
0

They redirect using a META tag in the HTML source. You will need to parse the actual sourcecode to catch this.

Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
-1

Did you try sending HttpRequest to certain page and then parsing the response headers? I'm not sure, but it should contain some IP or host info...

Moyshe
  • 1,122
  • 1
  • 11
  • 19
  • yeah,i tried curl to check "location" exist or not sometimes there we see the redirected url,for this url even that property isn't exist.. – vkGunasekaran Oct 15 '11 at 09:51
  • now i found curl couln't catch the javascript redirections. so how to check given url is redirected or not... – vkGunasekaran Oct 15 '11 at 09:55