[PHP] Basic authentication

Aug 27, 2020 PHP CakePHP Laravel password basic authentication

Basic authentication is set so that when you try to browse test.php, you will be prompted to enter your ID and password.

//Write in the form of ID:password. After the password, enter a new line.

<!-- If you add. At the beginning of the file name, it becomes a hidden file. -->
AuthType Basic
AuthName "Please enter your ID and password"
AuthUserFile /Applications/MAMP/htdocs/xxxxxxx/mainte/.htpasswd
require valid-user

//Specify the location of the password file with AuthUserFile
//require only valid-user authenticated users can enter the page. After require valid-user, enter a new line.

Determine the text on the screen that prompts you to enter the ID and password with AuthName. The file that describes the password in AuthUserFile is specified with the full path.

In the .htaccess file, describe the path to .htpasswd. The htpasswd file describes the user ID and encrypted password.

// The location of the file that records the password
echo __FILE__;
//_FILE__, current file location w displayed in browser
// /Applications/MAMP/htdocs/xxxxxxx/xxxxxx/test.php


// password encryption
echo(password_hash('xxxxxxx', PASSWORD_BCRYPT));
//Encrypt the password with the password_hash function. The password to be encrypted is specified in the first argument and the encryption type is specified in the second argument.
// $2y$10$KzSlgR.Ze4qmJS03gAFcJO80hdKuPrZ3XAVkMHa5Wf792M0mVX/nS

Basic authentication can be applied to test.php by putting .htaccess and .htpasswd in the folder containing test.php. In other words, basic authentication can be applied by placing these two files in the folder containing the file that you want to use basic authentication.