Skip to content

Instantly share code, notes, and snippets.

@faiyazalam
Created January 9, 2025 14:52
Show Gist options
  • Save faiyazalam/37c3d73419335e5f3b9b91a0d48a078f to your computer and use it in GitHub Desktop.
Save faiyazalam/37c3d73419335e5f3b9b91a0d48a078f to your computer and use it in GitHub Desktop.
check ssl for a given domain
<?php
function checkSsl($domain, $daysThreshold = 7)
{
$port = 443; // Default port for HTTPS
$timeout = 5; // Timeout for the connection
// Open a connection to the domain
$context = stream_context_create([
"ssl" => [
"capture_peer_cert" => true
]
]);
$client = @stream_socket_client(
"ssl://$domain:$port",
$errno,
$errstr,
$timeout,
STREAM_CLIENT_CONNECT,
$context
);
if (!$client) {
return "Error connecting to $domain: $errstr";
}
// Get the certificate
$params = stream_context_get_params($client);
$cert = openssl_x509_parse($params['options']['ssl']['peer_certificate']);
if (!$cert) {
return "Unable to parse SSL certificate for $domain.";
}
$validToTimestamp = $cert['validTo_time_t'];
$daysToExpiration = ($validToTimestamp - time()) / (60 * 60 * 24);
if ($daysToExpiration <= $daysThreshold) {
return "the domain $domain is expiring soon";
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment