How can I access the apache basic-auth username in a PHP script?
We can do HTTP basic authentication URL with @ in password. We have to pass the credentials appended with the URL. The username and password must be added with the format − https://username:password@URL.
Once the user has filled in a username and a password, the URL containing the PHP script will be called again with the predefined variables PHP_AUTH_USER , PHP_AUTH_PW , and AUTH_TYPE set to the user name, password and authentication type respectively. These predefined variables are found in the $_SERVER array.
You can store your Authorization header values in localStorage or sessionStorage. The value of Authorization header, stored in LocalStorage, will be automatically each time you make HTTP requests.
After receiving user authentication details in PHP, it compares the form data with the user database by executing a query by using the connection object. The query binds the username entered by the user via HTML form. Then, it verifies the password hash with the entered password to return the authentication results.
Simply access this variable:
$_SERVER['PHP_AUTH_USER']
HTTP Auth in the PHP Manual would be a good starting place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With