PHP
  Home arrow PHP arrow Page 2 - Build a Query Processor Class for Networking in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Build a Query Processor Class for Networking in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2006-05-17


    Table of Contents:
  • Build a Query Processor Class for Networking in PHP 5
  • Building the class: Converting host names to IP addresses and vice versa
  • Handling Internet services: defining the "getServiceNames()" and "getServicePorts()" methods
  • Assembling the methods: listing the full source code of the "QueryProcessor" class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Build a Query Processor Class for Networking in PHP 5 - Building the class: Converting host names to IP addresses and vice versa
    ( Page 2 of 4 )

    A good place to start coding the PHP 5 class that implements my networking query application is naturally with defining its constructor method, which takes up just one incoming parameter for doing its business: the name of the Internet host being queried.

    Since this is merely my first attempt at coding the class, for the moment this host name will be passed, for instance, in the form "hostname.com" or "hostname.net" or whatever you want to pass as host name to the class, without checking whether or not the host exists.

    Keeping in mind the initial condition that I mentioned above, the skeleton of the PHP 5 class, which not surprisingly I named "QueryProcessor," can be defined as follows:

    class QueryProcessor{
        private $host;
        public function __construct($host='myhost.com'){
            $this->host=$host;
        }
    }

    As shown by the above snippet of code, the "QueryProcessor" class has only one method (this is only temporary), that is its constructor, which accepts as a unique parameter the name of the Internet host being queried. In this case I also decided to assign a default value for the corresponding host name (myhost.com), but obviously you should change this value and provide another host name.

    Right, now that you saw how the primitive structure of the above class looks, it's time to provide the class with the ability to do something more useful than assigning the respective host name as a class property. For this reason, what I'll do next is add a couple of methods to the class, which come in handy for converting the specified host name to its corresponding IP address, and eventually for getting the complete list of IP addresses that match the given host name.

    Here are the signatures for these two new methods:

    // get IP address
    public function getIp(){
        if(!$ip=gethostbyname($this->host)){
            throw new Exception('Error converting hostname to IP
    address.');
        }
        return $ip;
    }

    // get list of IP addresses
    public function getIpList(){
        if(!$ips=implode(' - ',gethostbynamel($this->host))){
            throw new Exception('Error getting list of IP addresses
    for the provided hostname.');
        }
        return $ips;
    }

    As shown above, these two new methods, that is "getIp()" and "getIpList()" respectively, can be used to convert the specified host name to its IP address. In case you want to go one step further, they can also be used to obtain the entire set of IP addresses that correspond to that host.

    With regard to the first method, I used the "gethostbyname()" PHP built-in function, which gets the IP address corresponding to the specified Internet host name. If the IP address can't be obtained for some reason, this function will return the same string that was passed as host name.

    Now, if you turn your attention to the second method, you'll see that I used another useful PHP function, "gethostbynamel()," in order to obtain the entire list of IP addresses that match the given host. Since this function returns the respective IP addresses as an array, I made the "getIpList()" method return a string containing all the IP addresses, separated by a "-" character. Just in case the host name can't be resolved to a list of IP addresses, the method will throw an exception.

    At this point, I provided the class with the capacity to turn a given host name into a single IP address, or eventually a list of them. Thus, this conversion process wouldn't be complete if I don't include an opposite method, that is one that converts an IP address to an Internet host name.

    For the sake of completeness, below I listed the "getHost()" method, which is responsible for reversing the process and getting the IP address that corresponds to a specified Internet host name:

    // get host name
    public function getHost(){
        if(!$host=gethostbyaddr($this->getIp())){
            throw new Exception('Error resolving host name.');
        }
        return $host;
    }

    As illustrated above, the "getHost()" method uses the "gethostbyaddr()" PHP built-in function, in order to obtain the IP address corresponding to a given Internet host. Also, notice that I internally utilized the public "getIp()" method for getting the IP address in question and then passing it to the function.

    However, it seems that the above method is really redundant, because the respective host name has already been passed in as an argument to the constructor. Well, the reason for including this method rests mainly in the functionality of the class, since you may want to modify its code and redefine this method, in order to take up any well-formed IP address and convert it to its host name.

    Additionally, there's another interesting thing that you can experiment with when using this method: if you're passing to it the same IP address, over and over again, and you get different host names at times, these results may be a consequence of dealing with a host that shares multiple domains.

    At this point, I defined three methods that convert a specific host name to its corresponding IP addresses and vice versa. So now, take a look at the definition of the improved "QueryProcessor" class:

    class QueryProcessor{
        private $host;
        public function __construct($host='myhost.com'){
            $this->host=$host;
        }
        // get IP address
        public function getIp(){
            if(!$ip=gethostbyname($this->host)){
                throw new Exception('Error resolving host IP
    address.');
            }
            return $ip;
        }
        // get list of IP addresses
        public function getIpList(){
            if(!$ips=implode(' - ',gethostbynamel($this->host))){
                throw new Exception('Error getting list of IP
    addresses for the provided hostname.');
            }
            return $ips;
        }
        // get host name
        public function getHost(){
            if(!$host=gethostbyaddr($this->getIp())){
                throw new Exception('Error resolving host name.');
            }
            return $host;
        }
    }

    Okay, at this stage the "QueryProcessor" class is now capable of performing some useful tasks, which implies using a few networking PHP functions. Nevertheless, there's a long way ahead of us, so it's time to jump into the next section and continue adding more methods to the class.

    Want to learn how this will be done? Please click on the link below and keep reading.



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek