繁体   English   中英

检查网站是 HTTP 还是 HTTPS

[英]Check if a website is HTTP or HTTPS

如何检查网站是使用HTTPS安全还是使用HTTP不安全?

实际上,我的用户需要像这样输入他们的网站: domain.com

我的系统需要ping这个域并检查它是HTTPS还是HTTP

谢谢你的帮助。

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

使用 curl 函数,您可以检查:

  • 如果网站在端口 80 上使用 http
  • 如果网站在端口 443 上使用 https

但也许答案不一样:

  • 某些服务器通过 http 回答“公共”内容,通过 https 回答“私人”内容
  • 一些服务器可能会回答一些完全不相关的问题,因为他们选择
  • 一些服务器可能443端口配置不正确,并回答web服务器的默认网站(可能是任何事情,与请求无关,可能证书错误)

cURL http:// 和 https://,返回你想要的那个。 用户仍然可以在没有协议的情况下输入 URL。

暂无
暂无

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

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