I am not a professional java script programmer. I hope you can help me.
I wrote a code to list port status using ajax. for example site:google.com ports: from 24 to 80 . But it is not working while I try to use a Javascript For Loop to list the output.
code is[index.php]:
<div id="inputform"><span>Enter URL or IP</span>
<input type="text" name="urladress" id="urladress" size=25 maxlength=100 value="<?php if (isset($_POST['urladress'])){echo $_POST['urladress'];}; ?>"></br>
from port
<input type="text" name="fromport" id="fromport" size=5 maxlength=6 value="<?php if (isset($_POST['fromport'])){echo $_POST['fromport'];}else{echo "1";} ; ?>">
to:
<input type="text" name="toport" id="toport" size=5 maxlength=6 value="<?php if (isset($_POST['toport'])){echo $_POST['toport'];}else{echo "24";} ;; ?>">
<input type="button" value="submit" onClick="getresults();" class="btn">
</div>
<div id="result" name="result"> </div>
<script language="javascript" type="text/javascript">
// Get the HTTP Object
function getHTTPObject(){
if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest) return new XMLHttpRequest();
else {
alert("Ajax not supported.");
return null;
}
}
// Change the value of the outputText field
function setOutput(){
document.getElementById('result').innerHTML += httpObject.responseText;
}
// Implement business logic
function getresults(){
document.getElementById('result').innerHTML = "calculating</br>"
httpObject = getHTTPObject();
if (httpObject != null)
{
var fromport = document.getElementById('fromport').value;
var toport = document.getElementById('toport').value;
if (fromport>toport || toport=='' || fromport==''){alert('wrong values');return 0;}
for(var i=fromport;i<=toport;i++){
httpObject.open("GET", "portscanner.php?inputText="+document.getElementById('urladress').value+"&port="+i, true);
httpObject.send(null);
httpObject.onreadystatechange = setOutput;
}
}
}
var httpObject = null;
</script>
</div>
server side[portscan.php]:
<?php
if (isset($_GET['inputText'])){
$url=$_GET['inputText'];
$url = str_replace("http://","",$url);
$url = str_replace("www.","",$url);
$port=$_GET['port'];
$fp = @fsockopen($url,$port,$errno,$errstr,2);
if(!$fp)
{
echo "port #". $port . "is closed</br>";
}
else
{
echo "Port #".$port."is open.</br>";
fclose($fp);
}
};
?>
it just shows last port’s result:
for example site:http://stackoverflow.com ports: from 70 to 80 shows:
calculating
Port #80is open.
Port #80is open.
it should look like:
calculating
Port #70is closed.
Port #71is closed.
Port #72is closed.
...
Port #79is closed.
Port #80is open.
httpObject will be overridden each time your loop executes. You probably need an array of httpObjects to handle this.
OnReadyStateChange is not a blocking command so the FOR loop will continue until the last time.
Change functions like this :