LoginSignup
6
6

More than 5 years have passed since last update.

PHPのBasic認証

Posted at
basic認証
$userArray = array("meishukan" => "minami2500");

basic_auth($userArray);
function basic_auth($auth_list,$realm="Restricted Area",$failed_text="認証に失敗しました"){

    if (isset($_SERVER['PHP_AUTH_USER']) and isset($auth_list[$_SERVER['PHP_AUTH_USER']])){
        if ($auth_list[$_SERVER['PHP_AUTH_USER']] == $_SERVER['PHP_AUTH_PW']){
            return $_SERVER['PHP_AUTH_USER'];
        }
    }

    header('WWW-Authenticate: Basic realm="'.$realm.'"');
    header('HTTP/1.0 401 Unauthorized');
    header('Content-type: text/html; charset='.mb_internal_encoding());

    die($failed_text);
}

一番ハマったのは、PHPがApacheモジュールとして動いているときのみ対応。
fastcgiやcgiアプリケーションの際は、だめ。

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6