How to check if a https site exists in php?

by muriel.schmidt , in category: Third Party Scripts , 9 months ago

How to check if a https site exists in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by darrion.kuhn , 9 months ago

@muriel.schmidt 

You can use the get_headers function in PHP to check if an HTTPS site exists. Here is an example code snippet to demonstrate this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
function checkHttpsSiteExists($url) {
    $headers = get_headers($url);
    
    if ($headers && strpos($headers[0], '200') !== false) {
        // Site exists and returns a 200 status code
        return true;
    } else {
        // Site does not exist or returns a different status code
        return false;
    }
}

$url = 'https://example.com';

if (checkHttpsSiteExists($url)) {
    echo 'HTTPS site exists!';
} else {
    echo 'HTTPS site does not exist!';
}


In this code snippet, the checkHttpsSiteExists function takes a URL as a parameter and uses the get_headers function to get the headers of the site. It then checks if the status code in the headers contains '200', which indicates that the site exists. If the site exists, the function returns true; otherwise, it returns false.


You can call this function with the URL of the HTTPS site you want to check, and it will return a message indicating whether the site exists.

Related Threads:

How to check if file exists in PHP?
How to check if file exists in laravel?
How to check if a record exists in octobercms?
How to check if a message exists in discord.js?
How to check if a subreddit exists in discord.js?
How check if a laravel console command exists?