Web Hosting Talk







View Full Version : Make PHP check links


Powi
06-10-2004, 11:57 AM
Is there any way to make PHP check if a link before it displays it? I mean, make a script enter a site, check for an error (404 error) or check to see if the site displays ok.

TechSolution
06-10-2004, 12:03 PM
Sure, open up an HTTP connection to the server the link is going to and make a "HEAD" request. If you get a 404, don't show the link.

glookihost
06-10-2004, 02:50 PM
you could also use fsockopen.


<?php

$website = "web addy";
$connect = @fsockopen($website, 80);

if($connect == TRUE){

//display link

} else {

//dont display it

}
?>


hope it helped

Powi
06-10-2004, 05:27 PM
Originally posted by TechSolution
Sure, open up an HTTP connection to the server the link is going to and make a "HEAD" request. If you get a 404, don't show the link.

Can you give me some code example? I don't know how to make php send an HTTP request and check for a 404 error.


glookihost, fsockopen() only lets me check if the domain (domain.com or www.domain.com) works I need to check a page (www.domain.com/page.html). Any more ideas?

Thanks all of your for the help!

Dan Grossman
06-10-2004, 06:15 PM
Grab the Snoopy.class.php file from the Snoopy project:

http://sourceforge.net/projects/snoopy/


include("Snoopy.class.php");
$snoopy = new Snoopy;
$snoopy->fetch("http://www.example.com/page.html");
$html = $snoopy->results;
//$html now contains the HTML of example.com/page.html, which you can check for a 404 error or otherwise


Using things like $snoopy->fetchlinks(), you can easily create your own link checking spider or whatever you need to do. Snoopy is very versatile.

Powi
06-10-2004, 09:53 PM
Thanks! that helps a lot, how can I check for an html 404 error, just search for 404 with ereg()?

Dan Grossman
06-10-2004, 10:12 PM
Yes, you can just do eregi("404",$html); to check for 404 anywhere in the result page from the fetch, if you know the page doesn't have 404 in it when it's actually up.

Powi
06-10-2004, 10:40 PM
Got it, thanks!