Coding Triumph

Helpful code snippets & tutorials

How to check if a URL exists with PHP

The simplest way to find out if a URL still exists is to send a GET request to the URL and check the response status code: “200” means the URL is alive and “404” means the URL is dead. Let’s see how we can implement this process in PHP.

The get_headers() function:

Although there are many functions we can use to make an HTTP request in PHP, the get_headers() function is probably the most suitable for our use case since we’re only interested in the response status code and we want to prevent fetching the entire response body. Here is a simple function to demonstrate that:

 * Check if a given url is alive
 * @return bool - true if 200, false otherwise
function urlExists(string $url): bool
    $headers = get_headers($url);
    return  $headers && strpos($headers[0], '200');
If you like this post, please share
Notify of
Inline Feedbacks
View all comments