apidoc
[ class tree: apidoc ] [ index: apidoc ] [ all elements ]

Source for file CheckIP2.php

Documentation is available at CheckIP2.php

  1. <?php
  2. /**
  3.  * Copyright (c) 2002-2006 Martin Jansen
  4.  *
  5.  * Permission is hereby granted, free of charge, to any person obtaining
  6.  * a copy of this software and associated documentation files (the
  7.  * "Software"), to deal in the Software without restriction, including
  8.  * without limitation the rights to use, copy, modify, merge, publish,
  9.  * distribute, sublicense, and/or sell copies of the Software, and to
  10.  * permit persons to whom the Software is furnished to do so, subject to
  11.  * the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included
  14.  * in all copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22.  * DEALINGS IN THE SOFTWARE.
  23.  *
  24.  */
  25.  
  26. /**
  27.  * Class to validate the syntax of IPv4 adresses
  28.  *
  29.  * Usage:
  30.  *   <?php
  31.  *   require_once 'Net/CheckIP2.php';
  32.  *     
  33.  *   if (Net_CheckIP2::check_ip("your_ip_goes_here")) {
  34.  *       // Syntax of the IP is ok
  35.  *   }
  36.  *   ?>
  37.  *
  38.  * @author   Martin Jansen <mj@php.net>
  39.  * @author   Guido Haeger <gh-lists@ecora.de>
  40.  * @author   Till Klampaeckel <till@php.net>
  41.  * @category Networking
  42.  * @package  Net_CheckIP2
  43.  * @version  0.0.1
  44.  * @license http://www.opensource.org/licenses/mit-license.html MIT License
  45.  */
  46. {
  47.     /**
  48.      * Force usage of check_ip
  49.      */
  50.     private function __construct()
  51.     {}
  52.  
  53.     /**
  54.      * Validate the syntax of the given IP adress
  55.      *
  56.      * This function splits the IP address in 4 pieces
  57.      * (separated by ".") and checks for each piece
  58.      * if it's an integer value between 0 and 255.
  59.      * If all 4 parameters pass this test, the function
  60.      * returns true.
  61.      *
  62.      * @access static
  63.      * @param  string $ip IP adress
  64.      * @return bool   true if syntax is valid, otherwise false
  65.      */
  66.     static function check_ip($ip '')
  67.     {
  68.         $oct explode('.'$ip);
  69.         if (count($oct!= 4{
  70.             return false;
  71.         }
  72.         for ($i = 0; $i < 4; $i++{
  73.             if (!preg_match("/^[0-9]+$/"$oct[$i])) {
  74.                 return false;
  75.             }
  76.  
  77.             if ($oct[$i< 0 || $oct[$i> 255{
  78.                 return false;
  79.             }
  80.         }
  81.         return true;
  82.     }
  83. }
  84. ?>

Documentation generated on Sat, 02 Feb 2008 16:30:04 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.