i want to allow users to filter their login based on their ip address (a new settings in the user preferences i will implement).
so if a user with a specific ip login and there is not restriction, the login is successful.
in any other case i was thinking this
if the user choose his full IP like 67.31.85.47 and he has this IP then the login is good if a user choose 67.31.85.* and has this IP then the login is good if a user choose 67.31.. and has this IP then the login is good if a user choose 67...* and has this IP then the login is good any other case it's invalid
the user can choose up to 5 IP restrictions. eg:
67.31.*.*
167.77.47.*
62.11.28.28
25.57.*.*
169.*.*.*
i was tinking to strip the IP using explode and then compared to all restrictions he setup. this can be slow since i have to check 5 times * 4 (4 = IP exploded on the dot)
is there a faster way to do it? thanks
Here's a simple PHP snippet that you can place at the top of a file to determine whether the IPv4 address of a request is blacklisted: $blacklist = array( '127.0. 0.1', '192.168.
The FILTER_VALIDATE_IP filter validates an IP address. Possible flags: FILTER_FLAG_IPV4 - The value must be a valid IPv4 address. FILTER_FLAG_IPV6 - The value must be a valid IPv6 address.
The simplest way to collect the visitor IP address in PHP is the REMOTE_ADDR. Pass the 'REMOTE_ADDR' in PHP $_SERVER variable. It will return the IP address of the visitor who is currently viewing the webpage.
<?php
function testIP($ip) {
if($ip == '*' || $ip == '*.*.*.*') {
return TRUE;
}
if($_SERVER['REMOTE_ADDR'] == $ip) {
return TRUE;
}
$mask = str_replace('.*', '', $ip);
return strpos($_SERVER['REMOTE_ADDR'], $mask) === 0;
}
$_SERVER['REMOTE_ADDR'] = '70.69.68.67';
$ip = '1.11.1.*';
echo "Is $ip good: "; var_dump(testIP($ip));
$ip = '2.34.9.1';
echo "Is $ip good: "; var_dump(testIP($ip));
$ip = '70.11.*.*';
echo "Is $ip good: "; var_dump(testIP($ip));
$ip = '70.69.68.*';
echo "Is $ip good: "; var_dump(testIP($ip));
$ip = '70.69.*.*';
echo "Is $ip good: "; var_dump(testIP($ip));
$ip = '70.*.*.*';
echo "Is $ip good: "; var_dump(testIP($ip));
$ip = '*.*.*.*';
echo "Is $ip good: "; var_dump(testIP($ip));
$ip = '*';
echo "Is $ip good: "; var_dump(testIP($ip));
will output:
Is 1.11.1.* good: bool(false)
Is 2.34.9.1 good: bool(false)
Is 70.11.*.* good: bool(false)
Is 70.69.68.* good: bool(true)
Is 70.69.*.* good: bool(true)
Is 70.*.*.* good: bool(true)
Is *.*.*.* good: bool(true)
Is * good: bool(true)
If you are looking for specific ip (no wild card) checking, you can use:
function is_private_ip($ip) {
return filter_var($ip, FILTER_VALIDATE_IP) != FALSE;
}
var_dump(is_private_ip('82.237.3.3')); var_dump(is_private_ip('748.1234.5.4'));
Maybe this idea help for you:
<?php
$ip_restrict = "67.31.*.*
167.77.47.*
62.11.28.28
25.57.*.*
169.*.*.*";
$ip_restrict = array_flip(explode("\n", $ip_restrict));
$ip = preg_match("!([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)!", $_SERVER['REMOTE_ADDR'], $ip_match);
$ip_in_blacklist = false;
$ip_check_1 = $ip_match[1] . '.*.*.*';
$ip_check_2 = $ip_match[1] . '.' . $ip_match[2] . '.*.*';
$ip_check_3 = $ip_match[1] . '.' . $ip_match[2] . '.' . $ip_match[3] . '.*';
$ip_check_4 = $ip_match[1] . '.' . $ip_match[2] . '.' . $ip_match[3] . '.' . $ip_match[4];
if (isset($ip_restrict[$ip_check_1]) || isset($ip_restrict[$ip_check_2]) || isset($ip_restrict[$ip_check_3]) || isset($ip_restrict[$ip_check_4])) {
$ip_in_blacklist = true;
}
var_dump($ip_in_blacklist);
;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With