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”

Org:

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

alt:

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

reference: .htaccess tricks and tips… part one: tips, tricks, hints, examples; juicy .htaccess information.](http://corz.org/serv/tricks/htaccess.php)
“Hide and deny files…” section.

Good Luck.

You could try something like this in your PHP script.



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

Good luck,
Hiatt