Jump to content

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";
}	
?>

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"

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.