404 detection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Longlands
Forum Newbie
Posts: 15
Joined: Tue Oct 25, 2005 3:36 am

404 detection

Post 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.
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Post by ambivalent »

Why not just code a custom 404 page and have it do whatever it needs to when/if called?
Longlands
Forum Newbie
Posts: 15
Joined: Tue Oct 25, 2005 3:36 am

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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());
}
?>
Post Reply