サーバー側の(例)会員専用フォームにパスワードを設定して閲覧制限するパスワード管理
仕様 | |
1.パスワード登録 2.パスワードの変更(現パスワード・新パスワード) 3.パスワードログイン 4.パスワード削除 5.パスワード管理(自動生成、ハッシュ化) 6.登録、削除後の各ファイルの権限仕様(登録後はパスワード登録フォームは非表示) 7.各種パスワードエラー表示 | |
password/パスワード管理 PHP CSS ├── register.php(パスワード登録) ├── login.php(ログイン) ├── change_password.php(パスワード変更) ├── auth.php(ファイル権限) ├── login.css ├── admin/ ← 🔒 管理ページ専用フォルダ(アクセス制限) ├── delete_account.php(パスワード削除) ├── passwords.txt(データ記録) | |
![]() | ![]() |
![]() | ![]() |
ご利用について | |
パスワード管理システムのコードを公開します。追加・変更・削除・修正・デザイン変更等は一切制限はありません。また、コピー&ペーストでご使用した場合に、動作不具合等発生しても作成者は責任を負う事は致しません。ファイルパスは環境に合わせて変更して、サーバーのディレクトリ管理はアクセス制限付きのディレクトリ下に置くことを推奨します。 ※全て変更なしでご使用された場合は、どこか片隅に、ごく小さくMOMOPLANと表記して頂けると嬉しいです | |
各ファイル | |
register.php login.php change_password.php delete_account.php auth.php login.css passwords.txt 実装デモ説明 | |
register.php |
<?php
session_start();
$message = "";
$imageVisible = false;
// すでにログイン済みの場合は login.php にリダイレクト
if (isset($_SESSION["username"])) {
header("Location: login.php");
exit;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = password_hash($_POST["password"], PASSWORD_DEFAULT);
file_put_contents("passwords.txt", "$username:$password\n", FILE_APPEND);
$message = "登録完了!";
$imageVisible = true; // ログイン成功時に画像を表示
}
?>
<head>
<style>
form {
background-color: #f9f9f9;
padding: 20px;
border: 2px solid #ccc;
border-radius: 10px;
width: 300px;
margin: auto;
}
h2 {
color: #4CAF50;
text-align: center;
}
input[type="text"],
input[type="password"] {
width: 100%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
background-color:rgb(66, 137, 230);
color: white;
padding: 10px;
border: none;
border-radius: 5px;
cursor: pointer;
width: 100%;
}
button:hover {
background-color:rgb(53, 66, 143);
}
.message {
text-align: center;
font-size: 14px;
margin-top: 10px;
color:rgb(34, 67, 255);
font-weight: bold;
}
</style>
</head>
<form method="post">
<h2>管理者専用登録画面</h2>
ユーザー名: <input type="text" name="username" required autocomplete="username">
パスワード: <input type="password" name="password" required autocomplete="current-password">
<button type="submit">登録</button>
<div class="message"><?php echo $message; ?></div>
</form>
login.php |
<?php
session_start();
$message = "";
$imageVisible = false;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];
$lines = file("passwords.txt");
foreach ($lines as $line) {
list($storedUser, $storedPass) = explode(":", trim($line));
if ($username == $storedUser && password_verify($password, $storedPass)) {
$_SESSION["username"] = $username;
$message = "ログイン成功!";
$imageVisible = true; // ログイン成功時に画像を表示
break;
}
}
if (empty($message)) {
$message = "ログイン失敗!";
}
}
?>
<head>
<link rel="stylesheet" href="login.css">
</head>
<body>
<form method="post">
<h2>管理者専用ログイン画面</h2>
ユーザー名: <input type="text" name="username" required autocomplete="username">
パスワード: <input type="password" name="password" required autocomplete="current-password">
<button type="submit">ログイン</button>
<div class="message"><?php echo $message; ?></div>
</form>
<?php if ($imageVisible): ?>
<div class="image-container">
<!---------------以下任意のイベント実行----------------->
<img src="./pass.png" alt="デモ画像" style="max-width: 400px;">
</div>
<?php endif; ?>
</body>
change_password.php |
<?php
session_start();
require 'auth.php';
$message = "";
$message2 = "";
$imageVisible = false; // 画像表示フラグを初期化
$filePath = "passwords.txt";//絶対パス・相対パス指定
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_SESSION["username"];
$currentPassword = $_POST["current_password"];
$newPassword = password_hash($_POST["new_password"], PASSWORD_DEFAULT);
// パスワードファイルの読み込み
$lines = file($filePath);
if (!is_array($lines)) {
die("エラー: パスワードファイルが読み込めません");
}
$newLines = [];
$passwordChanged = false;
foreach ($lines as $line) {
list($storedUser, $storedPass) = explode(":", trim($line));
// ユーザー名が一致し、現在のパスワードが正しければ変更
if ($username == $storedUser && password_verify($currentPassword, $storedPass)) {
$newLines[] = "$username:$newPassword\n";
$passwordChanged = true;
} else {
$newLines[] = $line;
}
}
// パスワード変更成功の場合、ファイル更新
if ($passwordChanged) {
file_put_contents($filePath, implode("", $newLines));
$message = "パスワード変更完了!";
$imageVisible = true; // ログイン成功時に画像を表示
} else {
$message2 = "エラー: 現在のパスワードが間違っています";
$imageVisible = true;
}
}
?>
<style>
form {
background-color: #f9f9f9;
padding: 20px;
border: 2px solid #ccc;
border-radius: 10px;
width: 300px;
margin: auto;
}
h2 {
color: #4CAF50;
text-align: center;
}
input[type="text"],
input[type="password"] {
width: 100%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
background-color:rgb(68, 49, 238);
color: white;
padding: 10px;
border: none;
border-radius: 5px;
cursor: pointer;
width: 100%;
}
button:hover {
background-color:rgb(139, 116, 224);
}
.message {
text-align: center;
font-size: 14px;
margin-top: 10px;
color:rgb(38, 34, 255);
font-weight: bold;
}
.message2 {
text-align: center;
font-size: 14px;
margin-top: 10px;
color: #ff5722;
font-weight: bold;
}
</style>
<form method="post">
<h2>管理者専用パスワード変更</h2>
現在のパスワード: <input type="password" name="current_password" required autocomplete="current_password">
新しいパスワード: <input type="password" name="new_password" required autocomplete="current-new_password">
<button type="submit">変更</button>
<div class="message"><?php echo $message; ?></div>
<div class="message2"><?php echo $message2; ?></div>
</form>
delete_account.php |
<?php
session_start();
require 'auth.php';
$message = "";
$imageVisible = false;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_SESSION["username"];
$filePath = "passwords.txt";//絶対パス・相対パス指定
// ファイルから行を取得
$lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
// フィルター処理
$newLines = array_filter($lines, function($line) use ($username) {
return strpos(trim($line), "$username:") !== 0;
});
// ファイルの更新
file_put_contents($filePath, implode("\n", $newLines));
$message = "アカウント削除完了!"; // 成功メッセージを設定
$imageVisible = true; // ログイン成功時に画像を表示
}
?>
<style>
form {
background-color: #f9f9f9;
padding: 20px;
border: 2px solid #ccc;
border-radius: 10px;
width: 300px;
margin: auto;
}
h2 {
color: #4CAF50;
text-align: center;
}
button {
background-color:rgb(68, 49, 238);
color: white;
padding: 10px;
border: none;
border-radius: 5px;
cursor: pointer;
width: 100%;
}
button:hover {
background-color:rgb(139, 116, 224);
}
.message {
text-align: center;
font-size: 14px;
margin-top: 10px;
color: #ff5722;
font-weight: bold;
}
</style>
<form method="post">
<h2>管理者専用パスワード削除</h2>
<button type="submit">削除</button>
<div class="message"><?php echo $message; ?></div>
</form>
auth.php |
<?php
if (!isset($_SESSION["username"])) {
die("アクセス権限がありません");
}
?>
login.css |
form {
background-color: #f9f9f9;
padding: 20px;
border: 2px solid #ccc;
border-radius: 10px;
width: 300px;
margin: auto;
}
h2 {
color: #4CAF50;
text-align: center;
}
input[type="text"],
input[type="password"] {
width: 100%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
background-color:rgb(66, 137, 230);
color: white;
padding: 10px;
border: none;
border-radius: 5px;
cursor: pointer;
width: 100%;
}
button:hover {
background-color:rgb(53, 66, 143);
}
.message {
text-align: center;
font-size: 14px;
margin-top: 10px;
color: #ff5722;
}
.image-container {
display: flex;
justify-content: center;
margin-top: 20px;
}
button:disabled {
background-color: #ccc;
cursor: not-allowed;
}
以上です・・・・・登録デモ ログインデモ パスワード変更デモ パスワード変更デモ
※上記の各リンクにアクセスした場合、”権限無し”と表示された場合、どなたかがユーザー名、パスワードをテスト入力した場合に表示されます。この場合はユーザー名やパスワードが分からないため、削除ボタンで初期画面に戻り、パスワード登録画面から操作できます。また、全てが表示される場合は、情報を設定していない状態です。
※通常の削除フォームは他のファイルと同じディレクトリの階層には配置しません。サーバーのアクセス制限のあるディレクトリ内で配置するのがこのシステムの前提となっています。