Check If Website Is Available: A Developer's Guide

Learn how to programmatically verify website availability using PHP, from basic cURL implementations to optimized bulk checking solutions that can efficiently monitor thousands of sites.

Website availability checking is a fundamental task for developers who manage multiple sites, build monitoring dashboards, or need to verify external services. Whether you're maintaining a portfolio of client websites, integrating with third-party APIs, or building uptime monitoring tools, understanding how to programmatically verify that a website is accessible is an essential skill.

This guide explores multiple approaches to checking website availability using PHP, from basic cURL implementations to optimized solutions that can efficiently check thousands of sites without timing out. These techniques form the foundation of robust web development practices for maintaining healthy digital infrastructure.

Basic PHP Methods to Check Website Availability

Using cURL for Website Availability Checks

The cURL library is the most robust and flexible method for checking website availability in PHP. It provides granular control over the HTTP request, allowing you to configure timeouts, headers, and request methods while providing detailed response information. A basic cURL implementation checks whether a URL is accessible by connecting to the server and examining the response. The key advantage of cURL is its ability to distinguish between different types of failures--you can tell whether a site is completely unreachable, returning an error page, or redirecting to a different location.

The fundamental cURL approach involves initializing a handle, configuring options like connection timeout and whether to retrieve headers only, executing the request, and then checking the HTTP status code in the response. A status code in the 200 range typically indicates the site is available and responding correctly, while 300-range codes suggest redirects, 400-range codes indicate client errors, and 500-range codes point to server problems. This granular information allows you to make informed decisions about what constitutes "available" for your specific use case, which is essential for professional PHP development services.

Alternative PHP Methods

While cURL is the recommended approach, PHP provides alternative methods for checking website availability that may suit simpler use cases or environments where cURL is not available. The get_headers function fetches all headers sent by the server in an array, allowing you to inspect the HTTP response without downloading content. This method is simpler but offers less control over timeouts and connection behavior. The fopen wrapper with allow_url_fopen enabled can open a URL like a file and return a handle that you can read from, though this method downloads content which is inefficient for availability checking.

The fsockopen function provides low-level socket access to connect to a server and send raw HTTP requests. This method offers maximum control but requires manually constructing HTTP request strings and parsing responses. For most web development applications, cURL strikes the best balance between control, reliability, and ease of use.

Basic cURL Availability Check
function isWebsiteAvailable($url) {
 if (!filter_var($url, FILTER_VALIDATE_URL)) {
 return false;
 }
 
 $ch = curl_init($url);
 curl_setopt_array($ch, [
 CURLOPT_CONNECTTIMEOUT => 10,
 CURLOPT_TIMEOUT => 30,
 CURLOPT_HEADER => true,
 CURLOPT_NOBODY => true,
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_FOLLOWLOCATION => true,
 CURLOPT_USERAGENT => 'WebsiteAvailabilityChecker/1.0',
 CURLOPT_SSL_VERIFYPEER => true,
 CURLOPT_SSL_VERIFYHOST => 2
 ]);
 
 $response = curl_exec($ch);
 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 $error = curl_error($ch);
 curl_close($ch);
 
 if ($error) {
 error_log("cURL error for $url: $error");
 return false;
 }
 
 return ($httpCode >= 200 && $httpCode < 400);
}

Understanding HTTP Status Codes for Availability

What Different Status Codes Mean

HTTP status codes are three-digit responses from servers that indicate the result of your request. For website availability checking, the most important distinction is between sites that are genuinely down versus sites that are functioning but returning error pages. Understanding these codes is essential for building availability checks that match your definition of "available."

  • 2xx (Success): The page loaded successfully--the site is definitely available
  • 3xx (Redirect): The site is available but has moved to a new location
  • 4xx (Client Error): The specific page is missing but the server is running
  • 5xx (Server Error): The server failed to fulfill a valid request

A smart availability checker provides meaningful information about what kind of response was received rather than treating any non-200 response as unavailable. For example, a redirect might be intentional and healthy, while a 503 Service Unavailable response with a Retry-After header indicates the server is temporarily overloaded but expects to recover. This level of detail is crucial for effective website maintenance and monitoring.

Performance Optimization: Checking Multiple Websites

The Challenge of Bulk Availability Checking

When you need to check hundreds or thousands of websites, the sequential approach of making one request after another becomes prohibitively slow. With each site potentially taking 10-30 seconds to respond (or time out), checking 3,000 sites sequentially could take hours. This is where performance optimization becomes essential for any enterprise web solution.

Multi-curl, PHP's implementation of curl_multi functions, allows you to execute multiple cURL handles simultaneously. The technique works by adding individual cURL handles to a multi-handle, running them all at once, and then extracting results as each completes. While not true parallel execution in the threading sense, the approach makes efficient use of network time by starting the next request while waiting for the previous one to complete, achieving significant speedups in practice.

Multi-Curl Bulk Website Checker
class BulkWebsiteChecker {
 private $maxConcurrent = 10;
 private $timeout = 10;
 
 public function checkMultiple(array $urls) {
 $multiHandle = curl_multi_init();
 $handles = [];
 $results = [];
 
 foreach ($urls as $index => $url) {
 $ch = curl_init($url);
 curl_setopt_array($ch, [
 CURLOPT_CONNECTTIMEOUT => $this->timeout,
 CURLOPT_TIMEOUT => $this->timeout,
 CURLOPT_NOBODY => true,
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_FOLLOWLOCATION => true
 ]);
 $handles[$index] = ['handle' => $ch, 'url' => $url];
 curl_multi_add_handle($multiHandle, $ch);
 }
 
 $running = null;
 do {
 curl_multi_exec($multiHandle, $running);
 curl_multi_select($multiHandle);
 } while ($running > 0);
 
 foreach ($handles as $index => $data) {
 $ch = $data['handle'];
 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 $error = curl_error($ch);
 $results[$index] = [
 'url' => $data['url'],
 'available' => empty($error) && $httpCode >= 200 && $httpCode < 400,
 'status_code' => $httpCode
 ];
 curl_multi_remove_handle($multiHandle, $ch);
 curl_close($ch);
 }
 
 curl_multi_close($multiHandle);
 return $results;
 }
}

Timeout Configuration and Error Handling

Why Timeouts Matter

Proper timeout configuration is critical for reliable website availability checking. Without timeouts, your script can hang indefinitely waiting for a slow or unresponsive server. This affects not just the individual request but can cascade into problems for other parts of your application. Timeouts should be set based on the type of check being performed and the expected response characteristics of the websites being monitored.

  • Connection timeout: Limits how long to wait when establishing a connection to the server
  • Read timeout: Limits how long to wait for data after a connection is established

By capturing and categorizing different error types (DNS failures, SSL errors, connection timeouts, read timeouts), your availability checker provides actionable information for remediation rather than generic failure messages. This level of error categorization is essential for building robust monitoring systems as part of comprehensive digital operations.

Key Techniques for Reliable Website Checking

Essential practices for building robust availability monitoring

Use cURL for Reliability

cURL provides the best balance of control, flexibility, and detailed response information for HTTP checking in PHP.

Configure Appropriate Timeouts

Set connection and read timeouts to balance responsiveness with reliability--typically 8-10 seconds for connection, 15-30 seconds for read.

Parallelize Bulk Checks

Use multi-curl to check multiple websites simultaneously, reducing check time from hours to minutes.

Handle Errors Gracefully

Distinguish between different error types (DNS, SSL, timeout, HTTP errors) to provide actionable remediation information.

Integrate with Alerting

Connect availability checks with notification systems to respond quickly when sites go down.

Validate Content When Needed

For critical services, verify that expected content is present beyond just checking HTTP status codes.

Need Help Building Monitoring Solutions?

Our web development team can build custom monitoring systems tailored to your infrastructure and business requirements.

Frequently Asked Questions

What is the fastest way to check multiple websites in PHP?

Multi-curl (curl_multi_init, curl_multi_exec, curl_multi_select) is the fastest approach, allowing parallel execution of multiple requests. For 3,000+ sites, batch processing with 10-20 concurrent connections per batch provides optimal performance.

How do I handle SSL certificate errors in availability checks?

Set CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST appropriately. For internal monitoring, you might disable verification, but for production monitoring, valid SSL should be part of your availability criteria.

What HTTP status codes indicate a website is available?

Typically 2xx (success) and 3xx (redirect) codes indicate availability. Whether redirects are acceptable depends on your use case--follow redirects with CURLOPT_FOLLOWLOCATION if the final destination matters.

How often should I check website availability?

Critical services may need checking every minute, while less critical sites can be checked hourly. Consider the cost of downtime versus the overhead of checking when setting frequencies.

Can I check if specific content is present on a page?

Yes, fetch the full content (not just headers) and search for expected keywords or patterns. This catches cases where the server returns a 200 status but displays an error page.

Sources

  1. Stack Overflow - Checking if a website is online using a PHP Script - Core cURL implementation with timeout issues and multi-curl solution for bulk checking
  2. UptimeRobot Knowledge Hub - Ultimate Guide to Uptime Monitoring Types - Comprehensive monitoring types: HTTP, Ping, DNS, Keyword monitoring with pairing strategies
  3. Lagnis Blog - Uptime Monitoring Best Practices 2025 - Multi-layer monitoring strategy, business impact, performance optimization techniques