开发者

Determine whether an IP is online or not in PHP?

开发者 https://www.devze.com 2023-03-15 05:06 出处:网络
I have a few IPs (开发者_开发问答other servers hosting large binary files) and their static IP.

I have a few IPs (开发者_开发问答other servers hosting large binary files) and their static IP.

When a user clicks on a link, I want to ping each server until it finds one that is online and redirect the browser to the appropriate URI. If none of the servers are online, it should display an error.


look at this.... Ping site and return result in PHP

function availableUrl($host, $port=80, $timeout=10) { 
  $fp = fSockOpen($host, $port, $errno, $errstr, $timeout); 
  return $fp!=false;
}

//Return "true" if the url is available, false if not.
echo availableUrl("www.google.com");


You can make an explicit command to the command line with the PHP function exec()

exec("ping server");

Get the results from that and then parse it anyway you want.

$my_output = array();
exec("pwd", $my_output);

var_dump($my_output);


I am not sure how this is a php question but in command line just do:

ping IP_ADDRESS


You can use a library such as this one to do ping requests from within PHP.

shell_exec will do as well, but leaves parsing the output to you.


If you want to echo what you are executing just put it in a variable and then echo the variable. Keep in mind that if you exec("ping -c 5 google.com") you need to set it like this $output .= exec("ping -c 5 google.com");. Using .= for setting variable gives you the opportunity to set more than one setting in a variable. $output = null; $output = exec("pwd"); echo $output; Or you can use shell_exec("pwd"), it depends. Many people will be critic on my way of writing code. I know - it's not professinal. But IT WORKS! :)


I used fsockopen() to verify even if services listening on ports were up, not just the host.

0

精彩评论

暂无评论...
验证码 换一张
取 消