Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
886 views
in Technique[技术] by (71.8m points)

php - Find url and get ip address of website after redirect

If I have :

domainA.com/out.php :

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

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

What I want :

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
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

Example (demo)

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

Output

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...