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? That's why I've found the .htaccess method of protecting files and directories the most reliable. Oh, and it's easy too!
The system requires two files -- the .htaccess file and .htpasswd file.
The .htaccess Code
AuthType Basic
AuthName "restricted area"
AuthUserFile /home/user/html/protected-dir/.htpasswd
require valid-user
The above code protects a directory called "protect-me-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 a phpinfo() and find the DOCUMENT_ROOT value.
The .htpasswd Code
htaccess:htLufA1Qct6Fo
ud64:udC6JIsPbe1Ek
The .htpasswd file contains the usernames and passwords of allowed users. One per line. The passwords are MD5'd for security purposes.