F__M
F__M

Reputation: 1598

Check if a website is HTTP or HTTPS

How can I check is a website is secure with a HTTPS or insecure with a HTTP ?

Actually, my user need the enter their website like this : domain.com.

My system need to ping this domain and check if it's a HTTPS or a HTTP.

Thanks for your help.

Upvotes: 0

Views: 4015

Answers (3)

user6763587
user6763587

Reputation:

function check_https($url) {
  $ch = curl_init('https://' . $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD'); // it's a HEAD
  curl_setopt($ch, CURLOPT_NOBODY, true); // no body
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // in case of redirects
  curl_setopt($ch, CURLOPT_VERBOSE, 0); // turn on if debugging
  curl_setopt($ch, CURLOPT_HEADER, 1); // head only wanted
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // we don't want to wait forever
  curl_exec($ch);
  $header = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  // var_dump($header);
  if ($header === 0) { // no ssl
    return false;
  } else { // maybe you want to check for 200
    return true;
  }
}
var_dump(check_https("google.com"));

Upvotes: 2

Tom
Tom

Reputation: 4826

With the curl functions you can check:

  • if the website talks http on port 80
  • if the website talks https on port 443

But maybe it's not the same answer:

  • Some server answer the "public" content over http and the "private" content over https
  • Some server may answer something completely unrelated, because they choose to
  • Some sever may have port 443 incorrectly configured, and answer the default website of the webserver (could be anything, not related to the request, and probably with a wrong certificate)

Upvotes: 0

yeahright
yeahright

Reputation: 40

cURL both http:// and https://, return the one you'd like. The user can still enter the URL without the protocol.

Upvotes: -1

Related Questions