Forum Moderators: coopster

Message Too Old, No Replies

PHP redirection

Get the destination URL accessing only the redirector

         

yourock

8:42 pm on Jul 19, 2010 (gmt 0)

10+ Year Member



Hi,

I need help on redirect of php scripts. The problem is: I have a page for example www.domain.com/redirect.php?id=34 that redirects to another page, domain2.com/product.php?id=2.

I need to create a script that allow me to know the URL of the redirection (domain2.com/product.php?id=2) just by accessing the redirector (www.domain.com/redirect.php?id=34). I dont have acess to none of the pages.
Anyone have a an idea? tanks! ;)

yourock

8:44 pm on Jul 19, 2010 (gmt 0)

10+ Year Member



PS: when I mean access is write access

enigma1

3:16 pm on Jul 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi yourock welcome to the forums,

If I understand you correctly you can pass parameters with the URL or post the parameters for example

www.example.com/redirect.php?id=34&id2=2

the 2nd parameter id2=2 is included so the redirected page can now use the $_GET array and retrieve it.

Another approach if you need to keep the original links intact is to use post for example with curl you can post arguments.

yourock

4:17 pm on Jul 20, 2010 (gmt 0)

10+ Year Member



Tank you for your attention enigma1.

I found that what i'm looking for is exactly this (because I dont know the destiny url):

/**
* get_redirect_url()
* Gets the address that the provided URL redirects to,
* or FALSE if there's no redirect.
*
* @param string $url
* @return string
*/
function get_redirect_url($url){
$redirect_url = null;

$url_parts = @parse_url($url);
if (!$url_parts) return false;
if (!isset($url_parts['host'])) return false; //can't process relative URLs
if (!isset($url_parts['path'])) $url_parts['path'] = '/';

$sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : 80), $errno, $errstr, 30);
if (!$sock) return false;

$request = "HEAD " . $url_parts['path'] . (isset($url_parts['query']) ? '?'.$url_parts['query'] : '') . " HTTP/1.1\r\n";
$request .= 'Host: ' . $url_parts['host'] . "\r\n";
$request .= "Connection: Close\r\n\r\n";
fwrite($sock, $request);
$response = '';
while(!feof($sock)) $response .= fread($sock, 8192);
fclose($sock);

if (preg_match('/^Location: (.+?)$/m', $response, $matches)){
if ( substr($matches[1], 0, 1) == "/" )
return $url_parts['scheme'] . "://" . $url_parts['host'] . trim($matches[1]);
else
return trim($matches[1]);

} else {
return false;
}

}

/**
* get_all_redirects()
* Follows and collects all redirects, in order, for the given URL.
*
* @param string $url
* @return array
*/
function get_all_redirects($url){
$redirects = array();
while ($newurl = get_redirect_url($url)){
if (in_array($newurl, $redirects)){
break;
}
$redirects[] = $newurl;
$url = $newurl;
}
return $redirects;
}

/**
* get_final_url()
* Gets the address that the URL ultimately leads to.
* Returns $url itself if it isn't a redirect.
*
* @param string $url
* @return string
*/
function get_final_url($url){
$redirects = get_all_redirects($url);
if (count($redirects)>0){
return array_pop($redirects);
} else {
return $url;
}
}

////////////////////////////////////////////////////////////MAIN/////////////////////////////////////////////////////////////////////
//$rez = get_all_redirects('http://daerils.gtrends.hop.clickbank.net/');
//print_r($rez);
/*

Tank you again.