Hi, I want to validate IP address that is whitelisted. Currently here's my code: $whitelist_ips = '192.168.1.1,172.18.*.*'; // Whitelist IP addresses, seperate multiple with a comma $current_ip = $_SERVER['REMOTE_ADDR']; $whitelist_ips = explode(',',$whitelist_ips); foreach($whitelist_ips as $ip) { if (trim($ip) == $current_ip) { $whitelist = true; break; } } Code (markup): In the above code how can I exclude IP address that is part of 172.18.*.*? I want to break code execution if the IP address is for example equal 172.18.48.2. Thanks in advance
Instead of using PHP put your files you want to protect in its own folder and put this in a file called .htaccess: Allow from 127.0.0.1 Deny from all Code (markup): where 127.0.0.1 is your IP address. I'm pretty sure wildcards work.
<?php $whitelist_ips = '192.168.1.1,172.18.*.*'; // Whitelist IP addresses, seperate multiple with a comma $current_ip = $_SERVER['REMOTE_ADDR']; $whitelist_ips = explode(',',$whitelist_ips); foreach($whitelist_ips as $ip) { $cur = explode(".",$current_ip); $check = explode(".",$ip); if((($check[0] == $cur[0]) || ($check[0] == '*')) && (($check[1] == $cur[1]) || ($check[1] == '*')) && (($check[2] == $cur[2]) || ($check[2] == '*')) && (($check[3] == $cur[3]) || ($check[3] == '*'))) { $whitelist = true; break; } } ?> PHP:
Hi, While I wait for the reply I came up with this code: <?php $whitelist_ips = '192.168.1.1,172.18'; // Whitelist IP addresses, seperate multiple with a comma $current_ip = $_SERVER['REMOTE_ADDR']; $whitelist = false; $whitelist_ips = explode(',',$whitelist_ips); foreach($whitelist_ips as $ip) { echo "ip: " . $ip; $iplength = strlen($ip); if (trim($ip) == substr($current_ip,0,$iplength)) { //trim($ip) == $current_ip || $whitelist = true; echo "true"; break; } } ?> Code (markup): Let me know if this is ok compare to your code. Thank you