【问题标题】:Ping selected IPs from checkbox on PHP-MySQL query从 PHP-MySQL 查询的复选框中 Ping 选定的 IP
【发布时间】:2016-05-17 06:33:56
【问题描述】:

我有一个想法,想知道它是否可以在 PHP 上,因为我是 PHP 新手。我需要 ping 选定的计算机名称或 IP 地址,并在 ping 状态框中将状态显示为在线或离线。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>Books</title>
</head>

<body>
<form action="Untitled-1.php" method="post">
<input type="text" name="search"/>

<input type="submit" value="search"/>
<input type="button"value="ping"/>

</form>

</form>
<br/>
<table border="1">
    <thead>
        <tr>
            <th>username</th>
            <th>desktop</th>
            <th>ip address</th>
            <th>select pc's</th>
            <th>ping status</th>
        </tr>
    </thead>
    <?php
    include("db.php");
    $word = isset ($_POST['search']) ? $_POST['search'] : "";
    $result=mysql_query("SELECT * FROM pc WHERE desktop like '%$word%'");
    while($test = mysql_fetch_array($result)) {
        $id = $test['user_id'];	
        ?>
        <tr align='center'>
            <td><font color='black'><?php echo $test['username'] ?></font></td>
            <td><font color='black'><?php echo $test['desktop'] ?> </font></td>
            <td><font color='black'><?php echo $test['ip_address'] ?></font></td>
            <td><input name="selector[]" type="checkbox" value="<?php echo $id; ?>"></td>
            <td><font color='black'><?php echo $test['ping_status'] ?></font></td>
        </tr>
        <?php
    }
    ?>
</table>
</body>
</html>

请帮忙

【问题讨论】:

    标签: php select checkbox mysqli ping


    【解决方案1】:

    在这个函数中传递 ip 它给出主机的状态。

     <?php
    
        function ping_host($host){
        exec("ping -c 4 " . $host, $output, $result);
         return $result==0?"online":"offline";
    
        }
    
        echo ping("www.google.com"); // function call
    
       ?>
    

    注意:这里 4 是你想要的 ping 数,你可以根据 linux 系统的要求更改,如果你不设置它会永远 ping。

    【讨论】:

    • 谢谢,但我已经知道这个功能,但我想知道它是否可以与查询一起使用,或者查询上每一行旁边的至少 ping 按钮
    • 您可以根据需要在查询或 ping 按钮中进行操作,但在查询情况下,如果表中有更多记录,则如果 Internet 连接速度较慢,则加载页面需要时间,如果是本地网络则没有问题。
    • 好的,你能帮我如何通过查询中每个按钮旁边的按钮来做到这一点,这样我就可以 ping 有按钮的行
    • 您可以使用在 ping 按钮单击时选中的复选框获取所选 PC 的 IP 地址。在 ping 按钮单击时,如果您不想刷新页面,可以提交表单或使用 ajax。
    【解决方案2】:

    非常感谢 Sunil 尝试帮助我,我找到了解决方案,现在它对我来说很好用

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    </head>
    
    
    <?php
    require("db.php");
    
    ?>
    
    
    <body>
    
    
    <form method="post">
                            <table cellpadding="0" cellspacing="0" border="1" class="table table-striped table-bordered" id="example">
                                <div class="alert alert-info">
                                    <strong><i class="icon-user icon-large"></i></strong>
                                </div>
                                <thead>
                                    <tr>
                                    
                                        <th>username</th>
                                        <th>desktop</th>
                                        <th>ip address</th>
                                        <th>select</th>
                                         <th>status</th>
    
                                      
                                    </tr>
                                </thead>
                                <tbody>
                                  	<?php 
    							$query=mysql_query("select * from pc")or die(mysql_error());
    							while($row=mysql_fetch_array($query)){
    							$id=$row['ip_address'];
    				          
    							
    							?>
                                    
    										<tr>
    
            <td><?php echo $row['username'] ?></td>
           <td><?php echo $row['desktop'] ?></td>
              <td><?php echo $row['ip_address'] ?></td>
                                  										<td>
    <input name="selector[]" type="checkbox" value="<?php echo $id; ?>">										</td>  
    
    <td><?php echo $row['ping_status'] ?></td>
                                    </tr>
                             <?php  }
    	
    
    					 
    						 						 
    if (isset($_POST['submit'])){
    	
    if(!empty($_POST['selector'])){
         foreach($_POST['selector'] as $id){
    		 
    		
    		
    		if (!$socket = @fsockopen($id, 80, $errno, $errstr, 30)) 
    
    
    
    { $status= "offline";
    echo $status; } 
    
    else 
    
    
    { $status= "online";
    echo $status;
    
     fclose($socket); } 
    	
    
    	
    $sql="UPDATE pc SET ping_status='$status' WHERE ip_address='$id'";
    
    $query=mysql_query($sql);
    
    	 header("Location:test.php");	
    	}
    	}
    }
     ?>
     
     
    						 
                                </tbody>
                            </table>
    						
    <button class="btn btn-success"  name="submit" type="submit">
    ping
    </button>
    </form>
    
    </body>
    </html>

    【讨论】:

      【解决方案3】:

      请找到最后的解决方案,

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta charset="utf-8">
      <title>Untitled Document</title>
      </head>
      
      
      <?php
      require("db.php");
      
      ?>
      
      
      <body>
      
      
      <form method="post">
                              <table cellpadding="0" cellspacing="0" border="1" class="table table-striped table-bordered" id="example">
                                  <div class="alert alert-info">
                                      <strong><i class="icon-user icon-large"></i></strong>
                                  </div>
                                  <thead>
                                      <tr>
                                      
                                          <th>username</th>
                                          <th>desktop</th>
                                          <th>ip address</th>
                                          <th>select</th>
                                           <th>status</th>
      
                                        
                                      </tr>
                                  </thead>
                                  <tbody>
                                    	<?php 
      							$query=mysql_query("select * from pc")or die(mysql_error());
      							while($row=mysql_fetch_array($query)){
      							$id=$row['ip_address'];
      				          
      							
      							?>
                                      
      										<tr>
      
              <td><?php echo $row['username'] ?></td>
             <td><?php echo $row['desktop'] ?></td>
                <td><?php echo $row['ip_address'] ?></td>
                                    										<td>
      <input name="selector[]" type="checkbox" value="<?php echo $id; ?>">										</td>  
      
      <td><?php echo $row['ping_status'] ?></td>
                                      </tr>
                               <?php  }
      	
      
      		$sql1="UPDATE pc
      SET ping_status = NULL
      WHERE ping_status is not null";
      $query1=mysql_query($sql1);
      
      		
      		
      
      						function pingAddress($id) {
          $pingresult = exec("ping -n 1 $id  && exit", $output, $result);
          //echo $result. "<br/>";
      global $status;
      
          if (($result == 0)){
              if(count(preg_grep('/Destination host unreachable/i', $output)) == 0){
                  $status="online <br/>";
      			echo $status;
      			
      		
      		}else
       $status="offline <br/>";
      			echo $status; 
      			
      			  }
          elseif ($result == 1){
      $status="offline <br/>";
      			echo $status;
      			  
      			
      			
      		 }    
      }
      
      
      				 
      						 						 
      if (isset($_POST['submit'])){
      	
      if(!empty($_POST['selector'])){
           foreach($_POST['selector'] as $id){
      
      	
      	
      
      		 
      		echo $id."";
      		
      
      
      	pingAddress($id);
      
      $sql="UPDATE pc SET ping_status='$status' WHERE ip_address='$id'";
      
      $query=mysql_query($sql);
      
      
       header("Location:test.php");
      
      
      }
      
      	}
      
      }
       ?>
       
       
      						 
                                  </tbody>
                              </table>
      						
      <button class="btn btn-success"  name="submit" type="submit">
      ping
      </button>
      </form>
      
      </body>
      </html>

      【讨论】:

        猜你喜欢
        • 2012-11-25
        • 2013-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-29
        • 2013-09-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多