Page 1 of 1

How to get URL address of redirected page?

Posted: Sat Nov 05, 2005 8:32 am
by surfinglight
Hello,

I have written codes which read url from a table and redirect to the address.

For example, when id number 3274 is passed,
http://www.sample.com/process?id=3274

This will search the relevant url address from a table(let's say, 3274 is http://www.yahoo.com),
and redirect a user to the address.
Therefore, the user who runs "http://www.sample.com/process?id=3274" will be redirected to
http://www.yahoo.com

My question is how do I code to retireve that URL addres, "http://www.yahoo.com" when coding.
I mean assuming that I cannot refer to the table.

So, I would like to know how to code to retrieve the url address after redirection is occured(it will be "http://www.yahoo.com" from the above sample), assuming that I only know the url address which will redirect itself to other address(it will be "http://www.sample.com/process?id=3274" from the above sample).

Is there a Php function for this?

I would really appreciate if some one can help on this.

Many thanks in advance.

Posted: Sat Nov 05, 2005 8:43 am
by timvw
You could fe: use scriptable browser:

Code: Select all

require_once('simpletest/browser.php');
    
$browser = &new SimpleBrowser();
$browser->setMaximumRedirects(0);
$browser->get('http://example.com/link=1234');
$browser->getHeaders();
And test if there is a Location header present...

Posted: Sat Nov 05, 2005 8:44 am
by feyd
you want to know what address a url redirects to?

A HTTP redirect uses the page headers. In PHP 5 get_headers() can be used (cURL in older versions, which is quite a bit more complicated, but works similarly). You'll also need to check the returned page code for meta refresh (easy to detect) and possibly a javascript redirect (can be quite hard to detect) using regular expressions..

Posted: Sat Nov 05, 2005 6:33 pm
by surfinglight
timvw wrote:You could fe: use scriptable browser:

Code: Select all

require_once('simpletest/browser.php');
    
$browser = &new SimpleBrowser();
$browser->setMaximumRedirects(0);
$browser->get('http://example.com/link=1234');
$browser->getHeaders();
And test if there is a Location header present...

Thanks for your reply.
I have a question.
Where can I find the following file please?

'simpletest/browser.php'


feyd | it helps to have BBCode and Smilies enabled for the tags to work.. :P

Posted: Sat Nov 05, 2005 6:45 pm
by timvw
I think its mentionned on lastcrafts site...

Download PHP Simple Test

Thanks

Posted: Sat Nov 05, 2005 10:53 pm
by surfinglight
Hi,

Thank you very much for your swift reply.

Would there be any other way to implement it without using third party codes?

Many thanks.

Posted: Sat Nov 05, 2005 10:57 pm
by feyd
read my post..

Posted: Sun Nov 06, 2005 6:51 am
by surfinglight
[quote="feyd"]read my post..[/quote]


Hi,

Thanks for your kind help. Really helpful.

Since my current hosting does not support Php 5, I tried it as follows:
$test = "http://www.sample.com/process?id=3274 ";
$ch = curl_init($test);
$txt=curl_exec($ch);

echo $txt;

This outputs a message to click on an anchored text shown to redirect.
I can code to retireve the redirected url address from $txt.
But this is an extra coding.

Any other better way afterwards? I mean, without searching and retrieving from the $txt value.
Would there be any one call function to get the redirected address?

Many thanks to you all.

Posted: Sun Nov 06, 2005 8:37 am
by phpmaven
By your initial description you are looking up the id passed in the first url in a 'table' and getting the url to redirect to, but you want to try and figure out someway to see what url you are redirecting to? Am I missing something here? :?