我有一个想法,想知道它是否可以在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>
请帮助
答案 0 :(得分:0)
在此功能中传递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个你希望你可以根据Linux系统的要求改变的ping数量,如果不设置它将永远ping。
答案 1 :(得分:0)
非常感谢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>
答案 2 :(得分:0)
请找到最后的解决方案,
<!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>
&#13;