Skip to content

Instantly share code, notes, and snippets.

@adalenv
Forked from rchrd2/test-php-basic-auth.php
Created August 6, 2018 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adalenv/5fcede98d0ea5787df6f125f8e88e508 to your computer and use it in GitHub Desktop.
Save adalenv/5fcede98d0ea5787df6f125f8e88e508 to your computer and use it in GitHub Desktop.
PHP basic auth example
<?php
function require_auth() {
$AUTH_USER = 'admin';
$AUTH_PASS = 'admin';
header('Cache-Control: no-cache, must-revalidate, max-age=0');
$has_supplied_credentials = !(empty($_SERVER['PHP_AUTH_USER']) && empty($_SERVER['PHP_AUTH_PW']));
$is_not_authenticated = (
!$has_supplied_credentials ||
$_SERVER['PHP_AUTH_USER'] != $AUTH_USER ||
$_SERVER['PHP_AUTH_PW'] != $AUTH_PASS
);
if ($is_not_authenticated) {
header('HTTP/1.1 401 Authorization Required');
header('WWW-Authenticate: Basic realm="Access denied"');
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment