How to block website and leave free only for some ips? [duplicate]

4

I would like to know how I can block my site that is under maintenance and leave free access for two ips, I'm doing this, but it only leaves one free. I want you to be free for both of you.

$ip = '10.11.30.175';
$ip ='10.11.30.182';


if ( $_SERVER['REMOTE_ADDR'] != $ip )
   die('Site em manutenção, voltaremos em instantes');
    
asked by anonymous 10.10.2016 / 20:45

4 answers

4

You can use the in_array() function to check which ips has access to the site.

$validos = array('10.11.30.175', '10.11.30.182');
if (! in_array($_SERVER['REMOTE_ADDR'], $validos)) die('Site em manutenção, voltaremos em instantes');
    
10.10.2016 / 20:58
4

Following your reasoning:

$ip1 = '10.11.30.175';
$ip2 ='10.11.30.182';


if ( $_SERVER['REMOTE_ADDR'] != $ip1 || $_SERVER['REMOTE_ADDR'] != $ip2 )
   die('Site em manutenção, voltaremos em instantes');
    
10.10.2016 / 20:55
3

An option to block accesses would be via .htaccess :

APACHE 2.4

<Limit GET POST>
 Require all denied
 Require ip 10.11.30.175
 Require ip 10.11.30.182
</Limit>

APACHE 2.2

<Limit GET POST>
 order deny,allow
 deny from all
 allow from 10.11.30.175
 allow from 10.11.30.182
</Limit>
    
10.10.2016 / 21:05
2

Your script should be like this

first part (validation function)

function valida($ip){
   $retorna=false;
   $liberado=array('10.11.30.175','10.11.30.182');
   for($i=0;$i<count($liberado);$i++){
      if($ip==$liberado[$i]){ $retorna=true; }
   }
   return $retorna;
}

This top cod can be an external include or anywhere on your php page to use just do the following

if (!valida($_SERVER['REMOTE_ADDR'])) die('Site em manutenção, voltaremos em instantes');

If you want to add more ips just include in the array of the variable released plus ips separated by, within single quotation marks.

    
10.10.2016 / 20:53