Jump to content


Photo

Get URL after its been redirected


  • Please log in to reply
1 reply to this topic

#1 Pythondev

Pythondev
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 23 June 2006 - 09:54 PM

Ok,

What I have is this... I have a link which when clicks redirects you to another page... For example lets say this link is example.com/website/hello/329472 and this redirects to domain.com/page.html

What I want to do is have a script where you type in the original url, in this case example.com/website/hello/329472 and it finds out what the page that it redirects to is...

This needs to be done with no user input though apart from typing in the original URL. and if possible id like it so it doesnt show the website loading or anything...

How can I do this?

Thanks

#2 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 23 June 2006 - 11:59 PM

Note that the e in "curl_exec" is removed from the code below to allow the post to be accepted on the board.
<?php

$ch = curl_init();
$url = 'http://example.com/website/hello/329472';

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

$r = curl_xec($ch);
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close ($ch);

print $url;
?>
http://php.net/curl

If curl isn't installed you can do this manually using fsockopen and complimentary functions by doing the HTTP request manually. There should be examples in the "User Contributed Notes" that you can look at.

You should also be able to use fopen and stream_get_meta_data to follow the redirects.

If this "redirect" is done internally by Apache using ModRewrite I don't know how you'd get the url. Perhaps by parsing a log file with ModRewrite debug info.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users