1

I was looking for a PHP script which checks if the script can connect to the http port.

I tried codes like this, but the response of https and http is the same:

<?php
$get = stream_context_create(array("ssl" => array("capture_peer_cert" => TRUE)));
$read = stream_socket_client("ssl://www.raffelpages.com:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $get);
$cert = stream_context_get_params($read);
var_dump($cert["options"]["ssl"]["peer_certificate"]);
Joanmi
  • 442
  • 3
  • 19
  • What have you found? What have you tried? What is failing? – MonkeyZeus Jun 01 '18 at 13:35
  • I updated my post with the script tried – Joanmi Jun 01 '18 at 13:37
  • This solution uses fopen() rather than stream_socket_client(): https://stackoverflow.com/questions/36950874/check-if-a-website-is-using-ssl-using-curl – Mr Glass Jun 01 '18 at 13:43
  • I tried it too, but it just show me "1" on all links – Joanmi Jun 01 '18 at 13:46
  • Can you provide an example of a site which has an invalid one? – MonkeyZeus Jun 01 '18 at 15:43
  • @MonkeyZeus sorry for response later, here the example http://www.raffelpages.com – Joanmi Jun 05 '18 at 06:23
  • 1
    It's worth noting that there could be different definitions of "a valid certificate" here: 0) does the connection respond to an SSL/TLS handshake with an X.509 certificate of some sort? 1) is it within its validity period according to your system clock? 2) is it issued for a particular domain name? 3) is it signed by an authority listed in some store of root certificates on your system? 4) has it been revoked? Note that each of these relies on some additional data for comparison beyond just opening the connection. – IMSoP Jun 12 '18 at 14:02

1 Answers1

-1

You can try something like this it will try to connect to the web site: It will try to connect to the domain with the port "443" and if it isn't possible its because the certificate is not valid

$domain = "www.google.es";
$port = "443";

    $fp = fsockopen($domain, $port, $errno, $errstr, $timeOut);
        if (!$fp) {
            //If not valid:
            echo "Certificate not valid";
        } else {
            echo "Certificate is valid";
Anthy
  • 60
  • 1
  • 8
  • This will also return "not valid" if the server is down, if there's a network connectivity problem somewhere along the way, etc. Not a great approach. – ceejayoz Jun 12 '18 at 13:53
  • 1
    It will also return "Certificate is valid" for a server with port 443 listening for something other than HTTPS. It may actually meet some use cases, but the descriptions should be changed to something like "server is/is not currently listening on the standard HTTPS port". What it definitely will not do is validate anything about any certificate. – IMSoP Jun 12 '18 at 14:04