Reputation: 1598
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
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
Reputation: 4826
With the curl functions you can check:
But maybe it's not the same answer:
Upvotes: 0
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