Advertisement
Guest User

Untitled

a guest
Dec 7th, 2022
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.74 KB | Source Code | 0 0
  1. <?php
  2. public function valid_ip($ip, $which = '')
  3.     {
  4.         $which = strtolower($which);
  5.  
  6.         // First check if filter_var is available
  7.         if (is_callable('filter_var'))
  8.         {
  9.             switch ($which) {
  10.                 case 'ipv4':
  11.                     $flag = FILTER_FLAG_IPV4;
  12.                     break;
  13.                 case 'ipv6':
  14.                     $flag = FILTER_FLAG_IPV6;
  15.                     break;
  16.                 default:
  17.                     $flag = array();
  18.                     break;
  19.             }
  20.  
  21.             return (bool) filter_var($ip, FILTER_VALIDATE_IP, $flag);
  22.         }
  23.  
  24.         if ($which !== 'ipv6' && $which !== 'ipv4')
  25.         {
  26.             if (strpos($ip, ':') !== FALSE)
  27.             {
  28.                 $which = 'ipv6';
  29.             }
  30.             elseif (strpos($ip, '.') !== FALSE)
  31.             {
  32.                 $which = 'ipv4';
  33.             }
  34.             else
  35.             {
  36.                 return FALSE;
  37.             }
  38.         }
  39.  
  40.         $func = '_valid_'.$which;
  41.         return $this->$func($ip);
  42.     }
  43. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement