Building a User Registration System with PHP, MYSQL Print

  • 15

Building a User Registration System with LAMP Stack

Building a User Registration System is a great way to learn LAMP (Linux, Apache, MySQL, PHP) stack development. It involves creating a system that allows users to register, log in, and log out. This process requires an understanding of session handling, form validation, and database interactions.

The system we're building will be a simple one, with the following functionalities:

  1. User Registration
  2. User Login
  3. User Logout

Step 1: Setting Up Your Environment

First, make sure your hosting environment is ready. In this case, we're using domainindia.com's Shared cPanel SSD hosting. Log in to your cPanel account and make sure the LAMP stack is correctly installed and running.

Step 2: Create the Database

To create the database, we will use the "MySQL Database Wizard" under the "DATABASES" section.

  1. Enter a name for your database and click "Next Step".
  2. Now create a database user, enter a strong password and click "Create User".
  3. On the next screen, give the user "All Privileges" and finish the wizard.

Step 3: Create the Users Table

Go to "phpMyAdmin" in cPanel, select your database, and create a table named 'users' with the following fields:

  1. id - INT(11), AUTO_INCREMENT, PRIMARY KEY
  2. username - VARCHAR(50)
  3. password - VARCHAR(255) # we're storing hashed passwords, which may be up to 255 characters long.

CREATE TABLE `users` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`username` VARCHAR(50) NOT NULL,
`password` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`)
);

Step 4: Create Registration Page

Now, we will create a registration page. The registration form will include fields for username and password. Upon submission, form data will be sent to a PHP script (register.php) which will handle the data and store it in the database.

Here's the register.php code:

<?php
require_once 'dbconfig.php';

// Form field values are stored in the superglobal $_POST array
$username = $_POST['username'];
$password = $_POST['password'];

// Hash the password for secure storage
$hashed_password = password_hash($password, PASSWORD_DEFAULT);

try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);

// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "INSERT INTO users (username, password) VALUES (:username, :password)";

$stmt = $conn->prepare($sql);

$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', $hashed_password);

$stmt->execute();

echo "Registration successful. You can now log in.";
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}

$conn = null;
?>

Note: 'dbconfig.php' is a separate file where we store our database connection parameters.

Step 5: Create Login Page

Next, we will create a login page. Here, the user will enter their username and password. Upon form submission, a PHP script (login.php) will verify the user's credentials against the data in the database.

Here's the login.php code:

<?php
require_once 'dbconfig.php';

$username = $_POST['username'];
$password = $_POST['password'];

try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);

// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $conn->prepare("SELECT * FROM users WHERE username=:username");
$stmt->bindParam(':username', $username);
$stmt->execute();

if ($stmt->rowCount() > 0) {
// Check password
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if (password_verify($password, $user['password'])) {
session_start();
$_SESSION['username'] = $username;
echo "Logged in successfully";
} else {
echo "Wrong password";
}
} else {
echo "No user with that username";
}
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}

$conn = null;
?>

Step 6: Logout

Finally, to handle logouts, you can create a logout.php script that destroys the session:

<?php
session_start();
session_destroy();
echo "Logged out successfully";
?>

This is a basic implementation of a User Registration System using the LAMP stack. As you get more comfortable with these concepts, you can begin to add more features, such as password resets, email verification, and more.

Remember to always properly sanitize and validate user input, and to handle errors and exceptions. This not only makes your application more robust but also helps prevent security issues like SQL Injection and XSS (Cross-Site Scripting).

In the `dbconfig.php` file, we will store our database connection parameters. It's good practice to keep this information in a separate file as it can be used in multiple scripts.

Here is what `dbconfig.php` might look like:


<?php
$host = 'localhost'; // Change to your hosting server hostname
$dbname = 'database_name'; // Change to your database name
$db_username = 'database_username'; // Change to your database username
$db_password = 'database_password'; // Change to your database password
?>

Now, let's create the HTML form code for both registration and login.

### Register Form (`register.html`)


<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>
<h2>Registration Form</h2>

<form action="register.php" method="post">
<label for="username">Username:</label><br>
<input type="text" id="username" name="username"><br>
<label for="password">Password:</label><br>
<input type="password" id="password" name="password"><br>
<input type="submit" value="Register">
</form>
</body>
</html>

### Login Form (`login.html`)


<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login Form</h2>

<form action="login.php" method="post">
<label for="username">Username:</label><br>
<input type="text" id="username" name="username"><br>
<label for="password">Password:</label><br>
<input type="password" id="password" name="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>

In these HTML forms, users will be able to enter their credentials. After clicking the submit button, the data will be sent to the respective PHP scripts (`register.php` and `login.php`) to process the data.

Remember to protect your PHP files from being directly accessed by users. You can do this by checking if the form has been submitted in your PHP scripts.

Please ensure to replace all the placeholders in the `dbconfig.php` file with the actual values specific to your hosting and database configuration. Also, always use secure connection methods and avoid sharing sensitive information such as your database password.


Was this answer helpful?

« Back