Skip to content

Instantly share code, notes, and snippets.

@Nav-Appaiya
Created June 23, 2021 14:19
Show Gist options
  • Select an option

  • Save Nav-Appaiya/b1d4a835c781d4b7a42f706c0b418198 to your computer and use it in GitHub Desktop.

Select an option

Save Nav-Appaiya/b1d4a835c781d4b7a42f706c0b418198 to your computer and use it in GitHub Desktop.
pass password protection php
<?php
function require_auth() {
$AUTH_USER = 'admin';
$AUTH_PASS = 'password';
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;
}
}
@Nav-Appaiya
Copy link
Author

minified version

function require_auth($u,$p){$AUTH_USER=$u;$AUTH_PASS=$p;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;}}

require_auth('admin','password');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment