Validate IP Address

Discussion in 'PHP' started by jaypabs, Jan 5, 2011.

  1. #1
    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
     
    Last edited: Jan 5, 2011
    jaypabs, Jan 5, 2011 IP
  2. Cozmic

    Cozmic Member

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    48
    #2
    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.
     
    Cozmic, Jan 5, 2011 IP
  3. jaypabs

    jaypabs Active Member

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #3
    Thanks for the reply but I want it in PHP code.


     
    jaypabs, Jan 5, 2011 IP
  4. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #4
    
    <?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:
     
    tvoodoo, Jan 6, 2011 IP
  5. jaypabs

    jaypabs Active Member

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #5
    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
     
    jaypabs, Jan 6, 2011 IP