Ajax help

+1 William Frost · August 8, 2014
Hello :)

Just finished learning little bit Ajax and I having some problems, I will glad if someone will point out my problem.

I was trying to make a port scanner, Ajax send a request with one port everytime. Anyway, its not working :(

Ajax Code:
<script type="text/javascript">
var ports = [80, 22, 443, 21, 8080, 25, 3306];
function loadData(url, port){

var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp = new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML += "<br>" + xmlhttp.responseText;
    }
  }

  xmlhttp.open("GET","scan.php?url=" + url + "&port=" + port,true);
xmlhttp.send();

}

function scan () {
// body...
document.getElementById("myDiv").innerHTML = "Scan Started";
if(document.getElementById("url").innerHTML){
var url = document.getElementById("url").innerHTML;
for (var i = 0; i < ports.length; i++) {
loadData(url, ports);
};
}
}
</script>
<center>
<input type="text" id="url" name="url"><br><button onclick="scan()">Scan</button>
<br>
<div id="myDiv"></div>



PHP code:
if(isset($_GET['url'], $_GET['port'])){
$site = $_GET['url'];
$addr = gethostbyname($site);
$port = $_GET['port'];
$fp = fsockopen($addr, $port, $errno, $errstr, 5);
if (!$fp) {
    echo "[$addr] $site - Port: <span style='color: red'>$port</span> is <span style='color: red'>closed</span><br>\n";
} else {
    echo "[$addr] $site - Port: <span style='color: limegreen'>$port</span> is <span style='color: limegreen'>open</span><br>\n";
    fclose($fp);
}
 

}



Thanks,
Mr. Frost

Post a Reply

Replies

Oldest  Newest  Rating
+2 William Frost · September 4, 2014
Thanks!
+1 Samuel Oloruntoba · August 14, 2014
I can only see a few things wrong

loadData(url, ports);



should be replaced with 

loadData(url, ports);



i tried it and it worked.
0 Kanon Tape · September 1, 2014
:sideways: I don't understand..?
0 Samuel Oloruntoba · September 1, 2014
checkout this http://pastebin.com/3sgfACT2, you should see the correct reply
0 Sergio Mironescu · September 1, 2014
Its "ports" not "port". The "s" is missing...
  • 1

Javascript

107,042 followers
About

A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.

Links
Moderators