Apply Http Basic Auth


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function apply_basic_auth() {
  2. $login = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';
  3. $password = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
  4. if(!check_credentials($login, $password)) {
  5. header('WWW-Authenticate: Basic realm="My Realm"');
  6. header('HTTP/1.0 401 Unauthorized');
  7. echo "You need to provide a valid login and password.";
  8. }
  9. }
  10.  
  11. function check_credentials($login, $password) {
  12. return $login == 'login' && $password == 'password';
  13. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.