查找网址,并得到重定向后的网站的IP地址(Find url and get ip address o

2019-06-25 13:16发布

如果我有 :

domainA.com/out.php:

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

是否有可能获得网址: domainB.com和它的IP Address ,与domanA.com/out.phpdomainC.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
?>

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

这将返回一个数组保存所有与上一个作为最终目的地的重定向。

实施例( 演示 )

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

产量

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

你不得不查找IP的手动虽然(在这里看到其他的答案),但注意,重定向目标不必须是主机名。 它可以很好地是IP为好。



Answer 2:

使用get_headers()从URL中获得的HTTP标头。

像这样的东西应该工作,以获得域名。

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

为了解决IP地址,看的gethostbyname()函数。

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


Answer 3:

我不知道你是什么意思

从domainC.com?

但在PHP中可以调用gethostbyname('domainB.com')这将给你domainB.com从IP domainA.com/out.php



文章来源: Find url and get ip address of website after redirect