Create a file called ". htpasswd".
The name can be anyone that suits you, not necessarily ". htpasswd".
Paste this into the ". htpasswd":
login:$apr1$pfIh.j7l$Zlqiecx1ZoYfEoUn1QVA50
"login" is the user
"$apr1$pfIh.j7l$Zlqiecx1ZoYfEoUn1QVA50" is an encrypted string. The password is "pass".
In htaccess, add:
AuthType Basic
AuthName "Password Protected Area"
AuthUserFile /caminho/absoluto/do/arquivo/.htpasswd
Require valid-user
That’s enough to start using.
To generate the encrypted password, there are hundreds of sites like this: http://www.htaccesstools.com/htpasswd-generator/. Just search in google "htaccess password Generator" or you can generate without internet use.
*So far we have approached in a superficial way. If you want to know more about the subject, continue with the reading below.
Generating password
For Windows environment, in the Apache installation directory, in the "bin" folder, you will find the executable "htpasswd.exe". By Windows CMD, access the folder of that file, example
cd C:\Apache\httpd-2.4.20-win64-VC14\bin
*The exact location varies according to the installation on your system.
The password can have different types of encryption and can even be generated as plain/text
, i.e., in "plain text", without encryption. Default is MD5. Note that the chosen password type affects the parameter AuthType
, therefore, if you encrypt the password with another format like crypt, set the AuthType
equivalent to encryption used. See documentation: http://httpd.apache.org/docs/current/howto/auth.html
*To generate the password in other environments like linux and mac, follow the same logic. Just modify the path of the executable and some features.
Safety tips
The file path . htpasswd must be in a private place with no public access.
For example, if the site index is in c:/www/site/index.php, put the file in a folder outside the public folder
HOW NOT TO:
c:/www/site/.htpasswd
This way, third parties can download the password file. So, avoid putting in a public access location
Suggestion of where to put:
c:/www/.htpasswd
Nomenclature of the password file
The default name is . htpasswd because the default Apache installation checks if this file exists and if it does, it is locked from external access if it is in a public directory. This is for cases where there is an oversight and leave the file with public access.
Despite this, do not fully trust, as not all environments can have the same rule by default. Just in case, leave the file outside the public folder.
For more details on the subject, read the documentation: https://httpd.apache.org/docs/current/programs/htpasswd.html#examples
Allow specific files and directories
To allow open access to specific subdirectories or files, add the URI-based rule:
SetEnvIf Request_URI "(/caminho/completo/do/diretorio1/)$" allow
SetEnvIf Request_URI "(/caminho/completo/do/diretorio2/)$" allow
SetEnvIf Request_URI "(/caminho/completo/do/diretorio3/)$" allow
SetEnvIf Request_URI "(/caminho/completo/aquivo/especifico\.php)$" allow
Order allow,deny
Allow from env=allow
Satisfy any
An alternative using PHP only
if (
!isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])
|| $_SERVER['PHP_AUTH_USER'] !== 'login'
|| $_SERVER['PHP_AUTH_PW'] !== 'pass'
) {
header('WWW-Authenticate: Basic realm="Enter username and password."');
header('Content-Type: text/plain; charset=utf-8');
echo 'Restricted Area'; exit;
}
In that case you would have to add the script to all the pages you want to lock with the password.
If your system has a router, for example, then just add the code to the router, usually "index.php".
Against
Other files like images, directory access, etc, finally, everything that is not PHP and does not have the script, will have free access.
Despite this, for a general purpose like keeping the site blocked from public access while it is in development or maintenance, it is enough.
Pro
The counterpoint can also be a positive one, as image files are freely accessible, search engines like google can still index images while the system is in temporary maintenance, for example. Thus avoiding losing image rankings and indexes while the site is blocked.
It is also more flexible as it can create conditions directly with PHP, where it is easier to handle. In your case, you want to allow free access to the login.php file. Then it would be enough to create a condition that checks which file name is running and generate the conditions you want.
Thanks Daniel, very good your tutorial guy. But I have a doubt here. The basic method you put right at the beginning worked perfect, but I want you to ask for password only at the root (www folder) so that people don’t see my wamp page and the folders listed above, on the way to my site, users can access without password. Got it?
– Bene
In this case you could use the alternative with PHP if possible. If it is not possible, I believe that it is enough to define the rule not to block subdirectories. I added an example in the answer "Allow specific files and directories"
– Daniel Omine
Thanks so much for the help man!!
– Bene