本文介绍了重定向后查找URL并获取网站的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有:

domainA.com/out.php:

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

能否从domainC.com中获取domanA.com/out.php并获取url:domainB.comIP Address?

Is it possible to get url : domainB.com and it's IP Address, with domanA.com/out.php from domainC.com?

我想要什么:

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
?>

推荐答案

如果需要获取所有重定向,则可以

If you need to get all the redirects, you can do

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;
}

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

This will return an array holding all the redirects with the last entry being the final destination.

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

输出

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

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

You'd have to lookup the IP's manually though (see other answers here) but note that a redirect target doesnt have to be a hostname. It can very well be an IP as well.

这篇关于重定向后查找URL并获取网站的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:51