Page is a not externally linkable
- Code, Content, and Presentation
-- PHP Server Side Scripting
---- Get URL redirected url?


janton - 12:00 pm on Jul 4, 2008 (gmt 0)


Hi... I'm getting really close! ;) But at the end there is something wrong..

Ok everything works UNTIL i want to echo $dburl;
or $dburl = "SELECT url FROM `films` WHERE id = '10'";

So my question How do i get the url out of my database?

____________________________________________________________________________________________________________

$_CONFIG["Username"]= 'username'; // Gebruikersnaam
$_CONFIG["Password"]= 'pass'; // Wachtwoord
$_CONFIG["Host"]= 'localhost'; // Host
$_CONFIG["Database"]= 'database';

$url = 'http://example.get.com';

function follow_redirect($url){
$redirect_url = null;


if(function_exists("curl_init")){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
}
else{
$url_parts = parse_url($url);
$sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : 80));
$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 = fread($sock, 2048);
fclose($sock);
}

$header = "Location: ";
$pos = strpos($response, $header);
if($pos === false){
return false;
}
else{
$pos += strlen($header);
$redirect_url = substr($response, $pos, strpos($response, "\r\n", $pos)-$pos);
return $redirect_url;
}
}


//echo '<ol>';
while(($newurl = follow_redirect($url)) !== false){
//echo '<li>', $url, '</li>';
$url = $newurl;
}
//echo '</ol>';

$Verbinding = mysql_connect($_CONFIG["Host"], $_CONFIG["Username"], $_CONFIG["Password"]);


$DbSelect = mysql_select_db($_CONFIG["Database"], $Verbinding);

if ($DbSelect == false)
{
trigger_error("can't find database");
}

$TestData = array();
$TestData[] = array( '10', $url );

$Query = "
INSERT INTO
`films`
SET
`id` = '%s',
`url` = '%s'
";

foreach ($TestData as $Data)
{
mysql_query(vsprintf($Query, $Data), $Verbinding)
or trigger_error('Can't find query');
}

$dburl = "SELECT url FROM `films` WHERE id = '10'";

echo $dburl;

MySql_close($Verbinding);
____________________________________________________________________________________________________________

Ok everything works UNTIL i want to echo $dburl;
or $dburl = "SELECT url FROM `films` WHERE id = '10'";

So my question How do i get the url out of my database?


Thread source:: http://www.webmasterworld.com/php/3681920.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com