Skip to content

Instantly share code, notes, and snippets.

@eric1234
Last active April 12, 2022 13:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eric1234/5700719 to your computer and use it in GitHub Desktop.
Save eric1234/5700719 to your computer and use it in GitHub Desktop.
Easy HTTP Auth in PHP

Usage

At the top of the page you want to secure:

auth('admin', 'passw0rd')

Now when someone visits that page they must enter admin/passw0rd to see the page.

<?php
# https://gist.github.com/eric1234/5700719
function auth($username, $password, $realm='Administration') {
if( $_SERVER['PHP_AUTH_USER'] != $username ||
$_SERVER['PHP_AUTH_PW'] != $password ) {
header("WWW-Authenticate: Basic realm=\"$realm\"");
header('HTTP/1.0 401 Unauthorized');
echo 'Access Denied';
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment