Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.07 KB | None | 0 0
  1. <?php
  2.  
  3. define( 'HTTP_AUTH_REALM',          '' ); // login üzenet
  4. define( 'HTTP_AUTH_USER',           '' );
  5. define( 'HTTP_AUTH_PASSWORD',       '' );
  6. define( 'PROTECTED_FILE_PATH',      '' ); // html fájl útvonala
  7.  
  8. class HTTPAuth {
  9.  
  10.     public $username;
  11.     public $password;
  12.     public $realm;
  13.     public $user = array();
  14.  
  15.     public function __construct( $username, $password, $realm = '' ) {
  16.         $this->username = $username;
  17.         $this->password = $password;
  18.         $this->realm = $realm;
  19.     }
  20.  
  21.     public function authenticate() {
  22.         if ( $_SERVER['PHP_AUTH_USER'] !== $this->username || $_SERVER['PHP_AUTH_PW'] !== $this->password ) {
  23.             header( 'WWW-Authenticate: Basic realm="' . $this->realm . '"' );
  24.             header( 'HTTP/1.0 401 Unauthorized' );
  25.             return false;
  26.         } else {
  27.             $this->user['username'] = $_SERVER['PHP_AUTH_USER'];
  28.             $this->user['password'] = $_SERVER['PHP_AUTH_PW'];
  29.             return true;
  30.         }
  31.     }
  32. }
  33.  
  34. $httpAuth = new HTTPAuth( HTTP_AUTH_USER, HTTP_AUTH_PASSWORD, HTTP_AUTH_REALM );
  35.  
  36. if ( $httpAuth->authenticate() ) {
  37.     include( PROTECTED_FILE_PATH );
  38. } else {
  39.     echo 'Permission denied';
  40. }
  41.  
  42. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement