Jump to content

Check a website status


themistral

Recommended Posts

Hi guys,

 

I run a few niche directory websites and I wanted to make sure they never get outdated.

With that in mind, I was thinking of running a check once a month to make sure all websites in the database return a 200 status.

If not they get flagged for me to look at.

Hopefully this means I won't have a directory of outdated links!

 

Firstly, is there any ethical reason not to check the status of someone else's website? It will only be once a month, so should not impact on server load for the sites I have listed.

 

Secondly, I have no idea how to do this! The only way I've found is to look for an image on the target website.

Well, I don't make people link back to me, and in fact, quite often site owners are unaware they are listed as anyone can suggest a relevant website be listed.

 

Can anyone point me in the right direction?

 

Thanks!  :D

 

Link to comment
https://forums.phpfreaks.com/topic/199000-check-a-website-status/
Share on other sites

$url='http://www.google.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
preg_match('*HTTP\/1\.1\s([0-9]{3})*is', $result, $status);
echo "$status[1]";

You can do this without cURL.

<?php
function httpHeader($server) {
$fp = @fsockopen($server,80,$errno,$errstr,30);
$out  = "GET / HTTP/1.1\r\n"; 
$out .= "Host: $server\r\n"; 
$out .= "Connection: Close\r\n\r\n";
@fwrite($fp,$out);
$content = @fgets($fp);
if(strstr($content, "200") || strstr($content, "302")) {
	return true;
}
return false;
}


if(httpHeader("www.google.com")) {
print "url ok";
}
else {
print "bad url";
}	
?>

  Quote

neil.johnson - I don't know a lot about CURL, so is there any reason to prefer your way over CURL?

 

In theory they are the same except that neil's solution will work when cURL is not enabled.

 

@neil I chose the wrong words it should have been "You can use" instead of "You'll need"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.