anyone know how to put IP restrictions on a single PHP file?

I usually use .htaccess to restrict access to directories. But what if I just wanted to secure a single php file?

Is there some sort of code that would allow me to say ONLY THIS IP can access this PHP file?

You can put access restrictions inside a <Files> or <FilesMatch> directive.

core - Apache HTTP Server

You probably want to nest it inside a <Directory> directive to ensure that the correct path is matched.

The FilesMatch or Files directives in an .htaccess file would “suggest” (at least to me) that it can be done with an allow from <ip> and deny from all, but this is a “creative alternative” to what I have read, it may not work.

Here’s the original code I saved: and the “creative alternative”


<Files admin.php>
AuthName "Dialog prompt"
AuthType Basic
AuthUserFile /home/username/.htpasswd
Require valid-user


<Files admin.php>
deny from all
allow from <ip>

reference: .htaccess tricks and tips… part one: tips, tricks, hints, examples; juicy .htaccess information.](
“Hide and deny files…” section.

Good Luck.

You could try something like this in your PHP script.

if ($_SERVER'REMOTE_ADDR'] == "" ) {
   //do something
else {
   echo "Go away"

Good luck,