Home  >  Article  >  Backend Development  >  htaccess file sets password protection for site folders

htaccess file sets password protection for site folders

WBOY
WBOYOriginal
2016-07-25 08:54:291261browse
  1. AuthType Basic
  2. AuthName "restricted area"
  3. AuthUserFile /full/path/to/passwordprotected/.htpasswd
  4. require valid-user
Copy code

The AuthType in the first line refers to the adopted Authentication method. Here we choose the general Basic authentication method. It should be noted that the password transmission process in Basic authentication method is not encrypted. The more secure method is Digest, but the Digest authentication method requires the support of the mod_auth_digest module. It is best before use. First check whether the server has enabled the module; the authentication name recorded in the AuthName in the second line will be displayed on the authentication inquiry box. If there are multiple authentications, the authentication name can make it clearer what user name and password to enter for the current authentication. , unfortunately the authentication name does not seem to support Chinese; on the third line, enter the authentication file path where the user password is stored.

  1. username:123456
  2. username:654321
  3. users:12tir.zIbWQ3c
Copy the code

After creating the file, we need to inject the username and password into this file, if you are using Linux or Unix class Operating system, you can use the htpasswd command. If you can log in to your server through SSH, then you can use htpasswd to manage the username and password in the .htpasswd file. If not, there are many online tools (such as http:/ /www.htaccesstools.com/htpasswd-generator/ or http://www.4webhelp.net/us/password.php) can help you generate passwords for use in .htpasswd files. Or generate with php

  1. // Password to be encrypted for a .htpasswd file
  2. $clearTextPassword = 'some password';
  3. // Encrypt password
  4. $password = crypt($clearTextPassword, base64_encode($clearTextPassword ));
  5. // Print encrypted password
  6. echo $password;
  7. ?>
Copy the code

Finally, test it, create a new folder passwordprotected, upload the two files and test them together Put the file test.php into the folder and upload the folder to the server root folder. If you are on the local test server, enter the path of http://localhost/passwordprotected/test.php. Will a popup pop up at this time? What about the authentication inquiry box?

Enter the corresponding account name and password to see the server configuration. If a 500 error occurs in the browser, it is most likely caused by incorrect AuthUserFile path settings.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn