HomePHP Page 3 - Adding Methods to the Query Processor in PHP
Adding more functionality to the "QueryProcessor" class: defining the "Ping()" and "IpConfig()" methods - PHP
Over this second article in a series covering network programming in PHP, you will learn how to run popular Windows network monitoring utilities, such as the “ping,” “netstat” and “ipconfig” programs, by adding some new methods to the “QueryProcessor” class introduced in the previous article.
The first method that I will add to the class is "IpConfig()," which, as the name clearly suggests, is responsible for executing the "ipconfig" Windows-based utility. In case you didn't know, this program will let you know the network settings of your Windows system, including the IP address, subnet mask, DNS suffixes, default gateway, and so on for each of the network adapters installed on your computer.
Having described the functionality of the "ipconfig" Windows utility, take a look at the method below, which runs this command on a Windows machine:
// execute 'ipconfig' command on Windows systems public function IpConfig(){ $output='Running ipconfig command...Please wait.<br />'; exec('ipconfig',$lines); foreach($lines as $line){ $output.=$line.'<br />'; } return $output; }
In this case, the above "Ipconfig()" method uses the "exec()" PHP built-in function, in order to perform the homonymous Windows utility. As you know, this function allows the execution of commands on the server (provided that you have the right permissions) and returns the corresponding results as an array structure.
After executing the "ipconfig" command, the output is returned to calling code, simply by iterating over the output array returned by the "exec()" function. Evidently, this method is really simple to read and code, thus I'll move on to the next one, "Ping()," which runs the popular "ping" command on Windows-based systems. Please take a look at the way this method is defined:
// execute 'ping' command on Windows systems public function Ping(){ $output='Running ping command...Please wait.<br />'; exec('ping '.$this->host,$lines); foreach($lines as $line){ $output.=$line.'<br />'; } return $output; }
As illustrated in the above code block, the "Ping()" method looks nearly identical to the previous one. Of course, the only difference rests on the type of Window command executed, which in this case is the famous "ping" utility. Similar to the "IpConfig()" method that you learned before, this method first uses the "exec()" PHP function, in order to run the "ping" program, and next returns the result as an array.
Due to the numerous similarities exposed by the two methods, it's possible to merge them into one and avoid writing additional code. However, I want you to learn each method as a separate block of code, since it's much simpler to read and understand.
Now, after defining the prior methods, I'll code two new methods in the next section, which also use the "exec()" PHP native function for doing its business on Windows systems. To learn more on this please keep on reading.