Si potrebbe fare con php ma risulterebbe un po' complesso.
Questo genere di cose si fano normalmente tramite i file .htaccess di apache

http://httpd.apache.org/docs/2.2/howto/htaccess.html
http://httpd.apache.org/docs/2.2/mod...ost.html#allow