How to find the redirect?

Discussion in 'PHP' started by Sebastin, Feb 22, 2009.

  1. #1
    I want to know whow to find the reditected url.

    like when someone uses a php header to redirect how can I use php to find the destination url?
     
    Sebastin, Feb 22, 2009 IP
  2. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #2
    If it redirects - destination url means the site where you are being redirected .. Am I wrong ? In that case - why you want to get it if you can see it in address bar ? :confused:
    ** You can't get it via php ..
     
    ActiveFrost, Feb 22, 2009 IP
  3. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #3
    This should work, but only if the redirection is from HTTP header.
    <?php
    
    $url = 'http://test.com/test.php';// the url to check
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_HEADER, true);// must be true
    $result = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
    
    if ($info['http_code'] == 301 || $info['http_code'] == 302) {
    	list($headers, $body) = explode("\r\n\r\n", $result);
    	preg_match('/location:([ ]+)?(.*)?/i', $headers, $match);
    	$redirectedTo = trim($match[2]);// yes, put a trim() here
    	echo 'You are redirected to <strong>'.$redirectedTo.'</strong>';
    } else {
    	echo 'Not redirected';
    }
    ?>
    PHP:
     
    xrvel, Feb 22, 2009 IP
  4. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #4
    $_SERVER['HTTP_REFERER']?

    - ads2help
     
    ads2help, Feb 22, 2009 IP
  5. websecrets

    websecrets Peon

    Messages:
    97
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You're going to have to show an example or explain a little better.

    Are you trying to get data on incoming connections to your site or outgoing proxy type connections?
     
    websecrets, Feb 23, 2009 IP