![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Aug 2005
Posts: 19
Rep Power: 0
![]() |
Final Destination.
Anybody know how give a URL such as
http://www.webhostdir.com/banners/banman.asp?ZoneID=9&Task=Click&Mode=HTML&SiteID=1&PageID= How one can find the final destination which in this case is http://www.webintellects.com/ Bearing in mind there are probably lots of ways that a link could be redirected to the final site. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
One would have to open up a HTTP connection to the URL, and then get the value of the redirect. PHP has some HTTP handling functions, and it's sometimes compiled with CURL, which might be easier. I suspect it would just be a case of parsing out the right header, or having a library follow the link and see where it goes.
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2006
Posts: 16
Rep Power: 0
![]() |
This might help you a little bit. It's a sloppy function I created a while back that grabs the contents of a document using HTTP via sockets. It follows 301 and 302 redirects. I've tweaked it a bit for your situation.
function get_http_document($url, &$headers = null, $port = 80, $timeout = 8) {
global $paths;
$paths[] = $url;
$pURL = parse_url($url);
if (empty($pURL['host'])) {
return false;
}
$remotePath = (isset($pURL['path'])) ? $pURL['path'] : '/';
$remoteDocument = (empty($pURL['query'])) ? $remotePath : $remotePath . '?' . $pURL['query'];
if (!$fp = @fsockopen($pURL['host'], $port, $errno, $errstr, $timeout)) {
return false;
}
$out = "GET $remoteDocument HTTP/1.0\r\n";
$out .= "Host: {$pURL['host']}\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
unset($out);
$received = '';
while (!feof($fp)) {
$received .= fread($fp, 128);
}
fclose($fp);
// Seperate the headers from the content
$parts = explode("\r\n\r\n", $received, 2);
$headers = $parts[0];
$content = $parts[1];
unset($parts);
$headerParts = explode("\r\n", $headers);
if (!preg_match('~HTTP/1\.\d ([\d]+)~i', $headerParts[0], $matches)) {
return false;
}
$statusCode = $matches[1];
if ($statusCode == 200) {
return $content;
} else if ($statusCode != 301 && $statusCode != 302) {
return false;
}
if (!preg_match('~^Location:(.*)$~im', $headers, $matches)) {
return false;
}
$newLocation = trim($matches[1]);
return get_http_document($newLocation, $headers, $port, $timeout);
}Be aware this uses the global variable $paths inside the function. That's just bad coding. But you would use the function like this:$paths = array();
$content = get_http_document('http://www.webhostdir.com/banners/banman.asp');
print_r($paths);The contents of the remote document will be in $content. Every website that was visited to get to the final destination will be in the $paths array. That array will look like this: Array
(
[0] => http://www.webhostdir.com/banners/banman.asp?ZoneID=9&Task=Click&Mode=HTML&SiteID=1&PageID=
[1] => http://www.webintellects.com
)That shows you that the first site visited was webhostdir.com. Then it went to webintellects.com. And so on. - Sean |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Instant Messaging System Final Year Project | bondito | Java | 20 | Oct 28th, 2007 11:08 PM |
| Simple java problem | Mixmaster | Java | 3 | Oct 3rd, 2006 2:57 PM |
| declaring a variable final | b1g4L | C++ | 5 | Jan 19th, 2006 11:41 AM |
| Final Year Dissitation | Berto | Coder's Corner Lounge | 4 | Jun 14th, 2005 6:36 AM |
| University Final Year Project | nez | Project Ideas | 3 | May 23rd, 2005 8:59 AM |