php - 使用 Ajax 列出站点/ip 端口状态

标签 php ajax

我不是专业的 java 脚本程序员。我希望你能帮助我。

我编写了一个代码来使用 ajax 列出端口状态。例如 site:google.com 端口:从 24 到 80。但是当我尝试使用 Javascript For Loop 列出输出时它不起作用。 代码是[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>

服务器端[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);
    }
};
?>

它只显示最后一个端口的结果: 例如站点:http://stackoverflow.com 端口:从 70 到 80 显示:

calculating
Port #80is open.
Port #80is open.

它应该是这样的:

calculating
Port #70is closed.
Port #71is closed.
Port #72is closed.
...
Port #79is closed.
Port #80is open.

最佳答案

httpObject 将在每次循环执行时被覆盖。您可能需要一组 httpObjects 来处理这个问题。

OnReadyStateChange 不是阻塞命令,因此 FOR 循环将一直持续到最后一次。

像这样改变函数:

// Change the value of the outputText field
function setOutput(index){
    if(this.readyState == 4) // Done
    {
        if(this.status == 200)
            document.getElementById('result').innerHTML += this.responseText;
        else
            document.getElementById('result').innerHTML += "Error occured : " + this.status + "<br>";
    }
}
// Implement business logic
function getresults(){
    document.getElementById('result').innerHTML = "calculating</br>";
    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++){
        var index = httpObject.push(getHTTPObject()) - 1;
        httpObject[index].open("GET", "test.php?inputText="+document.getElementById('urladress').value+"&port="+i, true);
        httpObject[index].send(null);
        httpObject[index].onreadystatechange = setOutput;
    }
}
var httpObject = new Array();

关于php - 使用 Ajax 列出站点/ip 端口状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7814704/

相关文章:

php - Stof 学说扩展 :Tree lvl doesn't update

php - 在新标签页中打开页面是否有任何PHP函数

java - Wicket:延迟加载 DropDownChoice

javascript - php - json_decode 未定义索引,但值在那里

python - 如何在 Flask 中使用 AJAX 遍历列表

javascript - 如何使用 Ajax 和 JSON 制作下拉菜单?

php - Mysql如何设置时间数据类型为only HH :MM in database

php - 找不到推进配置文件

jquery - 将序列化 JSON 对象 POST 到 Coldfusion 远程方法,而不是使用 FORM

php - 在mysql中注册的if语句