使用php获取网站ip

我需要使用PHP获取给定的网站IP地址,即托管网站的服务器的IP地址.

为此,我使用了gethostbyname(‘** example.com *’).当站点重定向时,它工作正常.例如,如果我使用此功能获取google.com,则会显示“74.125.235.20”.

当我尝试“lappusa.com”时,它给出了“lappusa.com”.然后我尝试在浏览器中重定向到“http://lappusa.lappgroup.com/”.我检查了它显示的http状态代码200.

但我需要获取IP地址,即使网站被重定向,如果lappusa.com重定向到lappusa.lappgroup.com然后我需要获取重定向网址的IP.

我应该怎么做到这一点?任何帮助非常感谢,谢谢!

问题不在于HTTP重定向(高于gethostbyname运行的级别),但lappusa.com无法解析为任何IP地址,因此无法在任何浏览器中加载.你的浏览器做了什么是自动尝试预先添加www ..

您可以在代码中重现该行为.另请注意,多个IP(版本4和6)可以与一个域关联:

<?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
) */

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...