Convert an IP Address to an Integer

Posted On 2017-09-14 by VB6Boy
Tags: VBScript ASP Tip 
Views: 47

This is a pretty basic function which allows you to convert an IP address, which a string, into a long integer. But where would this be useful?

A Basic Example

Let's say you have a website, and you want to disallow certain IP address ranges from accessing the site. Let pretend that you want to stop the range of - You have to have a way to check if the user IP is in that range.

Using string based variables to perform this comparison is madness. You'll drive yourself bonkers trying to do this. Instead, you convert the above into integers, and store that in your database. The lower number will be the start of the range, and the higher number will be end of the range. Using the example range above, the converted values are 1702887424 for the lower end, and 1702888447 for the upper end.

Now, when a visitor comes to the site, grab the IP address, convert it to an integer, see if it falls within a blocked range. You can do this with a simple MySQL (or MS SQL) query:

strSQL="select Count(*) As NumResults from tblIPBlockList where dblStart<=1702887424 AND dblEnd>=1702888447"

If you get more than 0 results back from the query, then you'll know that the visitors IP address falls within that range.

The Code

The function is listed below, and is pretty straightforward in its use. If you have any questions, hit me up.

Function CLngIP(ByVal asNewIP)

  ' Split the IP address using the dot as a delimiter
  lnIpAry = Split(asNewIP, ".", 4)
  ' Loop through each number in the IP address

  For lnIndex = 0 To 3
    ' haven't reached the last number -
    If Not lnIndex = 3 Then

      ' Convert the number to a value range that can be parsed from the others
      lnIpAry(lnIndex) = lnIpAry(lnIndex) * (256 ^ (3 - lnIndex))

    End If

    ' Add the number to the results
    lnResults = lnResults + lnIpAry(lnIndex)


  CLngIP = lnResults

End Function

Special Instructions

If you try using this with an Access database, note that the long integer field type won't hold these values. Instead, you might use the currency field type to get around this.

About the Author

VB6Boy has posted a total of 21 articles.


Comments On This Post

No comments on this post yet!

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Your IP address is:

Before you can post, you need to prove you are human. If you log in, this test goes away.

Recent Forum Posts

Advanced search added
dwirch posted on September 23, 2017 at about 13:44 in Site News

Job Spammer: Gaurav Mehta - AgreeYa Solutions
dwirch posted on September 22, 2017 at about 10:35 in Spammers

Job Spammer: Prutha Siri - Javelin Systems
dwirch posted on September 10, 2017 at about 6:15 in Spammers

New security implemented
dwirch posted on September 7, 2017 at about 7:16 in Site News

Malicious IP Checker Companion Tool
dwirch posted on August 12, 2017 at about 20:24 in Site News

Job Spammer: Steve Adams
dwirch posted on August 8, 2017 at about 7:44 in Spammers