Protecting files on your website from unauthorized users can be very important. Even more important is the method by which you accomplish this task. You could use PHP to listen for login authorization information on each page, but that doesn’t protect your images, documents, and other media, does it?
The system requires two files — the .htaccess file and .htpasswd file.
The .htaccess Code
1 2 3 4 |
AuthType Basic AuthName "restricted area" AuthUserFile /home/russianroot/secret_dir/.htpasswd require valid-user |
The above code protects a directory called “secret_dir” at root level. The “AuthUserFile” value is always specific to your hosting configuration. If you don’t know what the value should be, do aphpinfo() and find the DOCUMENT_ROOT value.
The .htpasswd Code
1 2 3 |
russianroot:12eMC4Wi9/C9o #russianroot: 12345 admin:adpexzg3FUZAk #admin: admin demo:devFxxVFZsuos #demo:demo |
The .htpasswd file contains the usernames and passwords of allowed users. One per line. The passwords are crypted for security purposes.