简体   繁体   English

Centos 6.2-Apache 2-PHP 5-执行空白结果

[英]Centos 6.2 - Apache 2 - PHP 5 - Blank result executing

I have one issue executing 'ping' on PHP, I received a blank result but if I execute other command like a 'whoami' I received a right result, Could you help me please? 我在PHP上执行“ ping”时遇到一个问题,但收到的结果是空白,但是如果执行其他命令(如“ whoami”),我会收到正确的结果,请问您能帮我吗? any Idea? 任何想法?

<?php
exec('ping google.com', $output);
echo $output;
//Result: 
?>

<?php
exec('whoami', $output);
echo $output;
//Result: apache
?>

Thanks 谢谢

Note: Maybe can be some from apache config? 注意:也许可以从apache config中获取一些? or php config? 或PHP配置? or linux permission? 或Linux权限?

I'm guessing it's cos the default behavoir of ping is to never stop. 我猜这是因为ping的默认行为是永不停止。 It keeps going until you kill it. 它一直持续到您杀死它为止。

man ping says " -c count Stop after sending count ECHO_REQUEST packets. With deadline option, ping waits for count ECHO_REPLY packets, until the timeout expires." man ping说:-c count发送计数ECHO_REQUEST数据包后停止。使用截止日期选项,ping等待计数ECHO_REPLY数据包,直到超时到期。

Try adding options to limit pings run-time and see if you get a result. 尝试添加选项以限制ping运行时间,看看是否有结果。

Try this code. 试试这个代码。

<?php
function GetPing($ip=NULL) {
 if(empty($ip)) {$ip = $_SERVER['REMOTE_ADDR'];}
 if(getenv("OS")=="Windows_NT") {
  $exec = exec("ping -n 3 -l 64 ".$ip);
  return end(explode(" ", $exec ));
 }
 else {
  $exec = exec("ping -c 3 -s 64 -t 64 ".$ip);
  $array = explode("/", end(explode("=", $exec )) );
  return ceil($array[1]) . 'ms';
 }
}

echo GetPing();
?>

You can also try this one 您也可以尝试这个

<?php
$output = shell_exec('ping google.com');
echo "<pre>$output</pre>";
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM