Jump to content

[SOLVED] MAC Address vaidation


realjumper

Recommended Posts

I need to validate MAC Adresses for the correct syntax. The support staff here constantly get student MAC Addresses wrong, and when I go to authenticate the address on the network, I find the address isn't valid....usually due to sloppy reading or writing from the support staff. So I thought about making a scipt for them to enter the address into a form and when they submit the form I could validate the syntax of the hex number to at least find out whether such an address is possible. I'm not sure how to do this, but I think MAC addresses are based on a calculation of numbers and letters like 0-9 and a-f, although I'm not sure. I'm also not sure how to validate the total address to find out whether it is a valid hex number. Any help would be great. Thanks :-)

Link to comment
https://forums.phpfreaks.com/topic/41147-solved-mac-address-vaidation/
Share on other sites

I see that this script is validating each block to see whther each block fit's the format, but it doesn't calculate the entire string to see whether the total string is valid. I think the entire string should be equal to a hexidecimal number, although I'm not sure.

 

So, my MAC address is '00-16-cb-03-2c-aa'. if I use this script, an address such as 'aa-aa-aa-aa-aa-aa' would also validate because each block meets the validation criterior. The whole string however, isn't validated.

 

So, what do I validate the string against? Is is a hexidecimal number?

 


<?php

// $address = 'aa-aa-aa-aa-aa-aa'; <-- not a valid address but passes the test

$address = '00-16-cb-03-2c-aa';   // valid address

function IsPhysicalAddress($address){
If(strlen($address) > 17) return 0;
If($address == "") return 0;
If (!eregi("^[0-9A-F]+(\-[0-9A-F]+)+(\-[0-9A-F]+)+(\-[0-9A-F]+)+(\-[0-9A-F]+)+(\-[0-9A-F]+)$",$address)) return 0;
$Array=explode("-",$address);
If(strlen($Array[0]) != 2) return 0;
If(strlen($Array[1]) != 2) return 0;
If(strlen($Array[2]) != 2) return 0;
If(strlen($Array[3]) != 2) return 0;
If(strlen($Array[4]) != 2) return 0;
If(strlen($Array[5]) != 2) return 0;
return 1;
}

## echo "<form name=\"mac\" ";

If(!IsPhysicalAddress($address)) {
        echo"Wrong Physical Address Format";
}
else{
echo "Valid";
}
?>

My understaning of MAC addresses was floored.....what I have in my code is the best that I can hope for as it isn't possible to validate the whole string. That's ok though, at least the supprt staff won't be able to enter an 's' when they really mean '5' etc etc

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.