Belajar PHP untuk PEMULA | 16. REGISTRASI

3 min read 4 hours ago
Published on Oct 23, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, we will learn how to implement a user registration system in PHP. This feature is essential for allowing users to create accounts before they can log in to your application. This guide is suitable for beginners looking to enhance their PHP skills and build a foundational understanding of user authentication.

Step 1: Set Up Your Database

To store user information, we need to create a database and a table for users.

  1. Create Database:

    • Use a database management tool like phpMyAdmin.
    • Create a new database named user_registration.
  2. Create Users Table:

    • Execute the following SQL command to create a table:
      CREATE TABLE users (
          id INT(11) AUTO_INCREMENT PRIMARY KEY,
          username VARCHAR(50) NOT NULL,
          password VARCHAR(255) NOT NULL,
          email VARCHAR(100) NOT NULL,
          created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
      );
      

Step 2: Create Registration Form

Next, we will create a simple HTML form for user registration.

  1. Create HTML Form:
    • In your project directory, create a file named register.php.
    • Add the following HTML code for the form:
      <form action="register.php" method="POST">
          <label for="username">Username:</label>
          <input type="text" id="username" name="username" required>
          
          <label for="email">Email:</label>
          <input type="email" id="email" name="email" required>
          
          <label for="password">Password:</label>
          <input type="password" id="password" name="password" required>
          
          <button type="submit">Register</button>
      </form>
      

Step 3: Handle Form Submission

Now, we will process the form data when the user submits the registration form.

  1. Process Registration:
    • At the top of your register.php, add the following PHP code to handle form submission:
      <?php
      if ($_SERVER["REQUEST_METHOD"] == "POST") {
          $username = $_POST['username'];
          $email = $_POST['email'];
          $password = password_hash($_POST['password'], PASSWORD_DEFAULT); // Hash the password
      
          // Database connection
          $conn = new mysqli('localhost', 'username', 'password', 'user_registration');
      
          // Check connection
          if ($conn->connect_error) {
              die("Connection failed: " . $conn->connect_error);
          }
      
          // Insert user data into the database
          $sql = "INSERT INTO users (username, email, password) VALUES ('$username', '$email', '$password')";
          if ($conn->query($sql) === TRUE) {
              echo "Registration successful!";
          } else {
              echo "Error: " . $sql . "<br>" . $conn->error;
          }
      
          $conn->close();
      }
      ?>
      

Step 4: Validate User Input

To enhance security and data integrity, we should validate user input.

  1. Input Validation:

    • Before inserting data into the database, you can validate the input:
      $username = filter_var($username, FILTER_SANITIZE_STRING);
      $email = filter_var($email, FILTER_VALIDATE_EMAIL);
      
  2. Check for Duplicate Users:

    • Before inserting, check if the username or email already exists:
      $checkUser = "SELECT * FROM users WHERE username='$username' OR email='$email'";
      $result = $conn->query($checkUser);
      if ($result->num_rows > 0) {
          echo "Username or email already exists.";
      }
      

Conclusion

In this tutorial, we covered the essential steps to create a user registration system in PHP, including setting up a database, creating a registration form, handling form submissions, and validating user input.

These foundational skills are crucial for building secure web applications. Next steps could involve implementing a login system or enhancing security features such as email verification. Happy coding!