https://takayakondo.com/individual-basic-authentication/
ファンクション
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); }
ヘッダー最初
<head> <?php if(!is_home()): if(is_page('5555') ||is_page('5561')||is_page('5563')||is_page('5565')||is_page('5559') ): $userArray = array( "guest" => "passguest" ); basic_auth($userArray); endif; endif; ?>
htaccess
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP:Authorization} ^(.*) RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1] RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule>
ここのぶぶん
RewriteCond %{HTTP:Authorization} ^(.*) RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
参考
https://takayakondo.com/individual-basic-authentication/