Results 1 to 4 of 4

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

  1. #1

    Default 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?

  2. #2
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,683
    Blog Entries
    4

    Default Re: anyone know how to put IP restrictions on a single 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.

  3. #3
    Join Date
    Nov 2010
    Location
    Youngstown, OH
    Posts
    155

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

    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:
    Code:
    <Files admin.php>
    AuthName "Dialog prompt"
    AuthType Basic
    AuthUserFile /home/username/.htpasswd
    Require valid-user
    </Files>


    alt:
    Code:
    <Files admin.php>
    deny from all
    allow from <ip>
    </Files>
    reference: .htaccess tricks and tips.. part one: tips, tricks, hints, examples; juicy .htaccess information.
    "Hide and deny files.." section.

    Good Luck.
    Lenovo: OpenSUSE 11.4/32 | Xfce 4.8.1 | Intel Core2 Duo T7200 | Intel 945GM/GMS | 4G RAM | Firefox 8.0 | Thunderbird 3.1.16

  4. #4
    Join Date
    Aug 2008
    Location
    Behind the 8 ball
    Posts
    116

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

    You could try something like this in your PHP script.

    Code:
    if ($_SERVER['REMOTE_ADDR'] == "10.10.10.10" ) {
       //do something
    }
    else {
       echo "Go away"
       exit;
    }
    Good luck,
    Hiatt

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •