Monday, April 1, 2013

HTTP Authentication

HTTP authentication 
with PHP is used to authenticate a webpage. By using this when user open a website, it will prompt a username and password.
Username will stored in "PHP_AUTH_USER" and password will stored in "PHP_AUTH_PW"


Basic example of of Authentication is below:

$username='test';
$password='test123';
function authenticate(){
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
}
if (!isset($_SERVER['PHP_AUTH_USER'])) {
  authenticate();
    echo 'Text to send if user hits Cancel button';
    exit;
} else {
    $u=$_SERVER['PHP_AUTH_USER'];
    $p=$_SERVER['PHP_AUTH_PW'];
    if(!($u==$username && $p==$password)){
    authenticate();
    }
    
}

1 comment:

  1. How to create Http Authentication with .htaccess
    1) Create .htaccess File
    http://www.htaccesstools.com/htaccess-authentication/
    2) Create .htpasswd File
    http://www.htaccesstools.com/htpasswd-generator/
    3) Upload Both File in server
    4) done

    ReplyDelete