admin panel and password protection
This commit is contained in:
101
admin.php
Normal file
101
admin.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
session_start();
|
||||
|
||||
// Path to password storage
|
||||
$passwordFile = __DIR__ . '/passwords.json';
|
||||
if (!file_exists($passwordFile)) {
|
||||
file_put_contents($passwordFile, '{}');
|
||||
}
|
||||
$passwords = json_decode(file_get_contents($passwordFile), true);
|
||||
|
||||
// Simple admin login (hardcoded for demo)
|
||||
$adminPassword = 'admin123';
|
||||
$loggedIn = isset($_SESSION['admin']) && $_SESSION['admin'] === true;
|
||||
|
||||
if (isset($_POST['admin_login'])) {
|
||||
if ($_POST['admin_password'] === $adminPassword) {
|
||||
$_SESSION['admin'] = true;
|
||||
$loggedIn = true;
|
||||
} else {
|
||||
$error = 'Incorrect admin password.';
|
||||
}
|
||||
}
|
||||
|
||||
if ($loggedIn && isset($_POST['set_album_password'])) {
|
||||
$album = $_POST['album_name'];
|
||||
$pw = $_POST['album_password'];
|
||||
if ($album && $pw !== null) {
|
||||
$passwords[$album] = $pw;
|
||||
file_put_contents($passwordFile, json_encode($passwords));
|
||||
$success = "Password set for album '$album'.";
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($_POST['logout'])) {
|
||||
session_destroy();
|
||||
header('Location: admin.php');
|
||||
exit;
|
||||
}
|
||||
|
||||
// Get album list
|
||||
$dir = __DIR__ . '/images/';
|
||||
$albums = [];
|
||||
if (is_dir($dir)) {
|
||||
foreach (scandir($dir) as $album) {
|
||||
if ($album === '.' || $album === '..' || !is_dir($dir . $album)) continue;
|
||||
$albums[] = $album;
|
||||
}
|
||||
}
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Admin - Album Passwords</title>
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; background: #f0f0f0; }
|
||||
.container { max-width: 500px; margin: 40px auto; background: #fff; padding: 24px; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.12); }
|
||||
h2 { text-align: center; }
|
||||
label { font-weight: bold; }
|
||||
input, select { width: 100%; padding: 8px; margin: 8px 0 16px 0; border-radius: 4px; border: 1px solid #ccc; }
|
||||
button { padding: 8px 16px; background: #0078d4; color: #fff; border: none; border-radius: 4px; font-weight: bold; cursor: pointer; }
|
||||
.msg { color: green; }
|
||||
.error { color: red; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h2>Administrator Mode</h2>
|
||||
<?php if (!$loggedIn): ?>
|
||||
<form method="post">
|
||||
<label for="admin_password">Admin Password:</label>
|
||||
<input type="password" name="admin_password" id="admin_password" required>
|
||||
<button type="submit" name="admin_login">Login</button>
|
||||
<?php if (isset($error)) echo "<div class='error'>$error</div>"; ?>
|
||||
</form>
|
||||
<?php else: ?>
|
||||
<form method="post">
|
||||
<label for="album_name">Select Album:</label>
|
||||
<select name="album_name" id="album_name" required>
|
||||
<?php foreach ($albums as $album): ?>
|
||||
<option value="<?= htmlspecialchars($album) ?>"><?= htmlspecialchars($album) ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<label for="album_password">Set/View Password:</label>
|
||||
<input type="text" name="album_password" id="album_password" required>
|
||||
<button type="submit" name="set_album_password">Set Password</button>
|
||||
</form>
|
||||
<?php if (isset($success)) echo "<div class='msg'>$success</div>"; ?>
|
||||
<form method="post" style="margin-top:16px;">
|
||||
<button type="submit" name="logout">Logout</button>
|
||||
</form>
|
||||
<h3>Current Album Passwords:</h3>
|
||||
<ul>
|
||||
<?php foreach ($passwords as $album => $pw): ?>
|
||||
<li><strong><?= htmlspecialchars($album) ?>:</strong> <?= htmlspecialchars($pw) ?></li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user