Getting redirected url

Discussion in 'PHP' started by explorer11, Aug 26, 2007.

  1. #1
    explorer11, Aug 26, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    function get_final_location($url)
    {
        $headers = @get_headers($url);
       
        foreach ((array)$headers AS $header)
        {
            if (preg_match('/Location\s*:\s*(https?:[^;\s\n\r]+)/i', $header, $redirect))
            {
                return get_final_location($redirect[1]);
            }
        }
       
        return $url;
    }
    
    PHP:

    This gets the final URL, even if the page is redirected multiple times.
     
    nico_swd, Aug 26, 2007 IP
  3. explorer11

    explorer11 Peon

    Messages:
    107
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the reply. Is there any alternative for get_headers for PHP 4?
     
    explorer11, Aug 26, 2007 IP
  4. DeViAnThans3

    DeViAnThans3 Peon

    Messages:
    785
    Likes Received:
    83
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Does the following function works for you? I grabbed it instantly from php.net ;)
    Should be abled to work on PHP4.

    <?php
    if(!function_exists('get_headers'))
    {
        function get_headers($url,$format=0)
        {
            $url=parse_url($url);
            $end = "\r\n\r\n";
            $fp = fsockopen($url['host'], (empty($url['port'])?80:$url['port']), $errno, $errstr, 30);
            if ($fp)
            {
                $out  = "GET / HTTP/1.1\r\n";
                $out .= "Host: ".$url['host']."\r\n";
                $out .= "Connection: Close\r\n\r\n";
                $var  = '';
                fwrite($fp, $out);
                while (!feof($fp))
                {
                    $var.=fgets($fp, 1280);
                    if(strpos($var,$end))
                        break;
                }
                fclose($fp);
    
                $var=preg_replace("/\r\n\r\n.*\$/",'',$var);
                $var=explode("\r\n",$var);
                if($format)
                {
                    foreach($var as $i)
                    {
                        if(preg_match('/^([a-zA-Z -]+): +(.*)$/',$i,$parts))
                            $v[$parts[1]]=$parts[2];
                    }
                    return $v;
                }
                else
                    return $var;
            }
        }
    }
    ?>
    PHP:
     
    DeViAnThans3, Aug 26, 2007 IP
  5. ssanders82

    ssanders82 Peon

    Messages:
    77
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Another alternative is to use curl, it tells you stats like how long it took and where it ended up with curl_getinfo after your curl_exec call
     
    ssanders82, Aug 27, 2007 IP
  6. alpesh

    alpesh Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Is this script to achieve http://mydomain.com/go.php?http://microsoft.com

    Basically I want all links that users click should go through my home url.

    Makes sense ? :)
     
    alpesh, Aug 27, 2007 IP
  7. explorer11

    explorer11 Peon

    Messages:
    107
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    okay, just want to say thanks for all replies.
     
    explorer11, Aug 28, 2007 IP