Webtech Walker

PHPでBasic認証

自分のコピペ用にメモ。

<?php
$user   = "user";
$passwd = "password";

basic_auth($user,$passwd);

echo "ようこそ$user";

function basic_auth($user = "", $passwd = "") {
  if($user !== $_SERVER['PHP_AUTH_USER'] || $passwd !== $_SERVER['PHP_AUTH_PW']) {
    header('WWW-Authenticate: Basic realm="ma enquete"');
    header('HTTP/1.1 401 Unauthorized');
    echo 'Authentication failure.';
    exit;
  }
}
このエントリーをはてなブックマークに追加