PHP Classes

Get location when redirected

Recommend this page to a friend!

      PHP HTTP protocol client  >  All threads  >  Get location when redirected  >  (Un) Subscribe thread alerts  
Subject:Get location when redirected
Summary:small suggestion to know where you have been redirected
Messages:3
Author:StalkR GHOST
Date:2008-01-25 22:14:55
Update:2008-01-26 10:13:40
 

  1. Get location when redirected   Reply   Report abuse  
Picture of StalkR GHOST StalkR GHOST - 2008-01-25 22:14:55
Hello,

First, a *huge* thank you for this powerful class. I'm using it a lot in my web spiders, parsers, robots...

Second, I was using the good $follow_redirect feature but I was disappointed: I couldn't retrieve the URL of the page where I was finally redirected at the end.
This small suggestion adds an extra server header, to know where we go...

---[ OPEN ]--- // the main class

http.php


---[ FIND ]--- // approx. line 1318

if(strlen($error=$this->Close())==0
&& strlen($error=$this->Open($arguments))==0
&& strlen($error=$this->SendRequest($arguments))==0)
{
$this->redirection_level++;
if($this->redirection_level>$this->redirection_limit)
$error="it was exceeded the limit of request redirections";
else
$error=$this->ReadReplyHeaders($headers);
$this->redirection_level--;
}
if(strlen($error))
return($this->SetError($error));

---[ ADD AFTER ]--- // approx. line 1331

$headers['http_lib__redirected_to'] = $location;


--[ SAVE AND CLOSE ]---



Obviously, the header key must be recognizable enough because you don't want to parse it as a *server* header ;)

Anyway, this was just to tell you about the idea I got (because I needed it, of course ^^).
Another interesting thing could be to add a kind of "URL route" to know where the request has passed before arriving.

Once again, well done with this class!
Best regards,
StalkR.

  2. Re: Get location when redirected   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2008-01-26 03:26:41 - In reply to message 1 from StalkR GHOST
You can retrieve the final URL using some of the class variables like this:

$http->protocol.'://'.$http->host_name.':'.($http->host_port ? $http->host_port : '').$http->request_uri

  3. Re: Get location when redirected   Reply   Report abuse  
Picture of StalkR GHOST StalkR GHOST - 2008-01-26 10:13:40 - In reply to message 2 from Manuel Lemos
Hmmm... you're right!
Better way to do that indeed :)

Thank you!