Allow only certain Ip address to access website

Discussion in 'Programming' started by adam1987, May 16, 2008.

  1. #1
    Is this possible ?

    I only want to allow certain Ip address to access a website.
     
    adam1987, May 16, 2008 IP
  2. Arkserver

    Arkserver Banned

    Messages:
    2,533
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    yes, you need to specify the ip's in your .htaccess file.
     
    Arkserver, May 16, 2008 IP
  3. King Goilio

    King Goilio Member

    Messages:
    200
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    33
    #3
    ok i just wiped up and tested this .htaccess file:
    Order Deny,Allow
    Allow from PUT IP HERE
    Deny from All
    HTML:
    And to add more just copy the allow line and change the IP
     
    King Goilio, May 16, 2008 IP
  4. Y2K.bug

    Y2K.bug Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    try this, just change that IP to yours:

    if($_SERVER['REMOTE_ADDR']!="xx.xx.xx.xx")
    die("you don't have access to this web");
     
    Y2K.bug, May 17, 2008 IP
  5. adam1987

    adam1987 Well-Known Member

    Messages:
    714
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #5

    this does work.. but is it possible to put a range of ip's like ... 64.0.0.1 to 64.0.0.254 ??
     
    adam1987, May 17, 2008 IP
  6. King Goilio

    King Goilio Member

    Messages:
    200
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    33
    #6
    if($_SERVER['REMOTE_ADDR']!="64.0.0.[1-254]")
    die("you don't have access to this web");
    PHP:
    try that
     
    King Goilio, May 17, 2008 IP
  7. allaboutgeo

    allaboutgeo Peon

    Messages:
    85
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    For ranges, you need to get help from ip2long(). First you will convert your starting end ending ips to long using ip2long() and then convert IP in question to long and then compare it.
     
    allaboutgeo, May 18, 2008 IP
  8. gauravgrt

    gauravgrt Peon

    Messages:
    2,035
    Likes Received:
    129
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Yes it is 100% possible, nothing is impossible in programming world :D
     
    gauravgrt, May 18, 2008 IP
  9. adam1987

    adam1987 Well-Known Member

    Messages:
    714
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #9

    tried this but still i dont work....
     
    adam1987, May 20, 2008 IP
  10. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #10
    Might not be the best way to go about it, but it does work ...

    
    <?php
    /**
     * $allowed should contain all the ranges that you will allow
     * 
     * note: 
     * 		valid: 192.168.0.1-255
     * 		invalid: 192.1-255.0.1-255
     * 
     * 
     */
    $allowed = array(
    	'192.168.0.1-255',
    	'127.0.0.1-255'
    );	
    /** STOP EDITING STOP EDITING STOP EDITING STOP EDITING STOP EDITING STOP EDITING STOP EDITING STOP EDITING **/
    $continue = false ;
    
    function get_range_from_string( $string )
    {
    	if( ( $chunks = explode( ".", $string ) ) )
    	{
    		foreach( $chunks as $pos => $chunk )
    		{
    			if( strpos( $chunk, '-' ) !== false )
    			{
    				if( list( $start, $end ) = explode( '-', $chunk ) )
    				{
    					for( $i = $start; $i <= $end; $i++ )
    					{
    						for( $j = 0; $j <= 3; $j++ )
    						{
    							if( $pos == $j )
    							{
    								$parts[$j] = $i;
    							}
    							else $parts[$j] = $chunks[$j];
    						}
    						$ips[ ] = implode( ".", $parts );
    					}
    				}
    			}
    		}
    		return $ips ;
    	}
    	else return array( $string );
    }
    
    foreach( $allowed as $range )
    {
    	foreach( get_range_from_string( $range ) as $check )
    	{
    		if( $_SERVER['REMOTE_ADDR'] == $check )
    		{
    			$continue = true ;
    		}
    	}
    }
    
    if( !$continue )
    {
    	die( sprintf( "<font color=red>The address %s is not allowed to browse this website</font>", $_SERVER['REMOTE_ADDR'] ) );
    }
    
    /** IF EXECUTION GETS TO HERE THEN THE VISITOR IS ALLOWED **/
    ?>
    
    PHP:
     
    krakjoe, May 20, 2008 IP
  11. jasonleon

    jasonleon Active Member

    Messages:
    278
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #11
    You can stop Ip address by putting ban list but you cannot stop all other IP and allow some.
     
    jasonleon, Jun 6, 2008 IP
  12. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #12
    
    
    // Get IP
    function get_ip(){
    	$ipParts = explode(".", $_SERVER['REMOTE_ADDR']);
    	if ($ipParts[0] == "165" && $ipParts[1] == "21") {    
        	if (getenv("HTTP_CLIENT_IP")) {
            	$ip = getenv("HTTP_CLIENT_IP");
            } elseif (getenv("HTTP_X_FORWARDED_FOR")) {
                $ip = getenv("HTTP_X_FORWARDED_FOR");
            } elseif (getenv("REMOTE_ADDR")) {
                $ip = getenv("REMOTE_ADDR");
            }
        } else {
           return $_SERVER['REMOTE_ADDR'];
       	}
       	return $ip;
    }
    
    $ip = get_ip(); // Example 64.0.0.XXX
    $ipsubs = explode('.',$ip);
    for($x=0;$x<count($ipsubs)-2;$x++){ $final .= '.'.$ipsubs[$x];}
    
    if($final != '64.0.0'){ die('invalid address');} //64.0.0.
    if($ipsubs[count($ipsubs)-1] >= 0 && $ipsubs[count($ipsubs)-1] <= 100){ die('invalid range');}
    
    
    PHP:
    Peace,
     
    Barti1987, Jun 6, 2008 IP