Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
how do you validate and ip range using a mask?
Old 03-23-2005, 11:09 AM how do you validate and ip range using a mask?
simptech's Avatar
Skilled Talker

Posts: 81
Location: Cape Coral, Florida, United States
Trades: 0
Let's say i want to check against 69.189.55.128/25

Of course the low range would be 69.189.55.128 and I am pretty sure the high range is 69.189.55.255

Currently I am using the following code:

Code:
function ip_allowed() {
  $lo = ip2long("69.189.55.128");
  $hi = ip2long("69.189.55.255");
  $ip = ip2long($_SERVER['REMOTE_ADDR']);
  return ($ip >= $lo && $ip <= $hi);
}
 
/* Check for valid IP address */
if (!ip_allowed()) exit("Access denied!");
I would like to use something more dynamic like:

Code:
function ip_allowed($val) {
  list($ip_lo, $ip_mask) = split("/", $val);
  $ip_lo = ip2long($ip_lo);
  $ip_hi = ip2long(CALCULATED_VALUE_FROM_IP_MASK);
  $ip_user = ip2long($_SERVER['REMOTE_ADDR']);
  return ($ip_user >= $ip_lo && $ip_user <= $ip_hi);
}
 
/* Check for valid IP address */
if (!ip_allowed("69.189.55.128/25")) exit("Access denied!");
Does anybody know how to determine the maximum range by performing calculations on the ip mask?
__________________

Please login or register to view this content. Registration is FREE

FREE PHP scripts for your website!
simptech is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-23-2005, 06:47 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
If you have a mask of 25, you can get the equivalent long value by left shifting a hex value of all ones. For a mask of /25 you want to shift by 7:

PHP Code:
<?php


function calculate_mask($range) {
  
$shift 32 $range;
  
$mask 0xffffffff << $shift;
  
printf("Range: \\$range<br />Binary Mask:\t%b<br />",$mask);
  
printf("Dotted quad mask:\t%s<br /><br />",long2ip($mask));
}

for(
$i=10$i<28$i++) {
  
calculate_mask($i);
}

?>
gets you:

Code:
Range: \10
Binary Mask: 11111111110000000000000000000000
Dotted quad mask: 255.192.0.0

Range: \11
Binary Mask: 11111111111000000000000000000000
Dotted quad mask: 255.224.0.0

Range: \12
Binary Mask: 11111111111100000000000000000000
Dotted quad mask: 255.240.0.0

Range: \13
Binary Mask: 11111111111110000000000000000000
Dotted quad mask: 255.248.0.0

Range: \14
Binary Mask: 11111111111111000000000000000000
Dotted quad mask: 255.252.0.0

Range: \15
Binary Mask: 11111111111111100000000000000000
Dotted quad mask: 255.254.0.0

Range: \16
Binary Mask: 11111111111111110000000000000000
Dotted quad mask: 255.255.0.0

Range: \17
Binary Mask: 11111111111111111000000000000000
Dotted quad mask: 255.255.128.0

Range: \18
Binary Mask: 11111111111111111100000000000000
Dotted quad mask: 255.255.192.0

Range: \19
Binary Mask: 11111111111111111110000000000000
Dotted quad mask: 255.255.224.0

Range: \20
Binary Mask: 11111111111111111111000000000000
Dotted quad mask: 255.255.240.0

Range: \21
Binary Mask: 11111111111111111111100000000000
Dotted quad mask: 255.255.248.0

Range: \22
Binary Mask: 11111111111111111111110000000000
Dotted quad mask: 255.255.252.0

Range: \23
Binary Mask: 11111111111111111111111000000000
Dotted quad mask: 255.255.254.0

Range: \24
Binary Mask: 11111111111111111111111100000000
Dotted quad mask: 255.255.255.0

Range: \25
Binary Mask: 11111111111111111111111110000000
Dotted quad mask: 255.255.255.128

Range: \26
Binary Mask: 11111111111111111111111111000000
Dotted quad mask: 255.255.255.192

Range: \27
Binary Mask: 11111111111111111111111111100000
Dotted quad mask: 255.255.255.224
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 03-25-2005, 08:18 AM
simptech's Avatar
Skilled Talker

Posts: 81
Location: Cape Coral, Florida, United States
Trades: 0
maybe that's a bit more detail than i needed.

can you provide a simple method for WHAT_IS_MAX_IP from IP_AND_MASK?

CLARIFICATION:
Do not need the long value of the mask.
Trying to determine max ip number allowable by specified range.
__________________

Please login or register to view this content. Registration is FREE

FREE PHP scripts for your website!

Last edited by simptech; 03-25-2005 at 08:20 AM..
simptech is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to how do you validate and ip range using a mask?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.57086 seconds with 12 queries