PHP Classes

File: tests/www_digestqop.php

Recommend this page to a friend!
  Classes of Kevin Muret   PHP HTTP Basic Authentication   tests/www_digestqop.php   Download  
File: tests/www_digestqop.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: Digest classes (optional ->isLogged() and using ->getSecret() each request)
Date: 6 years ago
Size: 2,253 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 check if user has been already logged are not ?
    // (Bypass the call to ->getSecret())
public function isLogged(){
array_key_exists('logged', $_SESSION)
// 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'], $_SESSION['auth_secret'])
// 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(), 'auth_secret' => null)
// 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 !
$_SESSION['auth_secret'] = $auth->secret()
$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 !"