Day: February 1, 2018

How to check port is open and not open using PHP?

Sometime this is need to know that some port is active or not cause a port is used to recognize a specific service which is being executed in a computer. To recognize a port and to send packets, 16 bit numbers known as port numbers are used. A port number along with IP address serves as a destination for packets to be sent.

Suppose domain name https://techparkbd.com/ so get this ip address so ip is 184.107.37.81. There are many website to know ip. Then check port that open or not.

<?php
$host_connection = '184.107.37.81'; // techparkbd
$ports_number = array(21, 25, 80, 81, 110,143, 443, 3306); //checking port number

foreach ($ports_number as $port)
{
    $connection_check = @fsockopen($host_connection, $port);

    if (is_resource($connection_check))
    {
        echo '<h5>' . $host_connection . ':' . $port . ' ' . '(' . getservbyport($port, 'tcp') . ') is open now.</h5>' . "\n";

        fclose($connection_check);
    }

    else
    {
        echo '<h5 style="color: red">' . $host_connection . ':' . $port . ' is not open now.</h5>' . "\n";
    }
}

?>

when you run this code it will show result like this

184.107.37.81:21 (ftp) is open now.
184.107.37.81:25 (smtp) is open now.
184.107.37.81:80 (http) is open now.
184.107.37.81:81 is not open now.
184.107.37.81:110 (pop3) is open now.
184.107.37.81:143 (imap) is open now.
184.107.37.81:443 (https) is open now.
184.107.37.81:3306 () is open now.

This is very simple way to know port open or not. If you face any problem then please inform me.

Comment

get ip from domain name using php

If you need to know ip address from domain name then you can use php which is very simple. You can use gethostbyname. gethostbyname — Get the IPv4 address corresponding to a given Internet host name . This is simple code


<?php
$ip = gethostbyname('https://techparkbd.com/');

echo $ip;
?>

Then Output is :

184.107.37.81 

If you find any problem then please inform me. I will try to help you.

Comment