96 lines
3.5 KiB
PHP
96 lines
3.5 KiB
PHP
<?php
|
|
ini_set('display_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
include 'db.php';
|
|
|
|
session_start();
|
|
|
|
$err = '';
|
|
$success = '';
|
|
$username_input = '';
|
|
|
|
if (isset($_POST['register'])) {
|
|
$username_input = trim($_POST['username'] ?? '');
|
|
$pass = $_POST['password'] ?? '';
|
|
$konfirmasi_pass = $_POST['konfirmasi_password'] ?? '';
|
|
|
|
if ($username_input === '' || $pass === '' || $konfirmasi_pass === '') {
|
|
$err = "Semua kolom wajib diisi.";
|
|
}
|
|
elseif ($pass !== $konfirmasi_pass) {
|
|
$err = "Konfirmasi password tidak cocok.";
|
|
}
|
|
elseif (strlen($pass) < 6) {
|
|
$err = "Password terlalu pendek. Minimal 6 karakter.";
|
|
}
|
|
else {
|
|
try {
|
|
$check = $conn->prepare("SELECT id FROM users WHERE username = ?");
|
|
$check->execute([$username_input]);
|
|
|
|
if($check->rowCount() > 0){
|
|
$err = "Username sudah terdaftar, silakan pilih nama lain.";
|
|
} else {
|
|
$hashed_pass = password_hash($pass, PASSWORD_DEFAULT);
|
|
|
|
$stmt = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
|
|
$stmt->execute([$username_input, $hashed_pass]);
|
|
|
|
$success = "Registrasi berhasil! Silakan <a href='login.php'>Login disini</a>.";
|
|
|
|
$username_input = '';
|
|
}
|
|
} catch (PDOException $e) {
|
|
$err = "Terjadi kesalahan sistem: " . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<html>
|
|
<head>
|
|
<title>Daftar Akun Baru</title>
|
|
<style>
|
|
body { font-family: Arial; background:#eef2f7; display:flex; height:100vh; justify-content:center; align-items:center; margin:0; }
|
|
.card { width:350px; background:white; padding:20px; border-radius:10px; box-shadow:0 6px 20px rgba(0,0,0,0.1); }
|
|
.input { width:100%; padding:10px; margin:8px 0; border:1px solid #ccc; border-radius:8px; box-sizing: border-box; }
|
|
.btn { width:100%; padding:12px; background:#28a745; color:white; border:none; border-radius:8px; cursor:pointer; font-weight: bold;}
|
|
.btn:hover { background:#218838; }
|
|
.err { color:#721c24; margin-bottom:10px; text-align:center; background: #f8d7da; padding: 10px; border-radius: 5px; border: 1px solid #f5c6cb; font-size: 14px;}
|
|
.success { color:#155724; margin-bottom:10px; text-align:center; background: #d4edda; padding: 10px; border-radius: 5px; border: 1px solid #c3e6cb;}
|
|
.link { text-align:center; margin-top:10px; font-size: 14px; color: #666; }
|
|
a { text-decoration: none; color: #007bff; }
|
|
h2 { text-align: center; margin-top: 0; color: #333; }
|
|
p { text-align:center; color:gray; font-size: 14px; margin-bottom: 20px; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<div class="card">
|
|
<h2>Register</h2>
|
|
|
|
<?php if ($err): ?>
|
|
<div class="err"><?= htmlspecialchars($err) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($success): ?>
|
|
<div class="success"><?= $success ?></div>
|
|
<?php endif; ?>
|
|
|
|
<form method="POST" action="">
|
|
<input class="input" type="text" name="username" placeholder="Username" value="<?= htmlspecialchars($username_input) ?>" required autocomplete="off">
|
|
|
|
<input class="input" type="password" name="password" placeholder="Password (Min 6 huruf)" required minlength="6">
|
|
|
|
<input class="input" type="password" name="konfirmasi_password" placeholder="Ulangi Password" required>
|
|
|
|
<button type="submit" name="register" class="btn">Daftar Sekarang</button>
|
|
</form>
|
|
|
|
<div class="link">
|
|
Sudah punya akun? <a href="login.php">Login disini</a>
|
|
</div>
|
|
</div>
|
|
|
|
</body>
|
|
</html>
|