YourPaste - For your paste! Archive - Tools - Login

function to test if an ip is ipv6 valid or not

Posted by mscholten.eu on Sat 27 Feb 2010 13:53 208 views - Syntax: PHP - Expires: never - Report - IMG - Download -

  1. <?php
  2.         ini_set("show_errors","on");
  3.         error_reporting(E_ALL);
  4.         ini_set('display_errors','1');
  5. function is_ipv6($ip){
  6.         $explode2 = explode(":",$ip);
  7.         $error = 1;
  8.         for($i = 3; $i <= 8; $i++){
  9.                 if($i == count($explode2)){
  10.                         $error = 0;
  11.                 }
  12.         }
  13.         if(empty($explode2[0])){
  14.                 $num = 0;
  15.         }elseif(empty($explode2[count($explode2)-1])){
  16.                 $num = 0;
  17.         }else{
  18.                 $num = 1;
  19.         }
  20.         foreach($explode2 as $ip_part){
  21.                 if(strlen($ip_part) == 0){
  22.                         if($num === 0 || $num === 1){
  23.                                 $num++;
  24.                         }else{
  25.                                 $error = 1;
  26.                         }
  27.                 }else{
  28.                         $explode = explode(".",$ip_part);
  29.                         $hex = Array( 0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5,
  30.                                   6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 'a',
  31.                                   11 => 'b', 12 => 'c', 13 => 'd', 14 => 'e',  
  32.                                   15 => 'f' );
  33.                         foreach($explode as $hex2){
  34.                                 $hex_is_wrong = 1;
  35.                                 foreach($hex as $hex3){
  36.                                         if($hex2 == $hex3){
  37.                                                 $hex_is_wrong = 0;
  38.                                         }
  39.                                 }
  40.                                 if($hex_is_wrong === 1){
  41.                                         $error = 1;
  42.                                 }
  43.                         }
  44.                         if(strlen($ip_part) == 1 || strlen($ip_part) == 2 || strlen($ip_part) == 3 || strlen($ip_part) == 4){
  45.                         }else{
  46.                                 $error = 1;
  47.                         }
  48.                 }
  49.         }
  50.         if($error === 0){
  51.                 return TRUE;
  52.         }else{
  53.                 return FALSE;
  54.         }
  55. }
  56. if(is_ipv6($_GET['h']) == TRUE){
  57.         echo "ip is ipv6 valid";
  58. }else{
  59.         echo "ip is ipv6 invalid";
  60. }
  61. ?>

Comments


Name:
Comment:

© 2010 YourPaste.net - Disclaimer