Thursday, December 6, 2018

this code is coped here for learning purpose!!!!!!!!!!!

this code is coped here for learning purpose!!!!!!!!!!!


<?php

/* Is That an IP Address?
 *
 * Given a string as input, create a program to evaluate
 * whether or not it is a valid IPv4 address.
 *
 * A valid IP address should be in the form of: a.b.c.d where a, b, c and d
 * are integer values ranging from 0 to 255 inclusive.
 *
 *   For example:
 *   127.0.0.1 - valid
 *   127.255.255.255 - valid
 *   257.0.0.1 - invalid
 *   255a.0.0.1 - invalid
 *   127.0.0.0.1 - invalid
 *
 *
 * Author: Mickel Sánchez.
 * Creation date: February 24, 2018.
 * Bugs: Unknown.
 */

function checkIP($ip) {
    $octets = explode(".", $ip);
    $check = true;
   
    if (count($octets) <> 4) {
        $check = false;
    }
   
    foreach ($octets as $octet) {
        if ((!is_numeric($octet)) || $octet < 0 || $octet > 255) {
            $check =  false;
        }
    }
   
    if ($check) {
        echo "$ip : It's a valid IP address. <br/>";
    } else {
        echo "$ip : It's not a valid IP address. <br/>";
    }
}

checkIP("127.0.0.1");
checkIP("127.255.255.255");
checkIP("257.0.0.1");
checkIP("255a.0.0.1");
checkIP("127.0.0.0.1");
checkIP("A01.0.0.1");
?>

No comments:

Post a Comment

form validation

function formsubmit ( ) { var empname = document .getElementById ( 'emp_name' ). value ; var email = document .getElem...