PHP code example of rikudou / http-basic-auth

1. Go to this page and download the library: Download rikudou/http-basic-auth library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rikudou / http-basic-auth example snippets




use Rikudou\HttpBasicAuth;

// message displayed in standard browser window
$message = "Please input your username and password!"; 

// add the message to the object
$auth = new HttpBasicAuth($message);

// set the callback, it accepts two parameters, username and password and should
// return true (auth succeeded) or false (auth failed)
// in callback you can do pretty much everything, connect to your db, call classes, etc.
// the callback can be any callable (see http://php.net/manual/en/language.types.callable.php)
$auth->setCallback(function($username, $password) {
   if($username == "foo" && $password == "bar") {
     return true;
   }
   return false;
});

$result = $auth->auth();

if($result) { // auth succeeded
  
} else { // auth failed
  
}




use Rikudou\HttpBasicAuth;

$auth = new HttpBasicAuth("Please, authorize", function($username, $password){
  return true;
});




use Rikudou\HttpBasicAuth;

$auth = new HttpBasicAuth("Authorize");

try {
  $auth->auth();
} catch (Exception $exception) {
  var_dump($exception->getCode() == HttpBasicAuth::ERR_NO_CALLBACK); // true
}

$auth->setCallback(1); // invalid callback

try {
  $auth->auth();
} catch (Exception $exception) {
  var_dump($exception->getCode() == HttpBasicAuth::ERR_CALLBACK_NOT_CALLABLE); // true
}