themistral Posted April 19, 2010 Share Posted April 19, 2010 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! Link to comment https://forums.phpfreaks.com/topic/199000-check-a-website-status/ Share on other sites More sharing options...
ignace Posted April 19, 2010 Share Posted April 19, 2010 You'll need cURL to read out HEAD response headers check for HTTP/1.x 200 OK http://icfun.blogspot.com/2008/07/php-get-server-response-header-by.html Link to comment https://forums.phpfreaks.com/topic/199000-check-a-website-status/#findComment-1044556 Share on other sites More sharing options...
themistral Posted April 19, 2010 Author Share Posted April 19, 2010 Thanks ignace! I will check it out! Link to comment https://forums.phpfreaks.com/topic/199000-check-a-website-status/#findComment-1044563 Share on other sites More sharing options...
teamatomic Posted April 19, 2010 Share Posted April 19, 2010 $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]"; Link to comment https://forums.phpfreaks.com/topic/199000-check-a-website-status/#findComment-1044566 Share on other sites More sharing options...
JonnoTheDev Posted April 19, 2010 Share Posted April 19, 2010 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"; } ?> Link to comment https://forums.phpfreaks.com/topic/199000-check-a-website-status/#findComment-1044621 Share on other sites More sharing options...
themistral Posted April 20, 2010 Author Share Posted April 20, 2010 Thanks teamatomic - it works great! neil.johnson - I don't know a lot about CURL, so is there any reason to prefer your way over CURL? Thanks for the responses - much appreciated! Link to comment https://forums.phpfreaks.com/topic/199000-check-a-website-status/#findComment-1045140 Share on other sites More sharing options...
Ken2k7 Posted April 20, 2010 Share Posted April 20, 2010 themistral, for a simple check, fsockopen is more ideal because it is simpler and less complex than cURL. It's also less intensive. But if you need to do more complex stuff, cURL would be the way to go. Link to comment https://forums.phpfreaks.com/topic/199000-check-a-website-status/#findComment-1045162 Share on other sites More sharing options...
ignace Posted April 20, 2010 Share Posted April 20, 2010 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" Link to comment https://forums.phpfreaks.com/topic/199000-check-a-website-status/#findComment-1045166 Share on other sites More sharing options...
themistral Posted April 20, 2010 Author Share Posted April 20, 2010 Thanks for the help guys! Link to comment https://forums.phpfreaks.com/topic/199000-check-a-website-status/#findComment-1045311 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.