Home arrow PHP arrow 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.

  1. Adding Methods to the Query Processor in PHP
  2. Reviewing previous PHP networking functions: a quick look at the "QueryProcessor" class
  3. Adding more functionality to the "QueryProcessor" class: defining the "Ping()" and "IpConfig()" methods
  4. More methods ahead: defining the "Netstat()" and "getMXRecordsWin()" methods
  5. Gluing the pieces together: listing the full source code of the "QueryProcessor" class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
May 24, 2006

print this 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 />';
    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.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: