Jesin's Blog

Welcome to the Portal of Technology

  • Facebook
  • GitHub
  • RSS
  • Twitter
  • Home
  • Categories
    • Domains
    • Linux
    • Networking
    • PHP
    • Virtualization
    • Web Design
    • Web Servers
    • Windows
  • WordPress Plugins
    • Custom Error Pages
    • HTTP Digest Authentication
    • Mailgun Email Validator
  • Toolbox
    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool
  • About Me
  • Contact Me
  • Sitemap
Home ›
PHP ›
PHP Open Port Check Script

PHP Open Port Check Script

July 16, 2011 PHP Jesin A 7 Comments

php category thumbnail

A quick tutorial on how to check for open ports using PHP. This function fsockopen() is used in the Open Port Check Tool in this blog. You can check whether a certain port is open on a specified IP address or hostname. Before reading take note that most FREE web hosting providers disable fsockopen() function as a security measure so first check whether the function can be used. Another thing to take note is that if fsockopen is unable to connect to a port it doesn’t necessarily mean that port is closed on the remote host, your server in which the script is running might also have block the OUTBOUND connection to that port.

Did you land up in this page searching for the Open Port Check Tool ?

Lets go straight to the syntax of fsockopen(). The complete syntax as shown in the official PHP documentation is

resource fsockopen ( string $hostname [, int $port = -1 [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]]] )

But for this PHP open port check tutorial we’ll use only the first two parameters

fsockopen(hostname,portno)

The fsockopen() function returns a resource identifier if the connection is successful or the Boolean value FALSE if connection couldn’t be established. We’ll use the if else loop to check this out.

<?php
if(fsockopen("websistent.com",80))
{
print "I can see port 80";
}
else
{
print "I cannot see port 80";
}
?>

Y0u can also replace hostnames with IP addresses in the fsockopen() function. An important thing is that when the script is unable to open a connection to the specified port it may also issue a PHP Warning message “[13-Jul-2011 10:53:09] PHP Warning:  fsockopen() [<a href=’function.fsockopen’>function.fsockopen</a>]: unable to connect to hostname:80 (Connection timed out) in /path/to/script.php(42) : eval()’d code on line 20”

To prevent PHP from displaying such errors at runtime read Disabling PHP Display Errors

Related posts:

php category thumbnailPHP Pie Chart Script web design category thumbnailUsername availability check with AJAX and PHP connect php with mysql thumbnailConnect PHP with MySQL php category thumbnailCreating a PHP MySQL Login Page Script linux category thumbnailFix serial port permission denied errors on Linux

Tags: php, php scripts

Comments

  1. Mohammed says

    October 25, 2014 at 12:53 am

    Hi

    Can You share the source for the Open Port check tool ?

    Reply
    • Jesin A says

      October 25, 2014 at 12:57 am

      Hi Mohammed,

      The source is in this article, did you try it?

      Reply
  2. E-Thee says

    November 7, 2014 at 10:56 am

    if I follow the https://websistent.com/disabling-php-display-errors/,
    the warning message will gone but it not show my custom error text
    (like example in line 8) and stop running to another script too

    how to make it show custom error text like you?

    Reply
    • Jesin A says

      November 8, 2014 at 1:01 am

      Put an ‘@’ before fsockopen –

      if ( @fsockopen( $variable, $port ) )
      Reply
  3. Jim S Smith says

    April 5, 2018 at 3:26 am

    Hi Jesin.

    Very informative articles here!

    I do a lot of my own PHP coding too. The “fsockopen()” function is also very useful for checking for some proxies, BTW. Sometimes a specific port is used by a proxy (or even, a “reverse proxy”) and can report as a valid resource (or not, depending on how the server is configured to handle such requests).

    Also,

    If the “fsockopen” function is disabled (or just not available for that specific system configuration/OS), the CURL library may be of help (again, if it is available and NOT disabled too).

    Otherwise,

    Once again, a very informative article. 🙂

    – Jim

    Reply
  4. KyrieAnn says

    April 16, 2018 at 1:28 am

    Hello.
    Is there a chance to check few ports on locahost like80, 443, 8080 in one request?
    if all respond then ok, if 0 then off else something …
    Greetings

    Reply
    • Jesin A says

      May 7, 2018 at 2:07 am

      Hi,

      You can try using this condition:

      if ( fsockopen( "localhost", 80 ) && fsockopen( "localhost", 443 ) && fsockopen( "localhost", 8080 ) )
      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get a wealth of information delivered to your inbox. Subscribe and never miss a single article.

  • Tutorials and howtos
  • Code, scripts and commands
  • Online Tools

* No spam, unsubscribe anytime

Hire Me

  • SSL installation and hardening (A+ on Qualys SSL test)
  • Apache & Nginx configuration
  • Email deliverability improvement (10/10 on Mail Tester & MailGenius)
  • WordPress customization, optimization and migration
  • and much more…

    Tools

    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool

    Nav

    • Home
    • About Me
    • Contact Me
    • Privacy Policy
    • Sitemap
    Vultr SSD VPS

    Creative Commons License
    Jesin's Blog by Jesin A is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
    Based on a work at websistent.com.