如何检查域是否有SSL证书?

时间:2014-12-29 12:43:48

标签: php ssl https

是否可以获取详细信息,例如域名(例如www.example.com)是否已准备好HTTPS?

我需要验证一些网址,无论他们是否拥有SSL证书。我知道,通过使用$_SERVER['HTTPS'],我们可以查看我们的服务器详细信息。但我怎样才能在其他领域实现同样的目标。

非常感谢任何帮助。

5 个答案:

答案 0 :(得分:13)

最后,我最终得到了以下代码:

$stream = stream_context_create (array("ssl" => array("capture_peer_cert" => true)));
$read = fopen("https://www.example.com", "rb", false, $stream);
$cont = stream_context_get_params($read);
$var = ($cont["options"]["ssl"]["peer_certificate"]);
$result = (!is_null($var)) ? true : false;

如果为域启用了HTTPS,var_dump($var)会显示输出,如下所示:

resource(4) of type (OpenSSL X.509) 

如果它不存在,则返回NULL

我查了几个域名。它似乎工作正常。我希望它会帮助别人。

答案 1 :(得分:9)

此功能不仅会检查域是否具有SSL证书,还会确认证书是否与请求的域匹配。

最重要的部分是函数openssl_x509_parse,它解析证书并将所有细节作为数组返回。

function has_ssl( $domain ) {
    $res = false;
    $stream = @stream_context_create( array( 'ssl' => array( 'capture_peer_cert' => true ) ) );
    $socket = @stream_socket_client( 'ssl://' . $domain . ':443', $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $stream );

    // If we got a ssl certificate we check here, if the certificate domain
    // matches the website domain.
    if ( $socket ) {
        $cont = stream_context_get_params( $socket );
        $cert_ressource = $cont['options']['ssl']['peer_certificate'];
        $cert = openssl_x509_parse( $cert_ressource );

        // Expected name has format "/CN=*.yourdomain.com"
        $namepart = explode( '=', $cert['name'] );

        // We want to correctly confirm the certificate even 
        // for subdomains like "www.yourdomain.com"
        if ( count( $namepart ) == 2 ) {
            $cert_domain = trim( $namepart[1], '*. ' );
            $check_domain = substr( $domain, -strlen( $cert_domain ) );
            $res = ($cert_domain == $check_domain);
        }
    }

    return $res;
}

答案 2 :(得分:3)

这是我在github上找到的一个不同的解决方案(再也找不到回购......)

它与接受的答案类似,但如果我们可以在端口443上建立SSL连接,则使用fsockopen进行测试。如果连接被拒绝,则域没有ssl证书。

function has_ssl( $domain ) {
    $ssl_check = @fsockopen( 'ssl://' . $domain, 443, $errno, $errstr, 30 );
    $res = !! $ssl_check;
    if ( $ssl_check ) { fclose( $ssl_check ); }
    return $res;
}

// Test it:
print_r( has_ssl('google.com') );

答案 3 :(得分:0)

无法检查证书是否安全,或者没有API(绿色挂锁)单独使用PHP的情况

但是您可以使用它,它使用API​​:https://github.com/spatie/ssl-certificate

答案 4 :(得分:-1)

是的,您可以检查任何域是否具有SSL证书。

  • 在地址栏中检查HTTPS和绿色挂锁。
  • 使用SSL Checker查找有关SSL证书的详细信息
  • 使用强制SSL证书扩展名检查SSL证书(如 网站管理员忘记将HTTP URL重定向到HTTPS URL和Google 将这两个网站都视为单独的网站,因此强制URL可以做到这一点)
  • 尝试在地址栏上手动放置https
相关问题