Simple Website Status Checker in PHP
- 1 minute read
Here is a simple website status checker built using PHP’s built-in cURL library:
function checkStatus($url) {
$req = curl_init($url);
curl_setopt($req, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($req, CURLOPT_HEADER, true);
curl_setopt($req, CURLOPT_NOBODY, true);
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($req);
curl_close($req);
return !!res
}
And here is some example usage:
$online = checkStatus('https://example.com');
After the above code has executed, $online
will equal true
if
https://example.com is online, and it will otherwise return false.
If you wanted to be a bit more sophisticated, you could check for the returned HTTP code.
This way, even if the website is online, you can check whether there is an error code:
$code = curl_getinfo($req, CURLINFO_HTTP_CODE);
if (!!$res && ($code >= 200 && $code < 300)) {
return true;
} else {
return false;
}
Conclusion
That’s all - I hope you found this useful!