PHP Classes

File: tests/www_digestqopbis.php

Recommend this page to a friend!
  Classes of Kevin Muret   PHP HTTP Basic Authentication   tests/www_digestqopbis.php   Download  
File: tests/www_digestqopbis.php
Role: Example script
Content type: text/plain
Description: Class source
Class: PHP HTTP Basic Authentication
Handle requests that require user authentication
Author: By
Last change:
Date: 5 years ago
Size: 1,977 bytes


Class file image Download
;namespace KevinMuret\HttpAuth
;define('HTTPAUTHDIR', '../httpauth/src')
HttpAuth extends DigestQOP {
// List of users and passowrds
private $users_pwds = array(
'test' => 'foobar'
// Method to fetch secret token (according given Digest parameters)
public function getSecret($digest){
// Check that username is not empty and exists
;return ($username = $digest['username']) && array_key_exists($username, $this->users_pwds)
// For example this generate the secret token to be stored in secured application.
? $this->createSecret($username, $this->users_pwds[$username]) : null

// Start session before instanciate because ->isLogged() wil be called at this time
// If authorization already started ('nonce' value must be re-used)
;if (array_key_exists('auth_nonce', $_SESSION))
$auth = new HttpAuth(null, $_SESSION['auth_nonce'], ++$_SESSION['auth_nc'])
// If not initalize session variables with a generated 'nonce' value
;else if ($auth = new HttpAuth())
// Should be completely reseted (ex: in case of others methods elsewhere on the same domain)
$_SESSION = array('auth_nonce' => $auth->nonce())
// Check authentication status
;switch ($auth->status){
// Make sure there is no bypass to this login system
;if (array_key_exists('logged', $_SESSION))
// Force the counter to be zero
;$_SESSION['auth_nc'] = 0
// Ask for autorization (HTTP Code: 401)
// Login were just made !
$auth::LOGGED:// Or previously logged !
;echo "Logged successfully !"
session_destroy()// Keep temporary files cleaner
    // 401 Code needed for re-asking password (keeping the parameters)
"Login failed !"