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);
    return !!res

And here is some example usage:

$online = checkStatus('');

After the above code has executed, $online will equal true if 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;

Link to this section Conclusion

That’s all - I hope you found this useful!