Created
January 9, 2025 14:52
-
-
Save faiyazalam/37c3d73419335e5f3b9b91a0d48a078f to your computer and use it in GitHub Desktop.
check ssl for a given domain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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