In this tutorial, you will learn how to build a full user registration and authentication system with PHP and MySQL. Users will be able to create an account by providing their username, email, and password. Additionally, they can log in, log out, and access pages only after logging in.
Step 1: Set Up Your Database
Start by setting up your database. You need to create a MySQL database named registration
and a table named users
to store user information.
1 2 3 4 5 6 7 8 9 10 11 12 | CREATE DATABASE registration; USE registration; CREATE TABLE users ( id INT(11) AUTO_INCREMENT PRIMARY KEY, username VARCHAR(100) NOT NULL, email VARCHAR(100) NOT NULL, password VARCHAR(100) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; |
You can execute this SQL query using a MySQL client like phpMyAdmin, or directly in your MySQL prompt.
Step 2: Set Up Your Project Folder
- Create a folder called
registration
inside thehtdocs
(for XAMPP users) orwww
(for WAMP users). - Inside this folder, create the following files:
register.php
login.php
server.php
index.php
style.css
errors.php
Step 3: Design the Registration Form (register.php)
Open register.php
and add the following code for the user registration form:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <?php include('server.php') ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Registration System</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="header"> <h2>Register</h2> </div> <form method="post" action="register.php"> <?php include('errors.php'); ?> <div class="input-group"> <label>Username</label> <input type="text" name="username" value="<?php echo $username; ?>"> </div> <div class="input-group"> <label>Email</label> <input type="email" name="email" value="<?php echo $email; ?>"> </div> <div class="input-group"> <label>Password</label> <input type="password" name="password_1"> </div> <div class="input-group"> <label>Confirm Password</label> <input type="password" name="password_2"> </div> <div class="input-group"> <button type="submit" class="btn" name="reg_user">Register</button> </div> <p>Already a member? <a href="login.php">Sign in</a></p> </form> </body> </html> |
In this form, users provide their username, email, and password. The form will submit to itself (register.php
), and the logic for handling form data is in the server.php
file.
Step 4: Style the Form (style.css)
For a clean design, add the following CSS in style.css
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | * { margin: 0; padding: 0; } body { font-size: 16px; background: #f4f4f4; } .header { width: 30%; margin: 50px auto; padding: 20px; text-align: center; background: #4CAF50; color: white; border-radius: 10px 10px 0 0; } form, .content { width: 30%; margin: 0 auto; padding: 20px; background: white; border: 1px solid #B0C4DE; border-radius: 0 0 10px 10px; } .input-group { margin: 10px 0; } .input-group label { display: block; margin: 5px 0; } .input-group input { width: 100%; padding: 10px; font-size: 16px; border-radius: 5px; border: 1px solid #ccc; } .btn { width: 100%; padding: 10px; background: #4CAF50; color: white; border: none; border-radius: 5px; font-size: 16px; } .error { background: #f2dede; color: #a94442; border: 1px solid #a94442; padding: 10px; margin-bottom: 20px; border-radius: 5px; } .success { background: #dff0d8; color: #3c763d; border: 1px solid #3c763d; padding: 10px; margin-bottom: 20px; border-radius: 5px; } |
Step 5: Handle Registration Logic (server.php)
In server.php
, add the following PHP code to handle user registration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | <?php session_start(); // Initialize variables $username = ""; $email = ""; $errors = array(); // Connect to the database $db = mysqli_connect('localhost', 'root', '', 'registration'); // Register user if (isset($_POST['reg_user'])) { // Receive form input values $username = mysqli_real_escape_string($db, $_POST['username']); $email = mysqli_real_escape_string($db, $_POST['email']); $password_1 = mysqli_real_escape_string($db, $_POST['password_1']); $password_2 = mysqli_real_escape_string($db, $_POST['password_2']); // Validate the form if (empty($username)) { array_push($errors, "Username is required"); } if (empty($email)) { array_push($errors, "Email is required"); } if (empty($password_1)) { array_push($errors, "Password is required"); } if ($password_1 != $password_2) { array_push($errors, "Passwords do not match"); } // Check if username or email already exists $user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1"; $result = mysqli_query($db, $user_check_query); $user = mysqli_fetch_assoc($result); if ($user) { if ($user['username'] === $username) { array_push($errors, "Username already exists"); } if ($user['email'] === $email) { array_push($errors, "Email already exists"); } } // If no errors, register the user if (count($errors) == 0) { $password = md5($password_1); // Encrypt the password $query = "INSERT INTO users (username, email, password) VALUES('$username', '$email', '$password')"; mysqli_query($db, $query); $_SESSION['username'] = $username; $_SESSION['success'] = "You are now logged in"; header('location: index.php'); } } ?> |
Step 6: Display Error Messages (errors.php)
Create an errors.php
file to display error messages:
1 2 3 4 5 6 7 8 9 | <?php if (count($errors) > 0) : ?> <div class="error"> <?php foreach ($errors as $error) : ?> <p><?php echo $error ?></p> <?php endforeach ?> </div> <?php endif ?> |
Step 7: Create the Login Form (login.php)
Create a simple login form in login.php
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?php include('server.php') ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Login</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="header"> <h2>Login</h2> </div> <form method="post" action="login.php"> <?php include('errors.php'); ?> <div class="input-group"> <label>Username</label> <input type="text" name="username"> </div> <div class="input-group"> <label>Password</label> <input type="password" name="password"> </div> <div class="input-group"> <button type="submit" class="btn" name="login_user">Login</button> </div> <p>Not yet a member? <a href="register.php">Sign up</a></p> </form> </body> </html> |
Step 8: Handle Login Logic (server.php)
In server.php
, add the following logic to handle user login:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | // Login user if (isset($_POST['login_user'])) { $username = mysqli_real_escape_string($db, $_POST['username']); $password = mysqli_real_escape_string($db, $_POST['password']); // Validate form data if (empty($username)) { array_push($errors, "Username is required"); } if (empty($password)) { array_push($errors, "Password is required"); } // If no errors, check credentials if (count($errors) == 0) { $password = md5($password); // Encrypt password $query = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $results = mysqli_query($db, $query); if (mysqli_num_rows($results) == 1) { $_SESSION['username'] = $username; $_SESSION['success'] = "You are now logged in"; header('location: index.php'); } else { array_push($errors, "Wrong username/password combination"); } } } |
Step 9: Create the Home Page (index.php)
Create the home page (index.php
) that will only be accessible after logging in:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <?php session_start(); if (!isset($_SESSION['username'])) { $_SESSION['msg'] = "You must log in first"; header('location: login.php'); } if (isset($_GET['logout'])) { session_destroy(); unset($_SESSION['username']); header("location: login.php"); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Home</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="header"> <h2>Home Page</h2> </div> <div class="content"> <?php if (isset($_SESSION['success'])) : ?> <div class="success"> <h3> <?php echo $_SESSION['success']; unset($_SESSION['success']); ?> </h3> </div> <?php endif ?> <?php if (isset($_SESSION['username'])) : ?> <p>Welcome <strong><?php echo $_SESSION['username']; ?></strong></p> <p><a href="index.php?logout='1'" style="color: red;">Logout</a></p> <?php endif ?> </div> </body> </html> |
Conclusion
By following these steps, you’ve successfully created a user registration and login system with PHP and MySQL. You can expand this system by adding features like password reset, user profile management, and security improvements like prepared statements and CSRF protection.
Now you have a fully functional user authentication system!
Source:
https://codewithawa.com/posts/complete-user-registration-system-using-php-and-mysql-database
Low Deposit Match up to our record of the bonus code