WEB_PROJECT_TA/register.php

154 lines
4.8 KiB
PHP

<?php
session_start();
require 'config/database.php';
// Buat koneksi database
$database = new Database();
$conn = $database->getConnection();
$success = "";
$error = "";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = trim($_POST['username']);
$password_plain = trim($_POST['password']);
$role = $_POST['role'];
// Validasi sederhana
if (empty($username) || empty($password_plain) || empty($role)) {
$error = "Semua field wajib diisi.";
} else {
$password = password_hash($password_plain, PASSWORD_DEFAULT);
try {
$stmt = $conn->prepare("INSERT INTO users (username, password, role) VALUES (?, ?, ?)");
$stmt->execute([$username, $password, $role]);
$success = "Pendaftaran berhasil! Silakan login.";
} catch (PDOException $e) {
if ($e->getCode() == 23000) { // duplicate entry
$error = "Username sudah digunakan.";
} else {
$error = "Terjadi kesalahan: " . $e->getMessage();
}
}
}
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register - FTTH Megadata</title>
<style>
body {
margin: 0;
font-family: Arial, sans-serif;
height: 100vh;
background: url('bg.jpg') no-repeat center center fixed;
background-size: cover;
display: flex;
align-items: center;
justify-content: center;
padding: 15px;
}
.register-container {
background: rgba(255, 255, 255, 0.9);
padding: 40px;
border-radius: 15px;
box-shadow: 0 0 15px rgba(0,0,0,0.2);
text-align: center;
width: 320px;
max-width: 100%;
}
.register-container img {
max-width: 150px;
height: auto;
object-fit: contain;
margin-bottom: 15px;
}
.register-container h2 {
margin-bottom: 20px;
color: #333;
}
.register-container input,
.register-container select {
width: 100%;
padding: 12px;
margin: 8px 0;
border: none;
border-radius: 30px;
background: #f1f1f1;
outline: none;
font-size: 14px;
}
.register-container button {
width: 100%;
padding: 12px;
border: none;
border-radius: 30px;
background: #333;
color: #fff;
font-size: 14px;
cursor: pointer;
}
.register-container button:hover {
background: #555;
}
.register-container p {
font-size: 12px;
margin-top: 15px;
}
.register-container a {
color: #0066cc;
text-decoration: none;
}
.success {
color: green;
margin-bottom: 10px;
}
.error {
color: red;
margin-bottom: 10px;
}
@media (max-width: 480px) {
.register-container {
padding: 25px;
}
.register-container img {
max-width: 100px;
}
}
</style>
</head>
<body>
<div class="register-container">
<img src="megadata.jpeg" alt="Logo">
<h2>Daftar Akun</h2>
<?php if (!empty($success)) echo "<p class='success'>$success</p>"; ?>
<?php if (!empty($error)) echo "<p class='error'>$error</p>"; ?>
<form method="POST">
<input type="text" name="username" placeholder="Username" required>
<input type="password" name="password" placeholder="Password" required>
<select name="role" required>
<option value="">Pilih Role</option>
<option value="engineer">Superadmin</option>
<option value="teknisi1">Site Bungatan</option>
<option value="teknisi2">Site Besuki</option>
<option value="teknisi3">Site jatisari</option>
<option value="teknisi4">Site Kendit</option>
<option value="teknisi5">Site Gending</option>
<option value="teknisi6">Site Bangkalan</option>
<option value="teknisi7">Site Bondowoso</option>
<option value="teknisi8">Site Arjasa</option>
<option value="teknisi9">Site Lamongan</option>
<option value="teknisi10">Site Asembagus</option>
</select>
<button type="submit">Daftar</button>
</form>
<p>Sudah punya akun? <a href="login.php">Login di sini</a></p>
</div>
</body>
</html>