1

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.

Vercas
  • 8,931
  • 15
  • 66
  • 106
  • What is the context? Will your server be running a periodic shell script and caching the result? Will a PHP application be checking when the user requests a certain page? –  Jun 23 '11 at 20:19
  • When a link is opened, it redirects to a online URL to the file or an error page... – Vercas Jun 23 '11 at 20:24

6 Answers6

9

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");
Community
  • 1
  • 1
fdaines
  • 1,216
  • 10
  • 12
1

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);
Steve Nguyen
  • 5,854
  • 5
  • 21
  • 39
  • How do I get any results? That naked function doesn't return anything I can eccho. – Vercas Jun 23 '11 at 20:14
  • @Vercas you'll have to add an extra parameter, see http://php.net/manual/en/function.exec.php – Steve Nguyen Jun 23 '11 at 20:15
  • `exec` and `shell_exec` are disabled for security reasons on my server. – Vercas Jun 23 '11 at 20:21
  • @Vercas simple enough, then you can't ping via PHP. You'll need to write it in another language. Shell scripting or perl, or python or something. – Steve Nguyen Jun 23 '11 at 20:23
  • I can't. It's a freakin' free host. – Vercas Jun 23 '11 at 20:29
  • @Vercas Then you can't complete your task. It's the limitation of the server. It's not your fault. Either find another host or if you're not allowed to do that, then talk to the business people paying you to do this and tell them it can't be done. – Steve Nguyen Jun 23 '11 at 20:30
  • @FinalForm I am just 15 years old I don't have any business. I am coding a friend's website and he has a private server of a game. The game is huge, tho... – Vercas Jun 23 '11 at 20:32
  • @Vercas well, that still doesn't change the fact that you can't control your environment. Again, I want to stress this is not your fault. Your server isn't setup to do pings. So I wouldn't worry about it. – Steve Nguyen Jun 23 '11 at 20:33
  • @Vercas oh, sorry we talked so long i forgot the original question. If they have like a service running, like a web server, maybe you can do a request through php to see if their web service is up and then determine the ip / server is running. – Steve Nguyen Jun 23 '11 at 20:41
  • @Vercas But that method is not reliable if their box/server/ip doesn't have a web service running. – Steve Nguyen Jun 23 '11 at 20:42
  • @FinalForm Unfortunately, the file hosts don't have any kind of web servers AFAIK... Don't know how's that possible but I can't access any protocol on the IPs. – Vercas Jun 23 '11 at 20:48
  • @Vercas If my answer is right. Or I did good, please upvote me or check the green checkbox. I need more points. It makes me want to contribute to people that ask questions like you. – Steve Nguyen Jun 23 '11 at 20:53
  • @Vercas Not here to be your friend Vercas, just here to answer questions man. – Steve Nguyen Jun 23 '11 at 20:56
  • This is a bad solution – Norielle Cruz Nov 22 '18 at 08:07
0

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! :)

darkness
  • 121
  • 1
  • 5
0

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

ping IP_ADDRESS
Naftali
  • 144,921
  • 39
  • 244
  • 303
0

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.

Alexander Gessler
  • 45,603
  • 7
  • 82
  • 122
0

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

dave
  • 2,199
  • 1
  • 16
  • 34