Forum Moderators: coopster
I'm getting crazy!
This is my problem:
I need to echo/print the url that redirects you to a other url.
So i have www.link1.com this site redirects you to www.link2.com
I want to echo/print the link2.com
I get the url through link1.com
Is it possible for php to see where it's redirecting to?
I found on the internet how to read the url of my browser:
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $url;
Maby some like....:
$url="www.link1.com";
......missing part.......
//needs to see where the link goes/read the url
.........................
echo $url;
I mean:
I have a URL this URL redirects you to a other URL.
This is not my URL, a url of somebody else.
So how did i get the url before:
First i went to firefox, typt the url: www.link1.com/whatever
Then it is loading......
And it goes to www.link1.com/2839398437839403/whatever
I had to copy this link and past it in my html.
This because i DON"T want to use the www.link1.com/whatever..but the coded link!
Because the coded link(www.link1.com/2839398437839403/whatever) changes every 4 days... i have to do this every week
Does this example helps?
In first place: if you don't make the redirection (with your script in your server) then you can't do nothing.
Any way it seems not a PHP issue. For URL redirections go to [webmasterworld.com...] (in quest of mod_rewrite)
If it is not the case explain more please. It could be interesting.
If I understand correctly what you want to do then something like this should work. Assuming you have the curl extension installed:
<?php
$ch = curl_init("http://www.link1.com/whatever");
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);
$header = "Location: ";
$pos = strpos($response, $header);
$pos += strlen($header);
$redirect_url = substr($response, $pos, strpos($response, "\r\n", $pos)-$pos);
echo $redirect_url;
?>
Andrew
But it did'n work... :(
Here is what I did and also my link, so you can try it maby?
<?php
$ch = curl_init("http://example.com/file/get/Movtrailer-trailer_deadspace514.mov");
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);
$header = "Location: ";
$pos = strpos($response, $header);
$pos += strlen($header);
$redirect_url = substr($response, $pos, strpos($response, "\r\n", $pos)-$pos);
echo $redirect_url;
?>
This gave me a blaco screen.
So this is my link: http://example.com/file/get/Movtrailer-trailer_deadspace514.mov
and if you past it in your browser you get:
[s9.video.example.com...]
[edited by: coopster at 12:03 pm (utc) on June 25, 2008]
[edit reason] please use example.com [/edit]
Here's the complete code, if curl is enabled it will use curl otherwise php sockets, I left the curl stuff in in case it gets enabled later as curl is generally faster then sockets.
<?php
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;
}
}
$url = 'http://example.com/file/get/Movtrailer-trailer_deadspace514.mov';
echo '<ol>';
while(($newurl = follow_redirect($url)) !== false){
echo '<li>', $url, '</li>';
$url = $newurl;
}
echo '</ol>';
echo '<a href="', $url, '">', $url, '</a>';
?>
Andrew
[webmasterworld.com...]
now i have one small question,
I'm going to adjust the script.
First i will tell you what i'm planning to do:
I want to past the $url that it generates to mysql database..
This happens the first time, the second time somebody clicks the link..
it checks if the link is still working(form the mysql)
if yes it echo's the link from mysql
if no it generates the new link it safed to mysql and echo's the new link.
What is my question:
How can i see a link is dead?
some like:
if $url_from_db is empty{
old script
save link to db
echo dblink }
else{
echo dblink}
Also i have to realise that i have to give every url I have a ID so the php script/mysql knows what link to grab! The id i can give by hand.. that is no probleme..
I'm trying the php->mysql->php for the first time so maby you have some tips?
hope you can help me out... i'm spending ours at it
but it's fun ;)
Greats,
Janton
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?
Because if i want to make a new url, that overrides the old id... it gives a error ;(
$TestData = array();
$TestData[] = array( '10', $url );
$Query = "
INSERT INTO
`films`
SET
`id` = '%s',
`url` = '%s'
";
If i delete the old one out of mysql (the one with 10 id) than i can make a new one that hase the value (id)
//-- haal de nieuws berichten uit de database
$sql = "SELECT url FROM `films` WHERE `films`.`id` = $jumi[3] LIMIT 1 ";
$res = mysql_query($sql);
$row = mysql_fetch_array($res);
$dburl = $row['url'];
echo $dburl
only how can i replace a existing tabel? question above here?
well i'm getting really close now! yes yes..
only the if statement?
Like my question before... how does php know the link is dead?
I mean:
you say: HEAD request and check the response for an error.
dburl is the url out of my database!
is it false or error or some? do you know?
$check = "HEAD " . $dburl . (isset($dburl['query']) ? '?'.$dburl['query'] : '') . " HTTP/1.1\r\n";
if ($check == error){
script
}
else {
script
}
For overwriting a value in the database use UPDATE [dev.mysql.com].
I'll post code for how to check a dead link in a minute.
Andrew
[edited by: Little_G at 8:48 pm (utc) on July 4, 2008]
The following function will check a url to see if it returns a '404 Not Found' error.
<?php
function is_link_dead($url){
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);
}
return (strpos($response, 'HTTP/1.1 404') === 0);
}
?>
Andrew
[edited by: Little_G at 8:48 pm (utc) on July 4, 2008]
the links on this newspaper site:
http://www.example.com/englanda-k.htm
they are all to some intermediate place e.g.
http://lt.example2.com/lt.php?3070
and if you go to this url, you get forwarded to the newspaper's site...
but sadly that script does not return the redirected url...
any ideas?
cheers
[edited by: jatar_k at 4:17 pm (utc) on Aug. 18, 2008]
[edit reason] please use example.com [/edit]