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

Asked

Viewed 1,225 times

4

I would like to know how I can block my site that is under maintenance and leave free access to two ips, I’m doing this way , but it only leaves free for one. 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');
  • plays these two ip’s in an array and uses the in_array function to see if REMOTE_ADDR matches any of its values.

  • You can compare this to an array (remember to use the function in_array() or or. First your variable must be an array ;).

  • @rray can so as answer the question , because I tried to do the way you said and nothing appears on screen .

  • In your example you are overwriting the variable value $ip

  • 1

    See my answer to this question at: http://answall.com/a/150386/31016

4 answers

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');

4


You can use the function in_array() to verify which ips have 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');
  • only one question only the two ips will see the site right . It is appearing the site . Thanks .

  • @allanaraujo was this?

  • this , is released the site only for the two ips .

3

An option to block access 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>

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 released variable more ips separated by , inside single quotes.

  • this code stays the same all blank .

  • I missed on i++ I switched to $i++

  • I tested on http://phptester.net/ and good roll d

Browser other questions tagged

You are not signed in. Login or sign up in order to post.