Skip to content

Instantly share code, notes, and snippets.

@EITANINOMIYA
Last active December 15, 2016 05:57
Show Gist options
  • Save EITANINOMIYA/695d09543afdecf7d0254c0c9cec3747 to your computer and use it in GitHub Desktop.
Save EITANINOMIYA/695d09543afdecf7d0254c0c9cec3747 to your computer and use it in GitHub Desktop.
【PHP】basic認証
/*#################################################
# phpでbasic認証したい場合
#################################################*/
<?php
// GoogleAppEngineの場合、以下の処理が必要
// 参考:http://d.hatena.ne.jp/chi-bd/20150815/1439620561
if (!isset($_SERVER['PHP_AUTH_USER']) and isset($_SERVER['HTTP_AUTHORIZATION'])) {
$arr = explode(" ", $_SERVER['HTTP_AUTHORIZATION']);
$arr = explode(":", base64_decode($arr[1]));
$_SERVER['PHP_AUTH_USER'] = $arr[0];
$_SERVER['PHP_AUTH_PW'] = $arr[1];
}
$loginid = 'hoge';
$password = 'hogehoge';
switch (true) {
case !isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']):
case $_SERVER['PHP_AUTH_USER'] !== $loginid:
case $_SERVER['PHP_AUTH_PW'] !== $password:
header('WWW-Authenticate: Basic realm="Enter username and password."');
header('Content-Type: text/plain; charset=utf-8');
die('このページを見るにはログインが必要です');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment