password protect a single file ?

Discussion in 'Security' started by thesamemanhal, Feb 15, 2010.

  1. #1
    I want to make a text file (located under /home/public_html) to be viewable through a browser by me only.
    One way is to make it password protected, I know how to do this through cpanel for a directory but I don't know how to do it for a single file, also I don't want to move the file in to a private directory because the file already been used in many places ?

    any idea ?
     
    thesamemanhal, Feb 15, 2010 IP
  2. maestria

    maestria Well-Known Member

    Messages:
    705
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #2
    maestria, Feb 17, 2010 IP
  3. thesamemanhal

    thesamemanhal Active Member

    Messages:
    1,126
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    55
    #3
    Thank you maestria.
     
    thesamemanhal, Feb 17, 2010 IP
  4. baonhi41

    baonhi41 Peon

    Messages:
    141
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This is protect directory, not single file.
    Ex: http://site.com/DIR
    Have 2 files: 1.php and 2.php in http://site.com/DIR
    As your way:
    If you browse http://site.com/DIR/1.php and http://site.com/DIR/2.php you must login.
    But author want protect single file: http://site.com/DIR/1.php require login http://site.com/DIR/2.php don't require login

    This is answer:

    For PHP, Add this code after <?php :
    $auth = 1; // Enable: 1, Disable: 0;
    $name='admin';
    $pass='password';
    if($auth == 1) {
    if (!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_USER']!==$name || $_SERVER['PHP_AUTH_PW']!==$pass)
       {
       header('WWW-Authenticate: Basic realm="Hey man! Please login"');
       header('HTTP/1.0 401 Unauthorized');
       exit("Oh no! You are bad man! <a href=http://www.fbi.gov>Exit Now</a>");
       }
    }
    PHP:
     
    baonhi41, Feb 22, 2010 IP
  5. SecureCP

    SecureCP Guest

    Messages:
    226
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Or if you don't want to edit your files:
    
    <FilesMatch "bar.html">
    AuthName "Member Only"
    AuthType Basic
    AuthUserFile /html/username/.htpasswd
    require valid-user
    </FilesMatch>
    
    Code (markup):
    And of course, generate your .htpasswd to /html/username/.htpasswd

    Hope that helps.
     
    SecureCP, Feb 24, 2010 IP