This is not actually a PHP code or command but since you have reached this article it simply means you are into PHP Programming………and since Apache is THE webserver mostly used for open source web programming then might as well put this article on the PHP Basics Category.

So you want the easiest way to password protect your pages or perhaps the whole web folder by giving someone a username and a password to access a certain content or a web page. Ok so what do you need??

1. Make sure your using Apache Web Server (im not sure if this works in IIS)
2. .htaccess file (should be located in the folder where the pages you want to protect is)
3. .htpasswd file (should be located somewhere in the webserver, just so you know where you saved it)

STEPS:

1. Create a .htaccess file or htaccess.txt (since you might have some problems when doing this with MS Windows)
2. Put this in your .htaccess file:

AuthName "LOGIN WINDOW NAME"
AuthType Basic
AuthUserFile /folderwhereyourpasswordislocated/.htpasswd
require valid-user

3. Edit 1st line with whatever you want to call it
4. Edit 3rd line where the .htpasswd will be located (which is somewhere in your web server). Dont change anything anymore on the .htaccess file
5. Upload the .htacess file or your htaccess.txt on your web server where you want to password protect a page (e.g. /public_html/secretpage/)
6. In case you save it to htaccess.txt Rename htaccess.txt to .htaccess using your favorite ftp application (usually you highlight it, right click, then rename) . If not go to Step 7.
7. Create your .htpasswd file or htpasswd.txt
8. Generate the .htpasswd contents by going here: http://www.advancehost.com/htpasswd.html or look for HTPASSWD GENERATOR in Google
9. Fill the form and click submit
10. It will have something like this: (i entered username: user password: pass)

Success!

Here is your crypted password. Copy and paste the output to your htpasswd file:

user:pauONM/HSu9pM (this is just an example)

11. Copy/paste user:pauONM/HSu9pM to .htpasswd then save
12. Upload .htpasswd to the location you put on step no. 4
13. Test it! Voila your page is protected!