Top

PHP Port Checker


PHP Code
+ 4 likes
Please Register to submit score.
Bookmark and Share
Average Score  8.0 (of 6 scores)
Date Added  Nov 30, 2008
Last Updated  Dec 05, 2008
Tags  checker  domain  file  frostwire  help  ip  jonesy  jonesy44  limewire  microtorrent  php  port  server  test  torrent  utorrent  website 

Introduction

This script can be added to a website to check if a port on a specified website/ip is open or not. my use for this was for checking with my uTorrent port.

In basic, if a socket is openned, it has connected. If it times out (2-3 seconds) it fails. You can edit the timeout in the fsockopen line, the last paramater is the timeout in seconds

Tested in Firefox. Report any bugs. Feel free to use wherever you need it.

Can now scan a range of ports!
Warning: A wide range of ports WILL take a long time to load the page.

Screenshot


Grab the Code

<?php
 
/* Port Checking PHP Script
   Created by Jonesy44
   Released: 30 November, 2008  */
 
echo '<title>Port Availability Checker';
//Please leave the next line :)
echo ', Writen by Jonesy44';
echo '</title>';
 
$addr = $_SERVER["REMOTE_ADDR"];
$port = "80";
if ($_GET["addr"]) {
  $addr = $_GET["addr"];
}
if ($_GET["port"]) {
  $port = $_GET["port"];
}
if ($_GET["port2"]) {
  $port2 = $_GET["port2"];
}
 
 
echo '<form action="' .$_SERVER["PHP_SELF"]. '" method="get">
  <div style="width:300px;background:#f1f1f1;padding:10px;font-family:arial;">
    <table width="100%" border="0" cellspacing="0" cellpadding="2">
      <tr>
        <td colspan="2" style="font-size:12px;">Please enter the Address/IP and port of the website or IP address you wish to test (enter the second IP if you want so scan to that port range)</td>
      </tr>
      <tr>
        <td width="30%" style="font-size:12px;">Address/IP</td>
        <td width="80%"><input type="text" name="addr" value="' .$addr. '"></td>
      </tr>
      <tr>
        <td width="30%" style="font-size:12px;">Port</td>
        <td width="80%"><input type="text" name="port" value="' .$port. '"></td>
      </tr>
      <tr>
        <td width="30%" style="font-size:12px;">-</td>
        <td width="80%"><input type="text" name="port2" value="' .$port2. '"></td>
      </tr>
        <td width="30%">&nbsp;</td>
        <td width="80%"><input type="submit" value="Check/Scan Port(s)"></td>
      </tr>
    </table>
  </div>
</form>
';
 
if ($_GET["addr"]) {
  if ($_GET["port"] && !$_GET["port2"]) {
    $fp = @fsockopen($addr, $port, $errno, $errstr, 2);
    $success = "#FF0000";
    $success_msg = "is closed and cannot be used at this time";
    if ($fp) {
      $success = "#99FF66";
      $success_msg = "is open and ready to be used";
    }
    @fclose($fp);
    echo '<div style="width:300px;background:' .$success. ';padding:10px;font-family:arial;font-size:12px;">
    The address <b>"' .$addr. ':' .$port. '"</b> ' .$success_msg. '
  </div>';
  }
  else if ($_GET["port"] && $_GET["port2"]) {
    $p1 = $_GET["port"];
    $p2 = $_GET["port2"];
    if ($p1 == $p2) {
      $fp = @fsockopen($addr, $port, $errno, $errstr, 2);
      $success = "#FF0000";
      $success_msg = "is closed and cannot be used at this time";
      if ($fp) {
        $success = "#99FF66";
        $success_msg = "is open and ready to be used";
      }
      @fclose($fp);
      echo '<div style="width:300px;background:' .$success. ';padding:10px;font-family:arial;font-size:12px;">
      The address <b>"' .$addr. ':' .$port. '"</b> ' .$success_msg. '
      </div>';
    }
    else {
      if ($p1 < $p2) {
        $s = $p1;
        $st = $p1;
        $e = $p2;
      }
      else if ($p2 < $p1) {
        $s = $p2;
        $st = $p2;
        $e = $p1;
      }
      while ($s <= $e) {
        $fp = @fsockopen($addr, $s, $errno, $errstr, 1);
        if ($fp) {
          $p_open = $p_open. " " .$s;
          $p_1 = 1;
        }
        @fclose($fp);
        $s++;
      }
      if ($p_1) {
        $c = "#99FF66";
        $m = "On the address <b>" .$addr. "</b> and port range <b>" .$st. "-" .$e. "</b> the following ports were open: " .$p_open;
      }
      else {
        $c = "#FF0000";
        $m = "No ports on the address <b>" .$addr. "</b> and port range <b>" .$st. "-" .$e. "</b> were open";
      }
      echo '<div style="width:300px;background:' .$c. ';padding:10px;font-family:arial;font-size:12px;">' .$m. '</div>';
    }
  }
}
?>

Comments

  (17)  RSS
jonesy44
Comments: 1,856
 
PHP Snippet:  PHP Port Checker
Posted on Nov 30, 2008 1:53 pm
Note: To test localhost, use 127.0.0.1 rather than "localhost" and of course port "80" The default is set to test your IP on port 80
Hawkee
Comments: 1,039
 
PHP Snippet:  PHP Port Checker
Posted on Nov 30, 2008 9:33 pm
Simple enough and the interface is clean, but the only complaint I have is with the HTML being mixed into the PHP. A simple template system or just closing your <?php ?> tag is easier to maintain in the long run.
F*U*R*B*Y*
Comments: 637
 
PHP Snippet:  PHP Port Checker
Posted on Nov 30, 2008 10:38 pm
What i would add next, is a feature to scan a range of ports, or persific ports... i.e. scan ports inbetween, and including port 80-100 or just scanning port 80 and 100..... the limitations are to what you see fit, but its what others may find handy...

Rating: 7/10
Like: +Like
jonesy44
Comments: 1,856
 
PHP Snippet:  PHP Port Checker
Posted on Dec 1, 2008 11:14 am
Thanks for the replies, i try and keep a clean interface, Hawkee. and yes, possibly a bad habit of integrating HTML with PHP, but i find it sometimes easier to maintain rather than using the two seperately.

I will see what i can do FURBY, might take a while to scan the ports, so perhaps some nice JS & Ajax will come in handy to make a nicer interface.

Thanks for the ratings & +Likes :]
jonesy44
Comments: 1,856
 
PHP Snippet:  PHP Port Checker
Posted on Dec 2, 2008 12:03 pm
Who rated this down :-[
F*U*R*B*Y*
Comments: 637
 
PHP Snippet:  PHP Port Checker
Posted on Dec 2, 2008 6:40 pm
It already had a rating of 5 when i rated it a 7, so no one ;)
jonesy44
Comments: 1,856
 
PHP Snippet:  PHP Port Checker
Posted on Dec 3, 2008 1:13 pm
:-(
Xpl0reR
Comments: 223
 
PHP Snippet:  PHP Port Checker
Posted on Dec 3, 2008 9:56 pm
/me rates 10 / 10 and +likes :)

can you make it to be centered in the web page ?
that would be really neat.
jonesy44
Comments: 1,856
 
PHP Snippet:  PHP Port Checker
Posted on Dec 4, 2008 11:14 am
of course, jus put "<center>" tags around it ;)

I'm working on a port range scanner next. i'll keep yah updated :]
jonesy44
Comments: 1,856
 
PHP Snippet:  PHP Port Checker
Posted on Dec 5, 2008 11:44 am
Can now scan a range of ports!
Warning: A wide range of ports WILL take a long time to load the page.
F*U*R*B*Y*
Comments: 637
 
PHP Snippet:  PHP Port Checker
Posted on Dec 5, 2008 7:35 pm
Code:

  else if ($_GET["port"] && $_GET["port2"]) {
    $p1 = $_GET["port"];
    $p2 = $_GET["port2"];
    if ($p1 == $p2) {
      $fp = @fsockopen($addr, $port, $errno, $errstr, 2);


sorry, but where is $port set?
should check to see if $_GET['port'] and $_GET['port2'] is numeric

Code:

if (is_numeric($_GET['port']) && is_numeric($_GET['port2'])) {


besides that, not bad....
jonesy44
Comments: 1,856
 
PHP Snippet:  PHP Port Checker
Posted on Dec 6, 2008 2:23 am
$_GET["port"] is set when you hit submit on the form, along with port2. it's a good idea, i'll checkit out furbs :P
jonesy44
Comments: 1,856
 
PHP Snippet:  PHP Port Checker
Posted on Dec 6, 2008 2:25 am
Sorry, misread youor question, $port is set at the top of the script.
Joshuaxiong1
Comments: 127
 
PHP Snippet:  PHP Port Checker
Posted on Dec 28, 2008 1:22 am
cool this script really work lolololol!!!!!!!!!!!!!!!!!!!!!!!!!!
jonesy44
Comments: 1,856
 
PHP Snippet:  PHP Port Checker
Posted on Feb 6, 2009 10:43 am
Thanks A^1^T^E^A^M, and ProIcons for the +Likes :)
jonesy44
Comments: 1,856
 
PHP Snippet:  PHP Port Checker
Posted on Feb 6, 2009 6:20 pm
And zmodem too i guess :D
jonesy44
Comments: 1,856
 
PHP Snippet:  PHP Port Checker
Posted on May 31, 2009 7:05 am
:o http://www.korbynn.com/checkport.php

who owns that site?

Commenting Options

Register or Login to Hawkee.com or use your Facebook or Twitter account by clicking the corresponding button below.

  
Bottom