473,382 Members | 1,665 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

how to check IP on the basis of IP Range?

Hi All,

I need a help from you guys. I have two tables once containing some list of ip range and other tables which contain the login id and their respective ip address from where they have logged in.

Now, I want to run a report on the daily basis to check whether the ips in the second table is come to ip range in the first table.

The table architecture is as follows:
iprange
id fromIP endIP
1 123.11.11.34 123.11.13.45
2 123.11.11.54 123.11.13.67
3 123.11.15.34 123.11.18.46 etc

now I have other table
logintable
ID username logip
1 A1 123.11.11.33
2 a3 123.15.11.34

the issue, i want to create php file where it checks the ip in logintable to iprange. If the record found, then show the details from logintable for the particular order.

Thank in advance for your help.

With Regards,
Robin
New Delhi - India
Jul 13 '10 #1
3 4584
TheServant
1,168 Expert 1GB
Have a look at ip2long().
And then I imagine something like: (I have not done this before)
Expand|Select|Wrap|Line Numbers
  1. function checkIP($user_ip, $lower_ip, $upper_ip) {
  2. $lower = ip2long($lower_ip);
  3. $upper = ip2long($upper_ip);
  4. $user = ip2long($user_ip);
  5. return ( ($user>=$lower) && ($user<=$upper) );
  6. }
Jul 13 '10 #2
@TheServant
Thanks for the nice response. Also, I was searching more about on the site I found a tutorial http://pgregg.com/blog/2009/04/php-a...fic-range.html which shows to check the ips. According to the code, i just modify few still I am not able to get the correct result.

The following are the code that I used

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     include_once "db_connect.inc.php";
  4.     include_once "ip_in_range.php";
  5.  // query to fetch the ip from my table logip2010_07_02 which have around 3000 records
  6.  $query = mysql_query("select * from logip2010_07_02 ") or die(mysql_error());
  7.   while($ip = mysql_fetch_array($query)){
  8.   $ip = $ip['loginIP'];
  9.  //echo $ip."<br>";
  10.   $iprange = mysql_query("Select * from nigeriaiprange limit 0,10") or die(mysql_error());
  11.   while($ipr = mysql_fetch_array($iprange)){
  12.   $range = '"'.$ipr['fromIP'].'-'.$ipr['toIP'].'"<br>';
  13.  //echo $range;
  14.   //$range = "10.134.21.1-10.134.24.1";
  15.   $ok = ip_in_range($ip, $range);
  16.   echo $ip, ($ok ? ' OK' : ' Fail'), "<br>";
  17.   }
  18.   }
  19. ?>
  20. the following is ip_in_range() function code that I got from the tutorial. 
  21.  
  22. <?php 
  23. function decbin32 ($dec) {
  24.   return str_pad(decbin($dec), 32, '0', STR_PAD_LEFT);
  25. }
  26. Function ip_in_range($ip, $range) {
  27.   if (strpos($range, '/') !== false) {
  28.     // $range is in IP/NETMASK format
  29.     list($range, $netmask) = explode('/', $range, 2);
  30.     if (strpos($netmask, '.') !== false) {
  31.       // $netmask is a 255.255.0.0 format
  32.       $netmask = str_replace('*', '0', $netmask);
  33.       $netmask_dec = ip2long($netmask);
  34.       return ( (ip2long($ip) & $netmask_dec) == (ip2long($range) & $netmask_dec) );
  35.     } else {
  36.       // $netmask is a CIDR size block
  37.       // fix the range argument
  38.       $x = explode('.', $range);
  39.       while(count($x)<4) $x[] = '0';
  40.       list($a,$b,$c,$d) = $x;
  41.       $range = sprintf("%u.%u.%u.%u", empty($a)?'0':$a, empty($b)?'0':$b,empty($c)?'0':$c,empty($d)?'0':$d);
  42.       $range_dec = ip2long($range);
  43.       $ip_dec = ip2long($ip);
  44.  
  45.       # Strategy 1 - Create the netmask with 'netmask' 1s and then fill it to 32 with 0s
  46.       #$netmask_dec = bindec(str_pad('', $netmask, '1') . str_pad('', 32-$netmask, '0'));
  47.  
  48.       # Strategy 2 - Use math to create it
  49.       $wildcard_dec = pow(2, (32-$netmask)) - 1;
  50.       $netmask_dec = ~ $wildcard_dec;
  51.  
  52.       return (($ip_dec & $netmask_dec) == ($range_dec & $netmask_dec));
  53.     }
  54.   } else {
  55.     // range might be 255.255.*.* or 1.2.3.0-1.2.3.255
  56.     if (strpos($range, '*') !==false) { // a.b.*.* format
  57.       // Just convert to A-B format by setting * to 0 for A and 255 for B
  58.       $lower = str_replace('*', '0', $range);
  59.       $upper = str_replace('*', '255', $range);
  60.       $range = "$lower-$upper";
  61.     }
  62.  
  63.     if (strpos($range, '-')!==false) { // A-B format
  64.       list($lower, $upper) = explode('-', $range, 2);
  65.       $lower_dec = (float)sprintf("%u",ip2long($lower));
  66.       $upper_dec = (float)sprintf("%u",ip2long($upper));
  67.       $ip_dec = (float)sprintf("%u",ip2long($ip));
  68.       return ( ($ip_dec>=$lower_dec) && ($ip_dec<=$upper_dec) );
  69.     }
  70.  
  71.     echo 'Range argument is not in 1.2.3.4/24 or 1.2.3.4/255.255.255.0 format';
  72.     return false;
  73.   }
  74.  
  75. }
  76. ?>
  77.  
  78.  
Even after the ip are present in the range it shows NO instead of OK.

Kindly check and help me in resolving the issue.

Regards,
Robin
Jul 13 '10 #3
TheServant
1,168 Expert 1GB
That code is a lot more involved than mine and is for special statements like: 195.25.*.* etc instead of the range as you originally requested. I am also confused as the result should be either FAIL or OK, instead of NO as you said it does. Can you confirm that?

I didn't write their code, and I imagine it's working. If not maybe you can contact the author, but it looks fine, and without more information, I can't tell what the problem is. How are you calling the function and your IP? It may be that your input format is wrong.
Jul 14 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Maik | last post by:
Hello, I've got a problem with access to special ranges. I renamed some cells (for example "C4" in "CUSTOM.GENERAL.VPRIM"). This is necessary, because I have to read out only these cells in...
1
by: Lala | last post by:
Hi, I need to check if a string contains unicode characters, and generate an alert if it does. (I do not want to parse the string to unicode). Anyone who knows how to do this? Thanks! :D
35
by: pinkfloydhomer | last post by:
How do I check if a string contains (can be converted to) an int? I want to do one thing if I am parsing and integer, and another if not. /David
3
by: Darrel | last post by:
I have this range validator: <asp:rangevalidator id=RngVal_LGwidth runat="server" ErrorMessage="LG Width needs to be between 10px and 2000px" EnableClientScript="False"...
5
by: sameer_deshpande | last post by:
Hi, I need to create a partition table but the column on which I need to create a partition may not have any logical ranges. So while creating or defining partition function I can not use any...
7
by: Joe Van Dyk | last post by:
Hi, Say I have: class Latitude { friend bool operator==(const Latitude& lhs, const Latitude& rhs); friend bool operator<(const Latitude& lhs, const Latitude& rhs); friend std::ostream&...
1
by: irfanali | last post by:
Hallo All, This is a Tool i m tryin to develop at work. I will explain how it works and then the Q I download a report from my ERP Tool on a daily basis and upload it into the Access Tool....
1
by: flumpuk | last post by:
Hi My job currently requires me to enter data from 300+ forms a month. The system which we used in Excel was slow , and theprevious guy had three workbooks for this job . I have created...
5
by: pereges | last post by:
I wrote a small program to check the range for int(signed) and long int(signed) on my machine: #include <stdio.h> #include <limits.h> int main(void) { printf("INT_MIN:%d INT_MAX: %d",...
3
by: Vinda | last post by:
Hi Bytes, Using a previous question as a base Access 2000 Inserting multiple rows based on a date range. I also wanted to insert multiple rows into a table according to a date range supplied by a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.