cakephp2.doc_034
最終投稿日:2019年11月20日
基本的にPHPの機能です。
制御したいコントローラに記述します。
public function beforeFilter(){
parent::beforeFilter();
//Basic認証
//Cake構文
$this->autoRender = false;
$loginId = 'hoge';
$loginPassword = 'hogehoge';
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Please enter your ID and password"');
header('HTTP/1.0 401 Unauthorized');
die("id / password Required");
} else {
if ($_SERVER['PHP_AUTH_USER'] != $loginId
|| $_SERVER['PHP_AUTH_PW'] != $loginPassword) {
header('WWW-Authenticate: Basic realm="Please enter your ID and password"');
header('HTTP/1.0 401 Unauthorized');
die("Invalid id / password combination. Please try again");
}
}
//Cake構文
$this->autoRender = true;
}
【注意】
PHP CGI 版では動きません!!