93 lines
3.4 KiB
PHP
93 lines
3.4 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
$error = '';
|
|
$success = '';
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$name = trim($_POST['name']);
|
|
$email = trim($_POST['email']);
|
|
$password = trim($_POST['password']);
|
|
$role = 'Wajib Pajak'; // Default role for new registrations
|
|
|
|
if (empty($name) || empty($email) || empty($password)) {
|
|
$error = 'Semua field harus diisi.';
|
|
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$error = 'Format email tidak valid.';
|
|
} else {
|
|
try {
|
|
$pdo = db();
|
|
|
|
// Check if email already exists
|
|
$stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?");
|
|
$stmt->execute([$email]);
|
|
if ($stmt->fetch()) {
|
|
$error = 'Email sudah terdaftar.';
|
|
} else {
|
|
// Hash the password
|
|
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
|
|
|
|
// Insert new user
|
|
$stmt = $pdo->prepare("INSERT INTO users (name, email, password, role) VALUES (?, ?, ?, ?)");
|
|
if ($stmt->execute([$name, $email, $hashed_password, $role])) {
|
|
$_SESSION['success_message'] = "Registrasi berhasil! Silakan login.";
|
|
header("Location: login.php");
|
|
exit;
|
|
} else {
|
|
$error = 'Terjadi kesalahan. Gagal mendaftar.';
|
|
}
|
|
}
|
|
} catch (PDOException $e) {
|
|
// In production, you should log this error instead of showing it to the user.
|
|
$error = "Database error: " . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="id">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Registrasi - Si-Apon</title>
|
|
<link href="https://fonts.googleapis.com/css2?family=Merriweather:wght@700&family=Lato:wght@400;700&display=swap" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
</head>
|
|
<body>
|
|
<div class="login-container">
|
|
<div class="login-box">
|
|
<div class="login-header">
|
|
<h2>Registrasi Akun</h2>
|
|
<p>Buat akun baru untuk memulai.</p>
|
|
</div>
|
|
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
|
|
<?php endif; ?>
|
|
|
|
<form action="register.php" method="post" class="login-form">
|
|
<div class="form-group">
|
|
<label for="name">Nama Lengkap</label>
|
|
<input type="text" id="name" name="name" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="email">Email</label>
|
|
<input type="email" id="email" name="email" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="password">Password</label>
|
|
<input type="password" id="password" name="password" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<button type="submit" class="btn-login">Daftar</button>
|
|
</div>
|
|
</form>
|
|
<div class="login-footer">
|
|
<p>Sudah punya akun? <a href="login.php">Login di sini</a></p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|