How to get Expiry date from the SSL Certificate file in PHP

前端 未结 3 1138
余生分开走
余生分开走 2021-02-02 00:19

I want to get the expiry date from the SSL Certificate file. There is a web page in PHP that I\'ve created, in which user can upload his SSL Certificate file and I will have to

相关标签:
3条回答
  • 2021-02-02 00:57

    The code below should help:

        $url = "https://www.google.com";
        $orignal_parse = parse_url($url, PHP_URL_HOST);
        $get = stream_context_create(array("ssl" => array("capture_peer_cert" => TRUE)));
        $read = stream_socket_client("ssl://".$orignal_parse.":443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $get);
        $cert = stream_context_get_params($read);
        $certinfo = openssl_x509_parse($cert['options']['ssl']['peer_certificate']);
    
        echo '<pre>';
        print_r($certinfo);
        echo '</pre>';
    

    The expiry date should be under $certinfo['validTo'] field.

    0 讨论(0)
  • 2021-02-02 01:05

    This worked for me:

    $url = "https://www.google.com";
    $orignal_parse = parse_url($url, PHP_URL_HOST);
    $get = stream_context_create(array("ssl" => array("capture_peer_cert" => TRUE)));
    $read = stream_socket_client("ssl://".$orignal_parse.":443", $errno, $errstr, 
    30, STREAM_CLIENT_CONNECT, $get);
    $cert = stream_context_get_params($read);
    $certinfo = openssl_x509_parse($cert['options']['ssl']['peer_certificate']);
    
    echo '<pre>';
    print_r($certinfo);
    echo '</pre>';
    

    fine ssl expire time in this fild

    [validTo_time_t] => 1523164135 // expire time.
    

    Convert Date & Time

    $valid_from = date(DATE_RFC2822,$certinfo['validFrom_time_t']);
    $valid_to = date(DATE_RFC2822,$certinfo['validTo_time_t']);
    echo "Valid From: ".$valid_from."<br>";
    echo "Valid To:".$valid_to."<br>";
    

    Result:

    Valid From: Mon, 09 Oct 2017 07:00:00 +0700 Valid To: Wed, 10 Oct 2018 06:59:59 +0700

    0 讨论(0)
  • 2021-02-02 01:14
    $certpath = "your_certificate.cer";
    $certinfo = openssl_x509_parse(file_get_contents($certpath));
    if( $certinfo['validFrom_time_t'] > time() || $certinfo['validTo_time_t'] < time() )
        print "Certificate is expired.";
    
    0 讨论(0)
提交回复
热议问题