【问题标题】:Find url and get ip address of website after redirect重定向后查找url并获取网站的ip地址
【发布时间】:2013-02-16 05:07:05
【问题描述】:

如果我有:

domainA.com/out.php:

<?php
  header('location: http://domainB.com/');
?>

是否可以从domainC.com 获取 url:domainB.com 和它是 IP AddressdomanA.com/out.php

我想要什么:

domainA.com/index.php

<?php
   $data = getUrlandIp("domainA.com/out.php");
   echo $data[0];  # wanted output (URL) : domainB.com
   echo $data[1];  # wanted output (IP) : 133.133.133.133
?>

【问题讨论】:

标签: php url ip-address http-redirect


【解决方案1】:

如果你需要得到所有的重定向,你可以这样做

function getRedirectsToUri($uri)
{
    $redirects = array();
    $http = stream_context_create();
    stream_context_set_params(
        $http,
        array(
            "notification" => function() use (&$redirects)
            {
                if (func_get_arg(0) === STREAM_NOTIFY_REDIRECTED) {
                    $redirects[] = func_get_arg(2);
                }
            }
        )
    );
    file_get_contents($uri, false, $http);
    return $redirects;
}

这将返回一个数组,其中包含所有重定向,最后一个条目是最终目的地。

示例 (demo)

print_r(getRedirectsToUri('http://bit.ly/VDcn'));

输出

Array ( 
    [0] => http://example.com/ 
    [1] => http://www.iana.org/domains/example/ 
) 

您必须手动查找 IP(请参阅此处的其他答案),但请注意重定向目标不必是主机名。它也可以是一个 IP。

【讨论】:

    【解决方案2】:

    使用get_headers() 从 URL 获取 http 标头。

    这样的东西应该可以用来获取域名。

    $headers = get_headers('http://domaina.com/out.php', 1);
    echo $headers['Location'];
    

    要解析 IP 地址,请查看 gethostbyname() 函数。

    echo gethostbyname($headers['Location']);
    

    【讨论】:

    • 在我的情况下,没有名为“Location”的标题
    • 可能是小写的location?执行print_r($headers); 以查看 $headers 包含的所有内容。
    【解决方案3】:

    我不知道你的意思是什么

    来自 domainC.com?

    但在 PHP 中,您可以调用 gethostbyname('domainB.com'),这将为您提供来自 domainA.com/out.php 的 domainB.com 的 IP

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-28
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      • 2013-02-24
      相关资源
      最近更新 更多