简体   繁体   English

检查网站是 HTTP 还是 HTTPS

[英]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 ?如何检查网站是使用HTTPS安全还是使用HTTP不安全?

Actually, my user need the enter their website like this : domain.com .实际上,我的用户需要像这样输入他们的网站: domain.com

My system need to ping this domain and check if it's a HTTPS or a HTTP .我的系统需要ping这个域并检查它是HTTPS还是HTTP

Thanks for your help.谢谢你的帮助。

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"));

With the curl functions you can check:使用 curl 函数,您可以检查:

  • if the website talks http on port 80如果网站在端口 80 上使用 http
  • if the website talks https on port 443如果网站在端口 443 上使用 https

But maybe it's not the same answer:但也许答案不一样:

  • Some server answer the "public" content over http and the "private" content over https某些服务器通过 http 回答“公共”内容,通过 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)一些服务器可能443端口配置不正确,并回答web服务器的默认网站(可能是任何事情,与请求无关,可能证书错误)

cURL both http:// and https://, return the one you'd like. cURL http:// 和 https://,返回你想要的那个。 The user can still enter the URL without the protocol.用户仍然可以在没有协议的情况下输入 URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM