Find url and get ip address of website after redirect

后端 未结 3 435
一整个雨季
一整个雨季 2020-12-21 06:14

If I have :

domainA.com/out.php :


Is it possible to ge

相关标签:
3条回答
  • 2020-12-21 06:27

    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.

    0 讨论(0)
  • 2020-12-21 06:27

    I am not sure what you mean by

    from domainC.com?

    but in PHP you can call gethostbyname('domainB.com') which will give you the IP of domainB.com from domainA.com/out.php

    0 讨论(0)
  • 2020-12-21 06:40

    Use get_headers() to get the http headers from a URL.

    Something like this should work to get the domain name.

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

    To resolve the IP address, look at the gethostbyname() function.

    echo gethostbyname($headers['Location']);
    
    0 讨论(0)
提交回复
热议问题