Skip to content

Instantly share code, notes, and snippets.

@naosim
Created October 4, 2017 21:46
Show Gist options
  • Save naosim/3d2d1f825baa6e533c238b86fceea82e to your computer and use it in GitHub Desktop.
Save naosim/3d2d1f825baa6e533c238b86fceea82e to your computer and use it in GitHub Desktop.
BasicAuth.php
<?php
function basic_auth_custom(Closure $auth) {
switch (true) {
case !isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']):
case !$auth($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
header('WWW-Authenticate: Basic realm="Enter username and password."');
header('Content-Type: text/plain; charset=utf-8');
die('このページを見るにはログインが必要です');
}
}
function basic_auth(string $id, string $pw) {
basic_auth_custom(function(string $inputId, string $inputPw) use ($id, $pw) {
return $inputId === $id && $inputPw === $pw;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment