Software, your way.
burger menu icon
WillMaster

WillMaster > LibrarySnooping (Information Retrieval)

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Check SSL Certificate

A few days ago, spamfreeform.com's SSL certificate expired. The hosting company was supposed to reissue the certficate. Their automated software failed. The site was unusable for a few hours.

(An SSL certificate is a file installed on a server with information for encrypting data between a user's browser and a website's server — when the URL begins with https://.)

My own SSL Certificate checking software was a bit klutzy to use, so I hadn't stayed on top of it. My bad.

The software is now updated so it's a pleasure to use. The source code is further below.

It is PHP software. The source code can be uploaded to your server as you find it here. In other words, no customization is required.

There is also a live implementation of the same source code. Test it. Use it to see how it works.

Type in a domain name. If the certificate is for a subdomain, then include the subdomain, www.domain.com for example. Protocol https:// is optional.

Tap the button and you get the certificate expiration date (assuming there is a certificate).

Want more certificate information? Tap the link below the expiration date. You'll end up with all the SSL Certificate information that could be found.

Here is the source code.

<?php
/*
   SSL Certificate Check
   Version 1.2
   January 8, 2024

   Version 1.0 created May 19, 2016 as SSL Certificate Information
   Version 1.1 updated March 25, 2019 has updated interface.
   Version 1.2 updaed January 8, 2024 was renamed as SSL Certificate Check and has updated interface.

   Will Bontrager Software LLC
   http://www.willmaster.com/
*/
ini_set('display_errors', 1);
$Information = array();
$CertsCount = 0;
$postURL = '';
$_POST['GET'] = false;
if( empty($_POST['URL']) )
{
   if( isset($_GET['URL']) )
   {
      $_POST['URL'] = $_GET['URL'];
      $_POST['GET'] = true;
      $_GET = array();
   }
   else { $_POST['URL'] = ''; }
}
if( preg_match('/\w\.\w/',$_POST['URL']) )
{
   $postURL = preg_replace('!^https?://!i','',$_POST['URL']);
   $postURL = preg_replace('!/.*$!','',$postURL);
   $postURL = "https://$postURL";
   $options = array(
      CURLOPT_VERBOSE        => true,
      CURLOPT_CERTINFO       => true,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_HEADER         => false,
      CURLOPT_CONNECTTIMEOUT => 120,
      CURLOPT_TIMEOUT        => 120,
      CURLOPT_FOLLOWLOCATION => false,
      CURLOPT_SSL_VERIFYHOST => 0,
      CURLOPT_SSL_VERIFYPEER => false
   );
   $ch = curl_init($postURL);
   curl_setopt_array($ch,$options);
   curl_exec($ch);
   $err = curl_errno($ch);
   $errmsg = curl_error($ch) ;
   $Information = curl_getinfo($ch);
   curl_close($ch);
   $Information['errno']   = $err;
   $Information['errmsg']  = $errmsg;
   if( isset($Information['redirect_url']) and preg_match('!^https://!i',$Information['redirect_url']) ) { $postURL = $Information['redirect_url']; }
   $CertsCount = empty($Information['certinfo']) ? 0 : count($Information['certinfo']);
}
?><!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SSL Certificate Check</title>
<style type="text/css">
* { box-sizing:border-box; }
html, body { font-size:100%; font-family:sans-serif; }
p { line-height:135%; }
h1 { font-size:1.8em; }
h2 { font-size:1.4em; }
a { text-decoration:none; color:#1c5292; font-weight:bold; }
a, a img { border:none; outline:none; }
input { width:100%; font-size:1em; }
input[type='text'] { border:1px solid #ccc; border-radius:5px; padding:4px 6px; }
table { border-collapse:collapse; margin-bottom:1em; }
th { vertical-align:bottom; font-size:.85em; white-space:nowrap; }
td { vertical-align:top; }
.bold { font-weight:bold; }
.content { display:table; position:relative; margin:1in auto; }
</style>
</head>
<body><div class="content">
<div style="position:absolute; left:0px; top:-60px;">
<a href="http://www.willmaster.com/">
<img src="http://www.willmaster.com/images/wmlogo_icon.gif" style="width:50px; height:50px; border:none;" alt="Willmaster logo">
</a>
</div>
<h1>SSL Certificate Check</h1>

<?php if( count($Information) ): ?>
<h2>
Certificate Information
</h2>
<p>
<span class="bold">Domain URL checked:</span><br><?php echo($postURL) ?>
</p>

<?php if( $Information['errno'] > 0 ): ?>
<p>
Error message:<br><?php echo($Information['errmsg']) ?>
</p>

<?php elseif($_POST['GET']): ?>

<p class="bold">
Certificate<?php if( $CertsCount > 1 ) { echo 's'; } ?> information:
</p>
<?php for( $i=0; $i<$CertsCount; $i++ ) { ?>
<table border="1" cellpadding="7" cellspacing="0">
<tr>
<th colspan="2" style="text-align:left; font-size:115%;">Certificate # <?php echo($i+1); ?></th>
</tr><tr>
<th>Label</th><th style="text-align:left;">Information</th>
</tr>
<?php
foreach( $Information['certinfo'][$i] as $k => $v )
{
    list($th['pre'],$th['cancelpre']) = $k == 'Cert' ? ['<pre>','</pre>'] : ['',''];
    echo <<<LINE
<tr>
<td>$k</td><td>{$th['pre']}$v{$th['cancelpre']}</td>
</tr>
LINE;
}
?>
</table>
<?php } ?>

<?php else: ?>
   <?php if($CertsCount>0): ?>
<?php
$expire = '';
$extime = array();
for( $i=0; $i<$CertsCount; $i++ )
{
   foreach( $Information['certinfo'][$i] as $k => $v )
   {
      if( preg_match('/Expire\s*date/i',$k) )
      {
         $extime[] = trim($v);
         break;
      }
   }
}
$cnt = count($extime);
for( $i=0; $i<$cnt; $i++ ) { $extime[$i] = strtotime($extime[$i]); }
$expire = date( 'j M Y \a\t G:i:s T', min($extime) );
?>
<p>
<span class="bold">Certificate expires:</span><br><?php echo($expire) ?>
</p>
<p>
<a href="<?php echo($_SERVER['PHP_SELF']) ?>?URL=<?php echo($postURL) ?>">Tap here to get more certificate information.</a>
</p>
   <?php else: ?>
<p>
No SSL certificate found.
</p>
   <?php endif; ?>

<?php endif; ?>

<?php endif; ?>

<div style="border-top:1px solid #333; height:2em;"></div>
<form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post">
<p>
Domain name:<br>
<input type="text" name="URL" value="<?php echo($_POST['URL']); ?>">
</p>
<p>
<input type="submit" value="Get Info" style="text-align:left; padding-left:2em;">
</p>
</form>
<p style="display:table; border-top:1px solid #ccc; margin-top:50px;">
Copyright 2019, 2024 <a href="https://www.willmaster.com/">Will Bontrager Software,&nbsp;LLC</a>
</p>
</div>
</body>
</html>

Save the source code to your server as SSLcert.php (or other *.php file name). To use it, types its URL into your browser's address bar.

I will be using it frequently. Actually, I'm apt to create a version to run from cron and send me an email whenever one of our domains is near SSL certificate expiration.

(This content first appeared in Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC