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 ?
Hi, You may follow the steps given in the following link http://johnbokma.com/mexit/2007/01/09/password-protecting-single-file-htaccess.html
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:
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.