View Full Version : Detecting potential URL redirection when using fopen()?
Hi:
I'm fairly certain this is possible- how do I detect potential URL redirection when using fopen() to open an external page/domain? For example, some URLs will redirect to a 404 page not found when the actual URL no longer exists, though fopen() would still return true in this case (Based on my understanding. Please correct if wrong).
Thanks for any insight. Somewhere in the back of my mind I recall having to send headers in order to detect the above, though obviously am not too sure or how to go about it.
firepages
11-11-2002, 01:50 PM
allo, if you talk HTTP to the server then the first line returned should be the HTTP status code, i.e. 200 OK for a page that is where it is supposed to be.
In the code below the url opened (/threads2/) is redirected and the status returned is "HTTP/1.1 302 Found"
<?
$fp = fsockopen ("www.firepages.com.au", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br>\n";
} else {
/* threads2/ is empty apart from a PHP header to the right place*/
fputs ($fp, "GET /threads2/ HTTP/1.0 \r\n\r\n");
echo fgets ($fp,1024);
fclose ($fp);
}
?>
PHP is supposed to set a 301 "Moved Permanently" header but it does not always appear to so expect any 3xx header.
The problem is that a page not found (404) may give the same result ("HTTP/1.1 302 Found") as the request may be redirected by apache to a user-deined error page.
If you are going to be doing a lot of this stuff then I would really suggest you take a look at snoopy (http://snoopy.sourceforge.net) which takes away a lot of the headaches when grabbing stuff from remote hosts, it handles all the HTTP and socket stuff and makes life generally easier
<?
include "Snoopy.class.inc";
$snoopy = new Snoopy;
if($snoopy->fetch("http://codingforums.com"))
{
echo "response code: ".$snoopy->response_code."<br>\n";
$grabbed_page=$snoopy->results;
}else{
echo $snoopy->errors;
}
echo htmlspecialchars($grabbed_page);
?>
Thanks firepages! I'm definitely going to read your post my closely, and see if I can take it from there. :)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.