Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if an IP address is in a range

Status
Not open for further replies.

countdrak

Programmer
Jun 20, 2003
358
US
Is there a way to check if an IP address is in a subnet range? say I have the IP range 10.10.0.0 - 10.10.255.255 and a subnet of 255.255.0.0 and am given the IP 10.10.50.1

and want to verify that it is in that range.. thanks!

Is there a coldfusion way of doing this?
 
You can, I guess.

Build from this example:
<cfset csublow = 0>
<cfset csubhigh = 255>
<cfset dsublow = 0>
<cfset dsubhigh = 255>
<cfset ctest = 254>
<cfset dtest = 1>

<cfif (csublow LTE ctest AND ctest LTE csubhigh) AND (dsublow LTE dtest and dtest LTE dsubhigh)>
YES
<cfelse>
NO
</cfif>

You can add the code to parse submitted values, evaluate the subnet mask, etc.

HTH,

Phil Hegedusich
Senior Programmer/Analyst
IIMAK
-----------
Pity the insomniac dyslexic agnostic. He stays up all night, wondering if there really is a dog.
 
I dont think this logic will work in all cases.

What if my ip is 123.23.254.1? This ip is not in my subnet. But you never compare the A and the B.


 
My code is a simple example of what you can do. As I said, you'll have to write the code to handle all of the situations. I suggest passing the lower-range IP, the upper-range IP, the subnet mask, and the target IP to a function that evaluates based on the subnet mask.

Happy coding!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top