检查网站是HTTP还是HTTPS。

问题描述 投票:-3回答:3

我如何通过以下方法检查网站是否安全?HTTPS 或不安全 超文本传输协议 ?

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

我的系统需要 这个域名,并检查它是否是一个 HTTPS超文本传输协议.

谢谢你的帮助。

php http https
3个回答
2
投票
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"));

0
投票

用curl函数可以检查。

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

但也许是不一样的答案。

  • 有些服务器通过http回答 "公开 "的内容 通过https回答 "私人 "的内容
  • 有些服务器可能会回答一些完全不相关的东西,因为他们选择了
  • 有些server可能端口443配置错误,并回答webserver的默认网站(可能是任何东西,与请求无关,可能是用了错误的证书)。

-1
投票

cURL既有http:/又有https:/,返回你喜欢的那个。用户仍然可以输入没有协议的URL。

© www.soinside.com 2019 - 2024. All rights reserved.