Solutions for Go, MySQL, PHP, Linux and more

Restricetd access to a web-application with htaccess or php

Posted — Jul 3, 2009

If you building a simple web-application and you are not really sure, ow secure are the authentication and or the rest of the application you should think about to limit the access to the application.

Furthermore, may your application should be available only for testing purpose of several customers the following could help you:

First way using a .htaccess which controls the access using a apache web server.

This is a very fast solution BUT you CAN’T use hostnames and or full qualified dns names like or

order deny,allow
allow from
allow from
allow from
deny from all

The second way which I prefer, using a php code snippet. This allows you to use hostnames and full qualified domain names. Definitely you can write it in less than the lines I used below for the code, but in of understanding what the code does, it’s better 😉

 * ACL array
$valid_hosts = array();
$valid_hosts[] = 'localhost';
$valid_hosts[] = '';
$valid_hosts[] = '';
$valid_hosts[] = '';
$valid_hosts[] = '';

  $valid_ip = gethostbyname($valid_hosts[$i]);
  if($valid_ip == $_SERVER['REMOTE_ADDR']){
    $valid_state = 'valid';
    $valid_state = 'invalid';
if($valid_state != 'valid')