简体   繁体   中英

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? 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? or php config? or linux permission?

I'm guessing it's cos the default behavoir of ping is to never stop. 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."

Try adding options to limit pings run-time and see if you get a result.

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>";
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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