php判断是否SSL协议

/**
 * 判断是否SSL协议。此代码来自ThinkPHP框架的公共函数文件(ThinkPHP/Common/functions.php)。
 * @return boolean
 */
function is_ssl() {
    if(isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))){
        return true;
    }elseif(isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'] )) {
        return true;
    }
    return false;
}

以下是一个基于PHP+HTML的在线检测SSL证书工具的示例代码,可以检测证书的状态和TLS协议版本等信息: ``` <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>SSL证书检测工具</title> </head> <body> <h1>SSL证书检测工具</h1> <form method="post"> <label for="domain">请输入域名:</label> <input type="text" id="domain" name="domain"> <input type="submit" value="检测"> </form> <?php if(isset($_POST['domain'])) { $domain = $_POST['domain']; $url = "https://" . $domain; $context = stream_context_create(array("ssl" => array("capture_peer_cert" => true))); $read = stream_socket_client($url . ":443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context); $cert = stream_context_get_params($read)["options"]["ssl"]["peer_certificate"]; $certinfo = openssl_x509_parse($cert); $validFrom = date("Y-m-d", $certinfo['validFrom_time_t']); $validTo = date("Y-m-d", $certinfo['validTo_time_t']); $cipher = stream_get_meta_data($read)["crypto"]["protocol"]; $tlsVersion = ""; if(strpos($cipher, "TLSv1.3") !== false) { $tlsVersion = "TLSv1.3"; } elseif(strpos($cipher, "TLSv1.2") !== false) { $tlsVersion = "TLSv1.2"; } elseif(strpos($cipher, "TLSv1.1") !== false) { $tlsVersion = "TLSv1.1"; } elseif(strpos($cipher, "TLSv1") !== false) { $tlsVersion = "TLSv1.0"; } echo "<h2>证书状态:</h2>"; if(strtotime($validTo) < time()) { echo "<p style='color:red;'>证书已过期!</p>"; } else { echo "<p style='color:green;'>证书有效!</p>"; } echo "<h2>TLS协议版本:</h2>"; echo "<p>" . $tlsVersion . "</p>"; stream_socket_shutdown($read, STREAM_SHUT_RDWR); } ?> </body> </html> ``` 该示例代码包含一个表单,用户可以在表单中输入要检测的域名。当用户提交表单后,PHP代码将获取该域名的SSL证书并解析证书信息,然后判断证书是否已过期,并检测使用的TLS协议版本。最后,PHP代码将输出检测结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值