Jump to content

Grabing final URL of redirect


pink

Recommended Posts

Hello.

I was wondering if anyone knows how to grab an URL that is re-directed many times.  Forexample, if I create a pageA that have header("Location pageB.php"), and page B redirect to page C.

My goal is to get URL of page C from page A.  You probably have experiences that URL that you've typed in and final destination URL are different.  Often happens for JSP or .NET site. 

I am looking for way to get the final URL (page C) without hard coding it from initial URL (page A).  I've tried with <a href="http://us3.php.net/manual/en/function.fread.php">fread</a> but it grabs HTML of inital page (page A). 

Has anyone tried this?  If so, were you successful?  What was your solutions?

Thank you!!!!!
Link to comment
https://forums.phpfreaks.com/topic/26061-grabing-final-url-of-redirect/
Share on other sites

If [url=http://www.php.net/curl_exec]curl[/url] is installed you can use the following to retrieve the page.

[code]
<?php
$url = 'http://www.com';
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$r = curl_exec($ch);
?>
[/code]

You should also be able to use file_get_contents to retrieve the final page.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.