homepage Welcome to WebmasterWorld Guest from 54.198.139.141
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Get URL redirected url?
janton




msg:3681922
 10:11 pm on Jun 23, 2008 (gmt 0)

Hello Hello,

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;

 

NomikOS




msg:3682026
 1:45 am on Jun 24, 2008 (gmt 0)

Explain better please.

I need to echo/print the url that redirects you to a other url

what do you mean? who are redirecting?

janton




msg:3682184
 8:03 am on Jun 24, 2008 (gmt 0)

NomikOS thx for the reaction!

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?

NomikOS




msg:3682725
 8:40 pm on Jun 24, 2008 (gmt 0)

What exactly do you want to do? it's legal?

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.

Little_G




msg:3682748
 9:05 pm on Jun 24, 2008 (gmt 0)

Hi,

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

janton




msg:3682982
 8:56 am on Jun 25, 2008 (gmt 0)

Thx thx Little_G

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]

Little_G




msg:3682990
 9:48 am on Jun 25, 2008 (gmt 0)

Hi,

The page actually redirects twice but you still shouldn't get a blank screen, are you sure you have curl installed?

Andrew

janton




msg:3682997
 10:02 am on Jun 25, 2008 (gmt 0)

hihi :) curl extension installed.. i did not see that.. I will google it up!

janton




msg:3683017
 11:25 am on Jun 25, 2008 (gmt 0)

Is there another way?

Because my host doesn't have curl extension enabled, and are not planning to enabling it!

Little_G thx for your quick respons, i will read more carefully next time.

Little_G




msg:3683054
 12:39 pm on Jun 25, 2008 (gmt 0)

Hi,

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

janton




msg:3683200
 3:44 pm on Jun 25, 2008 (gmt 0)

Little_G i LOVE you...

It works great :)
I'm a hobby phpr, so i never would have found this!

Now i have to make this happen on my page... thankyou;)

Great thanks!

Little_G




msg:3683205
 3:52 pm on Jun 25, 2008 (gmt 0)

Haha, no problem, good luck!

Andrew

janton




msg:3688301
 9:28 am on Jul 2, 2008 (gmt 0)

Ok Little_G Thx for the help before :)

[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

Little_G




msg:3688486
 1:55 pm on Jul 2, 2008 (gmt 0)

Hi,

The follow_redirect function above sends a HEAD request to look for redirection, you can do a similar thing to check if the link is dead by send a HEAD request and check the response for an error.

Andrew

janton




msg:3688501
 2:21 pm on Jul 2, 2008 (gmt 0)

ok will take a look at that tonight! Thx
But what is the response for an error -> (function code or code)

Little_G




msg:3688508
 2:25 pm on Jul 2, 2008 (gmt 0)

Hi,

I'm assuming the HTTP response code will be an error code, but that would depend on the website.

Andrew

janton




msg:3690397
 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?

janton




msg:3690404
 12:29 pm on Jul 4, 2008 (gmt 0)

o yeah is it possible to have a query been replaced (over the old one).

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)

janton




msg:3690681
 8:03 pm on Jul 4, 2008 (gmt 0)

OK OK i have it :) well not all but i know how to export or echo database code!

//-- 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?

janton




msg:3690687
 8:16 pm on Jul 4, 2008 (gmt 0)

AND...

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
}

Little_G




msg:3690701
 8:40 pm on Jul 4, 2008 (gmt 0)

Hi,

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]

Little_G




msg:3690703
 8:47 pm on Jul 4, 2008 (gmt 0)

Hi,

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]

janton




msg:3691166
 4:51 pm on Jul 5, 2008 (gmt 0)

Thx little_g

I'm trying it now!

face50




msg:3725638
 3:56 pm on Aug 18, 2008 (gmt 0)

Hey I had the same problem as janton, but sadly the script did not work for me...

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]

Little_G




msg:3726505
 6:16 pm on Aug 19, 2008 (gmt 0)

Hi,

That website appears to be blocking scraping [en.wikipedia.org].

Andrew

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved