How To Check Website SSL Exists Or Not in PHP

Reading Time: 5 minutes
3,158 Views

Inside this article we will see the concept i.e How to check Website SSL exists or not in PHP. Article is very interesting to learn and very easy to understand. Here, we will use PHP functions to validate the existence of SSL of any website.

When SSL exists of any website then it will appear as Secure and locked icon into address bar whereas for not having SSL will be insecure with warning icon.

We will see the complete detailed concept to check and validate SSL for any website. We will start from it’s definition first.

Learn More –

Let’s get started.

What is an SSL?

An SSL certificate is a digital certificate that authenticates a website’s identity and enables an encrypted connection. SSL stands for Secure Sockets Layer, a security protocol that creates an encrypted link between a web server and a web browser.

Companies and organizations need to add SSL certificates to their websites to secure online transactions and keep customer information private and secure.

Why website needs an SSL certificate?

Websites need SSL certificates to keep user data secure, verify ownership of the website, prevent attackers from creating a fake version of the site, and convey trust to users.

URL with SSL: https://www.example.com

URL without an SSL: http://www.example.com

Example:

Create an Application

Create a folder with name site-ssl in your localhost directory.

Next, create a file index.php inside /site-ssl folder.

Concept of PHP Functions

Inside this SSL existence validation we will couple of PHP functions which returns data once it finds SSL to website.

Have a look the PHP functions,

$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']);

When we pass $url to this above group of code block then URL having an SSL will returns –

Otherwise for not having an SSL will return an empty data set.

According to this conditions we can write our code. Open up index.php and write this following code into it.

<?php

function is_ssl_exists($url)
{
    $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']);

    if (isset($certinfo) && !empty($certinfo)) {
        if (
            isset($certinfo['name']) && !empty($certinfo['name']) &&
            isset($certinfo['issuer']) && !empty($certinfo['issuer'])
        ) {
            return true;
        }
        return false;
    }
    return false;
}

$domain = 'https://www.youtube.com/';

if (is_ssl_exists($domain)) {

    echo "SSL is enabled!";
} else {
	
    echo "No SSL";
}

Application Testing

Open browser and hit this URL:

URL: http://localhost/site-ssl/index.php

Output

SSL is enabled!

We hope this article helped you to learn about How To Check Website SSL Exists Or Not in PHP Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.