Skip to content

Instantly share code, notes, and snippets.

@pbrisbin
Created October 8, 2010 20:21
Show Gist options
  • Save pbrisbin/617465 to your computer and use it in GitHub Desktop.
Save pbrisbin/617465 to your computer and use it in GitHub Desktop.
<?php
function do_auth() {
// prompt for password
header('WWW-Authenticate: Basic realm="pbrisbin dot com"');
header('HTTP/1.0 401 Unauthorized');
// if user cancels
header('Content-type: text/plain');
echo 'Not authorized.';
exit;
}
function authenticate($_valid_users) {
// credentials not known
if (!isset($_SERVER['PHP_AUTH_USER']))
do_auth();
$user = $_SERVER['PHP_AUTH_USER'];
$pass = $_SERVER['PHP_AUTH_PW'];
// user not known
if (!isset($_valid_users[$user]))
do_auth();
// bad password
if ($_valid_users[$user] != $pass)
do_auth();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment