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
3 4584
Have a look at ip2long().
And then I imagine something like: (I have not done this before) - function checkIP($user_ip, $lower_ip, $upper_ip) {
-
$lower = ip2long($lower_ip);
-
$upper = ip2long($upper_ip);
-
$user = ip2long($user_ip);
-
return ( ($user>=$lower) && ($user<=$upper) );
-
}
@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 -
<?php
-
-
include_once "db_connect.inc.php";
-
include_once "ip_in_range.php";
-
// query to fetch the ip from my table logip2010_07_02 which have around 3000 records
-
$query = mysql_query("select * from logip2010_07_02 ") or die(mysql_error());
-
while($ip = mysql_fetch_array($query)){
-
$ip = $ip['loginIP'];
-
//echo $ip."<br>";
-
$iprange = mysql_query("Select * from nigeriaiprange limit 0,10") or die(mysql_error());
-
while($ipr = mysql_fetch_array($iprange)){
-
$range = '"'.$ipr['fromIP'].'-'.$ipr['toIP'].'"<br>';
-
//echo $range;
-
//$range = "10.134.21.1-10.134.24.1";
-
$ok = ip_in_range($ip, $range);
-
echo $ip, ($ok ? ' OK' : ' Fail'), "<br>";
-
}
-
}
-
?>
-
the following is ip_in_range() function code that I got from the tutorial.
-
-
<?php
-
function decbin32 ($dec) {
-
return str_pad(decbin($dec), 32, '0', STR_PAD_LEFT);
-
}
-
Function ip_in_range($ip, $range) {
-
if (strpos($range, '/') !== false) {
-
// $range is in IP/NETMASK format
-
list($range, $netmask) = explode('/', $range, 2);
-
if (strpos($netmask, '.') !== false) {
-
// $netmask is a 255.255.0.0 format
-
$netmask = str_replace('*', '0', $netmask);
-
$netmask_dec = ip2long($netmask);
-
return ( (ip2long($ip) & $netmask_dec) == (ip2long($range) & $netmask_dec) );
-
} else {
-
// $netmask is a CIDR size block
-
// fix the range argument
-
$x = explode('.', $range);
-
while(count($x)<4) $x[] = '0';
-
list($a,$b,$c,$d) = $x;
-
$range = sprintf("%u.%u.%u.%u", empty($a)?'0':$a, empty($b)?'0':$b,empty($c)?'0':$c,empty($d)?'0':$d);
-
$range_dec = ip2long($range);
-
$ip_dec = ip2long($ip);
-
-
# Strategy 1 - Create the netmask with 'netmask' 1s and then fill it to 32 with 0s
-
#$netmask_dec = bindec(str_pad('', $netmask, '1') . str_pad('', 32-$netmask, '0'));
-
-
# Strategy 2 - Use math to create it
-
$wildcard_dec = pow(2, (32-$netmask)) - 1;
-
$netmask_dec = ~ $wildcard_dec;
-
-
return (($ip_dec & $netmask_dec) == ($range_dec & $netmask_dec));
-
}
-
} else {
-
// range might be 255.255.*.* or 1.2.3.0-1.2.3.255
-
if (strpos($range, '*') !==false) { // a.b.*.* format
-
// Just convert to A-B format by setting * to 0 for A and 255 for B
-
$lower = str_replace('*', '0', $range);
-
$upper = str_replace('*', '255', $range);
-
$range = "$lower-$upper";
-
}
-
-
if (strpos($range, '-')!==false) { // A-B format
-
list($lower, $upper) = explode('-', $range, 2);
-
$lower_dec = (float)sprintf("%u",ip2long($lower));
-
$upper_dec = (float)sprintf("%u",ip2long($upper));
-
$ip_dec = (float)sprintf("%u",ip2long($ip));
-
return ( ($ip_dec>=$lower_dec) && ($ip_dec<=$upper_dec) );
-
}
-
-
echo 'Range argument is not in 1.2.3.4/24 or 1.2.3.4/255.255.255.0 format';
-
return false;
-
}
-
-
}
-
?>
-
-
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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
|
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"...
|
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...
|
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&...
|
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....
|
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...
|
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",...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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
|
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...
|
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...
|
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...
| |