Complete User Registration System using PHP and MySQL database | Coderanks
29/03/2019 . 4 minutes, 57 seconds to read . Posted by Admin#htmlemail #codeigniteremail #mysql #ViewsinCodeigniter #RoutinginCodeigniter #phpmyadmin #Laravel #WebDevelopment
User Registration System using PHP and MySQL database (Part 2)
In this Article, we'll walk through the complete process of creating a user registration system where users can create an account by providing a username, an email and password in md5, login page and log out page using PHP and MySQL. We will also learn how a user creates Database, Connection with Database, Insertion, Selection, Selection with Comparison and Forms with Post method and Action.
Requirements:
- Xampp or Wamp Server Installed
- Code Editor(VS Code)
Part-2:-
When a user is registered in the database he'll redirect/direct to the Login page (login.php)
Let's look at the user login page.
Login user:
Logged in is simple and easy as compare to register.php. Just open the login page and writes this code in the login page (login.php) file:
login.php
<?php include('db.php') ?>
<!DOCTYPE html>
<html>
<head>
<title>Registration in PHP and MySQL</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="header">
<h2>Sign In</h2>
</div>
<form method="post" action="login.php">
<?php include('error.php'); ?>
<div class="input-group">
<label>Username</label>
<input type="text" name="username" placeholder="User Name" required="required">
</div>
<div class="input-group">
<label>Password</label>
<input type="password" name="password" placeholder="Password" required="required">
</div>
<div class="input-group">
<button type="submit" class="btn" name="userLogin">Login</button>
</div>
<div>
Not have an account?
<a class="right" href="register.php">Register</a>
</div>
</form>
</body>
</html>
Everything on this page is almost similar to the register.php page.
the page will look like:
Now enter the User Name and Password of the user which one is already registered. Let's use the username admin and password is 123. Insert this to the form and it looks similar to this:
Now the code that logs the user in is to be written in the same db.php file. So open the db.php file and add this code at the bottom of the file. The full file db.php is:
db.php
<?php
// connect to the database
$server="localhost";
$username="root";
$password="";
$database_name="registration";
$errors = array();
$con = mysqli_connect($server, $username, $password, $database_name);
if($con){
if (isset($_POST['registerNewUser'])) {
// receive all input values from the register.php form
$username = mysqli_real_escape_string($con, $_POST['username']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$confirm_password = mysqli_real_escape_string($con, $_POST['confirm_password']);
// by using array_push() corresponding errors in $errors() which is an array of errors.
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($email)) {
array_push($errors, "Email is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
if ($password != $confirm_password) {
array_push($errors, "Failed to Match");
}
//fistly check in database that a user does not already exist with the same username and/or email.
$get_all = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
$result = mysqli_query($con, $get_all);
$user = mysqli_fetch_assoc($result);
if ($user) { // if user exists
if ($user['username'] === $username) {
array_push($errors, "Username is already existed");
}
if ($user['email'] === $email) {
array_push($errors, "email is already existed");
}
}
// Finally, register user if no error
if (count($errors) == 0) {
$pwd = md5($password);//encrypt the password before inserting in the database
$register = "INSERT INTO users (username, email, password)
VALUES('$username', '$email', '$pwd')";
mysqli_query($con, $register);
header('Location: login.php');
}
}
if (isset($_POST['userLogin'])) {
$username = mysqli_real_escape_string($con, $_POST['username']);
$password = mysqli_real_escape_string($con, $_POST['password']);
if (empty($username)) {
array_push($errors, "Username is required");
}
else if (empty($password)) {
array_push($errors, "Password is required");
}
else{
$password = md5($password);
$login = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$results = mysqli_query($con, $login);
if (mysqli_num_rows($results) == 1) {
header('Location: index.php');
}else {
array_push($errors, "Invalid username/password! Please try Again");
}
}
}
echo "Welcome to Registration Article";
}?>
Again all it'd check if the user has filled the form with no empty field, verifies that their credentials match the data retrieved from the database and logs them in if it does. After logging in, the user is redirected/directed to the index page (index.php) file.
Now let's see what happens in the index file. Open it up and write the following code in it:
index.php
<html>
<head>
<title>Index</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="header">
<h2>Home</h2>
div>
<div class="content">
<h2>Welcome to the Index Page</h2>
<h4>You've Successfully Completed the Registration Article</h4>
<p>Subscribe us for More at<a href="https://www.coderanks.com/">Coderanks.com</a></p>
</div>
</body>
</html>
After login the index file shows like this, called as Home Page or Landing Page:
Important Points:
WAMPP or XAMPP is required for localhost to run the code.
First, run localhost server and for the register.php file, the URL is localhoast/registration/register.php
Use mysqli_real_escape_string() to avoid possible string errors.
Carefully write/handle PHP in HTML.
The error.php file is a guide for errors only.
Subscribe us for more on Coderanks.com
Best Regards