Page 1 of 1

404 detection

Posted: Wed Oct 04, 2006 4:52 pm
by Longlands
I have a script that I need to be able to detect 404 pages.

I've been using file-get-contents(url) but it returns an empty string on any page that has a redirect - rather than follow the redirect and return the code of the destination page.

In a sense, I can use the empty string returned as an indicator of a 404 page, but it isn't precise, and returns many false positives.

Am I missing something, or am I just using the wrong function?

Martin.

Posted: Wed Oct 04, 2006 5:53 pm
by ambivalent
Why not just code a custom 404 page and have it do whatever it needs to when/if called?

Posted: Thu Oct 05, 2006 1:17 am
by Longlands
I didn't explain very well - sleep deprivation will do that to you!

My script takes a list of urls and then tries to check each one individually to see if it is a 404 page.

That's where it falls down because php file_get_contents is reading the source code of the target url and not the 404 page that is being redirected to.

What I think I need is some way to detect if a target url is redirecting and then read the source of the page it is redirecting to - to check if it is a 404 or not.

But the problem is that when file_get_contents places the source code of a page that redirects into a string, that string returns empty.

Martin.

Posted: Thu Oct 05, 2006 5:34 am
by volka
requires php 4.3+

Code: Select all

<?php
$fp = fopen('http://www.php.net', 'r');
$meta = stream_get_meta_data($fp);

print_r($meta);
fclose($fp);
or http://pear.php.net/manual/en/package.h ... equest.php

Code: Select all

<?php
require_once 'HTTP/Request.php';

$request =new HTTP_Request('http://www.php.net');
$err = $request->sendRequest();
if (PEAR::isError($err)) {
	echo 'error:', $err->getMessage();
}
else {
	echo 'Code: ', $request->getResponseCode(), "<br />\n",
		htmlentities($request->getResponseBody());
}
?>