MINI
C:
/
xampp
/
htdocs
/
cek
/
Upload File:
files >> C:/xampp/htdocs/cek/users.php
<?php session_start(); include 'db.php'; // Check if admin $stmt = $pdo->prepare("SELECT role FROM users WHERE id = ?"); $stmt->execute([$_SESSION['user_id'] ?? 0]); $user = $stmt->fetch(); if (!isset($_SESSION['user_id']) || ($user['role'] ?? '') != 'admin') { header("Location: login.php"); exit(); } // Handle add user if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['add_user'])) { $username = $_POST['username']; $password = password_hash($_POST['password'], PASSWORD_DEFAULT); $role = $_POST['role']; $stmt = $pdo->prepare("INSERT INTO users (username, password, role) VALUES (?, ?, ?)"); $stmt->execute([$username, $password, $role]); $_SESSION['message'] = ['type' => 'success', 'text' => 'User berhasil ditambahkan!']; header("Location: users.php"); exit(); } // Handle delete user if (isset($_GET['delete'])) { $id = $_GET['delete']; $stmt = $pdo->prepare("DELETE FROM users WHERE id = ? AND id != ?"); $stmt->execute([$id, $_SESSION['user_id']]); $_SESSION['message'] = ['type' => 'success', 'text' => 'User berhasil dihapus!']; header("Location: users.php"); exit(); } // Get all users $users = $pdo->query("SELECT * FROM users WHERE id != ".$_SESSION['user_id'])->fetchAll(); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Kelola User - Voucher App</title> <script src="https://cdn.tailwindcss.com"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"> </head> <body class="bg-gray-50"> <!-- Notification message --> <?php if (isset($_SESSION['message'])): ?> <div class="fixed top-4 right-4 z-50"> <div class="bg-<?= $_SESSION['message']['type'] == 'success' ? 'green' : 'red' ?>-500 text-white px-4 py-2 rounded-lg shadow-lg flex items-center"> <span class="mr-2"> <?php if ($_SESSION['message']['type'] == 'success'): ?> <i class="fas fa-check-circle"></i> <?php else: ?> <i class="fas fa-exclamation-circle"></i> <?php endif; ?> </span> <span><?= $_SESSION['message']['text'] ?></span> </div> </div> <?php unset($_SESSION['message']); ?> <?php endif; ?> <div class="container mx-auto px-4 py-8"> <!-- Header --> <header class="bg-white shadow"> <div class="container mx-auto px-4 py-4 flex justify-between items-center"> <h1 class="text-xl font-bold"> <i class="fas fa-users mr-2 text-blue-600"></i> Kelola User </h1> <div class="flex items-center space-x-4"> <a href="index.php" class="text-purple-600 hover:text-purple-800"> <i class="fas fa-arrow-left mr-1"></i> Kembali </a> <a href="logout.php" class="text-red-600 hover:text-red-800" title="Logout"> <i class="fas fa-sign-out-alt"></i> </a> </div> </div> </header> <!-- Add User Form --> <div class="bg-white rounded-lg shadow p-6 my-6"> <h2 class="text-lg font-semibold mb-4"> <i class="fas fa-user-plus mr-2"></i>Tambah User Baru </h2> <form method="POST" class="grid grid-cols-1 md:grid-cols-3 gap-4"> <input type="hidden" name="add_user" value="1"> <div> <label for="username" class="block text-sm font-medium text-gray-700">Username</label> <input type="text" name="username" required class="w-full px-4 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500"> </div> <div> <label for="password" class="block text-sm font-medium text-gray-700">Password</label> <input type="password" name="password" required class="w-full px-4 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500"> </div> <div> <label for="role" class="block text-sm font-medium text-gray-700">Role</label> <select name="role" class="w-full px- <select name="role" class="w-full px-4 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500"> <option value="user">User </option> <option value="admin">Admin</option> </select> </div> <button type="submit" class="w-full bg-purple-600 hover:bg-purple-700 text-white font-medium py-2 px-4 rounded-md"> <i class="fas fa-user-plus mr-2"></i> Tambah Pengguna </button> </form> </div> <!-- User List --> <h2 class="text-lg font-semibold mb-4"> <i class="fas fa-users mr-2"></i>Daftar Pengguna </h2> <div class="bg-white rounded-lg shadow p-6"> <table class="min-w-full divide-y divide-gray-200"> <thead class="bg-gray-50"> <tr> <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Username</th> <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Role</th> <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Aksi</th> <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Edit</th> </tr> </thead> <tbody class="bg-white divide-y divide-gray-200"> <?php if (empty($users)): ?> <tr> <td colspan="3" class="px-6 py-4 text-center text-sm text-gray-500">Tidak ada pengguna</td> </tr> <?php else: ?> <?php foreach ($users as $user): ?> <tr> <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?= htmlspecialchars($user['username']) ?></td> <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?= htmlspecialchars($user['role']) ?></td> <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"> <div class="flex space-x-2"> <a href="?delete=<?= $user['id'] ?>" class="text-red-600 hover:text-red-800" title="Hapus" onclick="return confirm('Apakah Anda yakin ingin menghapus pengguna ini?')"> <i class="fas fa-trash-alt"></i> <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"> <div class="flex space-x-2"> <a href="edit_user.php?id=<?= $user['id'] ?>" class="text-blue-600 hover:text-blue-800" title="Edit"> <i class="fas fa-edit"></i> </a> <a href="?delete=<?= $user['id'] ?>" class="text-red-600 hover:text-red-800" title="Hapus" onclick="return confirm('Apakah Anda yakin ingin menghapus pengguna ini?')"> <i class="fas fa-trash-alt"></i> </a> </div> </td> </a> </div> </td> </tr> <?php endforeach; ?> <?php endif; ?> </tbody> </table> </div> </div> <script> // Auto hide notification after 3 seconds setTimeout(() => { const notification = document.querySelector('.fixed'); if (notification) { notification.style.display = 'none'; } }, 3000); </script> </body> </html>
Copyright ©2021 || Defacer Indonesia