Last active
December 15, 2016 05:57
-
-
Save EITANINOMIYA/695d09543afdecf7d0254c0c9cec3747 to your computer and use it in GitHub Desktop.
【PHP】basic認証
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*################################################# | |
# 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