Project Web 01 Kode Register.php

 <?php

// register.php


session_start(); // Mulai session jika belum dimulai


// Redirect jika sudah login

if (isset($_SESSION['user_id'])) {

    header("index.php");

    exit();

}


// Sertakan file koneksi database

require_once 'conect.php';


$message = ""; // Untuk menampilkan pesan error/sukses


if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $username = $_POST['username'];

    $email = $_POST['email'];

    $password = $_POST['password'];

    $confirm_password = $_POST['confirm_password'];


    // Validasi sederhana

    if (empty($username) || empty($email) || empty($password) || empty($confirm_password)) {

        $message = "<p style='color:red;'>Semua field wajib diisi!</p>";

    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

        $message = "<p style='color:red;'>Format email tidak valid!</p>";

    } elseif ($password !== $confirm_password) {

        $message = "<p style='color:red;'>Password tidak cocok!</p>";

    } else {

        // Cek apakah username atau email sudah terdaftar

        $stmt = $conn->prepare("SELECT id FROM users WHERE username = ? OR email = ?");

        $stmt->bind_param("ss", $username, $email);

        $stmt->execute();

        $stmt->store_result();


        if ($stmt->num_rows > 0) {

            $message = "<p style='color:red;'>Username atau email sudah terdaftar!</p>";

        } else {

            // Hash password sebelum disimpan

            $hashed_password = password_hash($password, PASSWORD_DEFAULT);


            // Masukkan data pengguna baru

            $stmt = $conn->prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)");

            $stmt->bind_param("sss", $username, $email, $hashed_password);


            if ($stmt->execute()) {

                $message = "<p style='color:green;'>Registrasi berhasil! Silakan <a href='login.php'>login</a>.</p>";

            } else {

                $message = "<p style='color:red;'>Registrasi gagal. Silakan coba lagi.</p>";

            }

        }

        $stmt->close();

    }

}

?>


<!DOCTYPE html>

<html lang="id">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Registrasi Pengguna</title>

    <link rel="stylesheet" href="style.css"> </head>

<body>

    <div class="container">

        <h2>Registrasi Pengguna Baru</h2>

        <?php echo $message; ?>

        <form action="register.php" method="POST">

            <label for="username">Username:</label><br>

            <input type="text" id="username" name="username" required><br><br>


            <label for="email">Email:</label><br>

            <input type="email" id="email" name="email" required><br><br>


            <label for="password">Password:</label><br>

            <input type="password" id="password" name="password" required><br><br>


            <label for="confirm_password">Konfirmasi Password:</label><br>

            <input type="password" id="confirm_password" name="confirm_password" required><br><br>


            <button type="submit">Daftar</button>

        </form>

        <p>Sudah punya akun? <a href="login.php">Login di sini</a>.</p>

    </div>

</body>

</html>

Komentar

Postingan populer dari blog ini

Project Web 01 Kode Login.php

Project Web 01 Kode Conect.php