Skip to content
This repository has been archived by the owner on Jul 30, 2021. It is now read-only.

matiniamirhossein/php-httpauth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP HTTP AUTH

Simple php http auth library using http auth basic/digest.

Examples:

<?php
use PHPHttpAuth\PHPHttpAuth;
//basic auth
$httpAuth = new PHPHttpAuth(PHPHttpAuth::AUTH_TYPE_BASIC);
$httpAuth->verify("Protected area", "amir", "1234");

//digest auth
$httpAuth = new PHPHttpAuth(PHPHttpAuth::AUTH_TYPE_DIGEST);
$httpAuth->verify("Protected area", "amir", "1234");

You can also disable auto handling and handle the response yourself. (I don't know the use case though).

<?php
use PHPHttpAuth\PHPHttpAuth;
$httpAuth = new PHPHttpAuth(PHPHttpAuth::AUTH_TYPE_BASIC);
$result = $httpAuth->verify("Protected area", "amir", "1234", false);
if($result !== TRUE){
    //what to do when failed
    $httpAuth->getAdaptor()->sendHeaders();
    if ($result === PHPHttpAuth::AUTH_USERNAME_WRONG) {
        die("Invalid username provided.");
    } else if ($result === PHPHttpAuth::AUTH_PASSWORD_WRONG) {
        die("Invalid password provided."); //basic auth only
    } else if ($result === PHPHttpAuth::AUTH_RESPONSE_WRONG) {
        die("Invalid response provided."); //digest auth only
    }
}
echo "Authentication succeed.";

Multi user authentication

<?php
use PHPHttpAuth\PHPHttpAuth;
$httpAuth = new PHPHttpAuth(PHPHttpAuth::AUTH_TYPE_BASIC);
$data = [
    'user1' => '1',
    'user2' => '2'
];
$username = $httpAuth->getUsername();
if(!isset($data[$username])){
    $httpAuth->getAdaptor()->sendHeaders();
    die("Invalid username provided.");
}
$httpAuth->verify("Protected area", $username, $data[$username]);

About

Simple php http auth library using http auth basic/digest.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages