17.11. Checking if a Host Is Alive17.11.1. ProblemYou want to ping a host to see if it is still up and accessible from your location. 17.11.2. Solution
17.11.3. DiscussionThe ping program tries to send a message from your machine to another. If everything goes well, you get a series of statistics chronicling the transaction. An error means that ping can't reach the host for some reason. On error, Net_Ping::checkhost( ) returns false, and Net_Ping::ping( ) returns the constant PING_HOST_NOT_FOUND. If there's a problem running the ping program (because Net_Ping is really just a wrapper for the program), PING_FAILED is returned. If everything is okay, you receive an array similar to this:
Net_Ping doesn't do any parsing of the data to pull apart the information, such as the packet loss percentage or the average round-trip time. However, you can parse it yourself:
This regular expression searches for either a space or a slash. It then captures a sequence of one or more numbers and a decimal point. To avoid escaping /, we use the # nonstandard character as your delimiter. 17.11.4. See AlsoPEAR's Net_Ping package at http://pear.php.net/package-info.php?package=Net_Ping.
Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|