アクセス認証 15−1
<?php
error_reporting(1);
/*-------------------------------------------------書込まれたIDとパスワードを読む*/
$file = fopen("password.txt","r");
rewind($file);
$cnt =0;
while(!feof($file)){
$idpas[$cnt] = fgets($file);
$cnt++;
}
fclose($file);
/*-------------------------------------------------キャンセルした場合*/
if(!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="wakaran"');
header('HTTP/1.0 401 Unauthorized');
echo <<<ERR1
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="ja"><head>
<meta http-equiv="Content-type" content="text/html; charset=EUC-JP">
<meta http-equiv="Content-Style-Type" content="text/css">
<link rel="stylesheet" href="../style.css">
<title>アクセス認証</title>
</head><body><div class="file2">
キャンセルしましたね。<a href="15_01_pass.php">もう一回</a>
</div><div style="width:650;margin:30px;padding:20px;font-size:15px;background-color:#ffffff">
<a href="second.php?page_num=515">解説にもどる</a>
</div></body></html>
ERR1;
/*-------------------------------------------------入力した場合*/
}elseif(isset($_SERVER['PHP_AUTH_USER'])){
$count = count($idpas);
$cnt = 0 ;
while($cnt<$count){
list($id,$pass) = explode(":",$idpas[$cnt]); /*IDとパスが合えばokを代入*/
if(($_SERVER['PHP_AUTH_USER'] == $id) and ($_SERVER['PHP_AUTH_PW'] == rtrim($pass))) {
$ok = "ok";
}
$cnt++;
}
if($ok != "ok"){ /*IDとパスが合わない場合*/
header('WWW-Authenticate: Basic realm="wakaru"');
header('HTTP/1.0 401 Unauthorized');
echo <<<ERR2
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="ja"><head>
<meta http-equiv="Content-type" content="text/html; charset=EUC-JP">
<meta http-equiv="Content-Style-Type" content="text/css">
<link rel="stylesheet" href="../style.css">
<title>アクセス認証</title>
</head><body><div class="file2">
3回入力に失敗しました。<a href="15_01_pass.php">もう一回</a>
</div><div style="width:650;margin:30px;padding:20px;font-size:15px;background-color:#ffffff">
<a href="second.php?page_num=515">解説にもどる</a>
</div></body></html>
ERR2;
}
}
/*-------------------------------------------------ログインに成功した場合*/
if($ok=="ok"){
?> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="ja"><head>
<meta http-equiv="Content-type" content="text/html; charset=EUC-JP">
<meta http-equiv="Content-Style-Type" content="text/css">
<link rel="stylesheet" href="../style.css">
<title>アクセス認証</title>
</head><body><div class="file2">
<h2>アクセス認証</h2>
<P>認証に成功しました!</p>
<?php
echo "入力したユーザー名:{$_SERVER['PHP_AUTH_USER']}<br>";
echo "入力したパスワード:{$_SERVER['PHP_AUTH_PW']}";
?><form action="15_01_pass.php" method="post">
IDとPASSを追加しよう。<br>
IDは英語小文字、PASSは数字、各半角4文字<br><br>
ID <input type="text" name="newid" size="10">
PASS <input type="text" name="newpas" size="10">
<input type="submit" value="追加!">
</form>
<?php
$newid = $_POST['newid'];
$newpas = $_POST['newpas'];
$length_i = strlen($newid);
$length_p = strlen($newpas);
if($length_i == 4 and $length_p == 4){
if(ereg("[a-z]{4}",$newid)){
if(ereg("[0-9]{4}",$newpas)){
$filepointer=fopen("password.txt", "a");
flock($filepointer, LOCK_EX);
fputs($filepointer, "¥n".$newid.":".$newpas);
flock($filepointer, LOCK_UN);
fclose($filepointer);
}
}
}
?></div></body></html>
<?php
}
?>