PHP code example of pflorek / php-basic-auth

1. Go to this page and download the library: Download pflorek/php-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/ */

    

pflorek / php-basic-auth example snippets



use Psr\Http\Message\RequestInterface;
use \PFlorek\BasicAuth\BasicAuth;

//Given request with header line 'Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='
$credentials = $this->basicAuth->obtainCredentials($request);

var_dump($credentials);

//object(Credentials)#1 (2) {
//  ["username":"Credentials":private]=>
//  string(7) "Aladdin"
//  ["password":"Credentials":private]=>
//  string(11) "open sesame"
//}


use Psr\Http\Message\RequestInterface;
use \PFlorek\BasicAuth\BasicAuth;

$credentials = new Credentials('Alladin, 'open sesame');
$request = $this->basicAuth->addCredentials($request, $credentials);

var_dump($request->getHeaderLine('WWW-Authenticate'));

//string(34) "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="


use Psr\Http\Message\ResponseInterface;
use \PFlorek\BasicAuth\BasicAuth;

$response = $this->basicAuth->addChallenge($response, 'WallyWorld);

var_dump($response->getHeaderLine('WWW-Authenticate'));

//string(24) "Basic realm=\"WallyWorld\""

var_dump($response->getStatusCode());

//int(401)
bash
composer