有可能获得详细信息,如果域(说www.example.com)是否准备好HTTPS?
我需要验证一些URL,无论他们是否有SSL证书.我知道,通过使用$_SERVER['HTTPS']我们可以查看我们的服务器详细信息 但我怎样才能在其他领域实现同样的目标.
任何帮助将非常感激.
Kri*_*han 14
最后,我最终得到了以下代码:
$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;
Run Code Online (Sandbox Code Playgroud)
如果为域启用了HTTPS,var_dump($var)则输出如下所示:
resource(4) of type (OpenSSL X.509)
Run Code Online (Sandbox Code Playgroud)
如果它不存在则返回NULL.
我查了几个域名.它似乎工作正常.我希望它能帮助别人.
Phi*_*ipp 10
此功能不仅会检查域是否具有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;
}
Run Code Online (Sandbox Code Playgroud)
这是我在 github 某处找到的不同解决方案(再次找不到 repo...)
这是与接受的答案类似的方法,但使用 fsockopen 来测试我们是否可以在端口 443 上建立 SSL 连接。如果连接被拒绝,则域没有 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') );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10426 次 |
| 最近记录: |