Sign Up

Tutorial to create a login system using HTML, PHP, and MySQL

This is a tutorial for creating a login system with the help of HTML, PHP, and MySQL. Your website needs to be dynamic and your visitors need to have instant access to it. Therefore, they want to log in as many times as possible. The login authentication system is very common for any web application. It allows registered users to access the website and members-only features. It is also helpful when we want to store information for users. It covers everything from shopping sites, educational sites, and membership sites, etc.

This tutorial is covered in 4 parts.

Table of Contents

1) Building a Signup system

In this part, We will create a signup system that allows users to create a new account to the system. Our first step is to create a HTML registration form. The form is pretty simple to create. It only asks for a name, email, password, and confirm password. Email addresses will be unique for every user. Multiple accounts for the same email address are not allowed. It will show an error message to the users who try to create multiple accounts with the same email address.

Читайте также:  Css play animation on click

Step 1: Creating Registration Form in HTML

We will create a PHP file named register.php with the following code in it. This is a simple HTML form with some basic validation. If you are not familiar with HTML then you can get it from many online sites who give ready-made html5 login form templates.

       

Register

Please fill this form to create an account.

Already have an account? Login here.

The output of the above HTML form will look like this.

Sign Up

All the input fields are required by adding the «required» attribute which is the default HTML attribute. The use of type=»email» will validate the email address provided by users and gives an error if the email address is not valid. For the registration form, we have used bootstrap for rapid development. If you want to save your time on HTML code you can always use some free html5 templates for your project.

Step 2: Creating the MySQL Database Table

You will need to create a new database with any suitable name you want. After that please execute the below SQL query to create the user’s table inside your newly created MySQL database.

CREATE TABLE `users` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(75) NOT NULL, `password` varchar(255) NOT NULL, `email` varchar(100) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `email` (`email`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;

Step 3: Creating Database Configuration File

Now, we have created the users table. Let’s create a new PHP file named config.php to connect with the MySQL database. Paste the following code in the config.php file and change the database name to whatever you choose while creating the database.

Step 4: Creating a Session File

Let’s create a file named session.php. In this file, we will start the session and check if a user is already logged in, if yes then we will redirect the user to welcome.php file.

Step 5: Create Registration Form in PHP

Finally, it’s time to create a PHP code that allows users to register their accounts into the system. This PHP code will alert users with an error if any user is already registered with the same email address.

Replace the following code in the register.php file.

prepare("SELECT * FROM users WHERE email = ?")) < $error = ''; // Bind parameters (s = string, i = int, b = blob, etc), in our case the username is a string so we use "s" $query->bind_param('s', $email); $query->execute(); // Store the result so we can check if the account exists in the database. $query->store_result(); if ($query->num_rows > 0) < $error .= '

The email address is already registered!

'; > else < // Validate password if (strlen($password ) < 6) < $error .= '

Password must have atleast 6 characters.

'; > // Validate confirm password if (empty($confirm_password)) < $error .= '

Please enter confirm password.

'; > else < if (empty($error) && ($password != $confirm_password)) < $error .= '

Password did not match.

'; > > if (empty($error) ) < $insertQuery = $db->prepare("INSERT INTO users (name, email, password) VALUES (?, ?, ?);"); $insertQuery->bind_param("sss", $fullname, $email, $password_hash); $result = $insertQuery->execute(); if ($result) < $error .= '

Your registration was successful!

'; > else < $error .= '

Something went wrong!

'; > > > > $query->close(); $insertQuery->close(); // Close DB connection mysqli_close($db); > ?>

Register

Please fill this form to create an account.

Already have an account? Login here.

Once user click on submit button it will check if $_SERVER[«REQUEST_METHOD»] == «POST» and $_POST[‘submit’] variable has been set. For security concerns, we always suggest not to store the password as plain text in the database. We have used password_hash() function which creates a new password hash using a strong one-way hashing algorithm.

The above PHP script will validate that no user is registered with the same email address and also validate password. After validation is confirmed we store the user-provided information in the users’ table and alert the user that registration was successful.

2) Building a Login System

In this part, we will create a login form to allow users to access the restricted area of the system. In our case, the restricted area is a welcome page which we will cover in the next part.

Step 1: Creating a Login Form in HTML

Below is the Login Form in HTML. Paste it in a file named login.php

       

Login

Please fill in your email and password.

Don't have an account? Register here.

The output of the above code will look like this

Login

Step 2: Creating a Login System in PHP

After creating the login form in HTML, we will write a code to validate login credentials. On form submit we will check that the email and password are filled. If they filled then we will execute a SELECT query to find the record in a database on the basis of email and password. If any record found, then we will store the «userID» in session and the user is redirected to the welcome.php file, otherwise, the user is alerted with an error message.

Let’s replace the following code in the login.php file.

Please enter email.

'; > // validate if password is empty if (empty($password)) < $error .= '

Please enter your password.

'; > if (empty($error)) < if($query = $db->prepare("SELECT * FROM users WHERE email = ?")) < $query->bind_param('s', $email); $query->execute(); $row = $query->fetch(); if ($row) < if (password_verify($password, $row['password'])) < $_SESSION["userid"] = $row['id']; $_SESSION["user"] = $row; // Redirect the user to welcome page header("location: welcome.php"); exit; >else < $error .= '

The password is not valid.

'; > > else < $error .= '

No User exist with that email address.

'; > > $query->close(); > // Close connection mysqli_close($db); > ?>

Login

Please fill in your email and password.

Don't have an account? Register here.

3) Creating a Welcome Page

Below is the code for the welcome.php file. Users will be redirected to this page after a successful login process. We have added some code at the top of the page to check if the user is not logged in, then redirect the user to the login page.

Let’s create a welcome.php file and paste the following code in it.

 ?>    Welcome   

Hello, . Welcome to demo site.

Log Out

4) The Logout script

Finally, Let’s create a logout.php file with the following code in it.

Once the user clicks on the Log Out link, the above script, will be called to destroy the session and redirect user to the login.php file.

Conclusion

In this tutorial, I explained how you can create a Login System using HTML, PHP and MySQL. Once you understand how simple it is to create a login system you can add other features like reset password, forgot password, verify email address, edit user’s profile, etc.

Источник

Create a Registration and Login System with PHP and MySQL

Create a Registration and Login System with PHP and MySQL

How to create a Registration and Login System with PHP and MySQL. Here is the quick solution to build a login system with PHP and MySQL. Nowadays almost every website provides Registration and login functionality. Thus, it is necessary to add a login system in modern web applications.

In this tutorial, we walk through the complete process of creating a user registration system. Users can create an account by providing username, password, email. After the account was created, the user can log in to their own account. Once the user login, it will redirect to the Dashboard page. Moreover, the user can logout from his panel. This whole system we are developed using PHP and MySQL.

Furthermore, we will show you how to build secure pages that are only accessed by logged in users. Without login, the user can not access the page.

How to create a Registration and Login System with PHP and MySQL

Here are Seven pretty simple steps you have to follow to create a login system.

  1. Create a Database and Database Table
  2. Connect to the Database
  3. Session Create for Logged in User
  4. Create a Registration and Login Form
  5. Make a Dashboard Page
  6. Create a Logout (Destroy session)
  7. CSS File Create

Create a Database and Database Table

First, you have to log in to PHPMyAdmin. Next, click on the Database tab to create a new database. Enter your database name and click on create database button. As soon as PHPMyAdmin will create a new database.

Create new database in MySQL - Registeration and login system

Similarly, you can execute the below query to create a database.

CREATE DATABASE LoginSystem;

Once you create a database, the second step to creating a user table. The user’s table will have the following fields.

  • id – int(11)
  • username – varchar(100)
  • email – varchar(100)
  • password – varchar(100)
  • create_datetime – datetime
CREATE TABLE IF NOT EXISTS `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) NOT NULL, `email` varchar(50) NOT NULL, `password` varchar(50) NOT NULL, `create_datetime` datetime NOT NULL, PRIMARY KEY (`id`) );

Copy the above query and execute it in the SQL query area.

Create user table in MySQL database - Registeration and login system

Your database table looks like the below screen.

User table structure

Connect to the Database

After creating the table, we have to create a PHP MySQL connector script to connect to the MySQL database server. Create a file named db.php and put the following code inside it.

db.php

Session Create for Logged in User

Next, we have to create a session for the user. Create a file named auth_session.php and paste the codes below.

auth_session.php

Creating a Registration Form

Furthermore, create PHP file registration.php and paste the following example code in it. This will create an HTML form. It will allow users to register.

registration.php

       

You are registered successfully.


Click here to Login

"; > else < echo "

Required fields are missing.


Click here to registration again.

"; > > else < ?>

Registration

Click to Login

?>

The output of the above code will look like this.

Registration form - Registration and Login System

Creating a Login Form

Similarly, create a PHP file login.php and put the following example code in it. This file code contains a form that allows users to enter username and password.

login.php

       else < echo "

Incorrect Username/password.


Click here to Login again.

"; > > else < ?>

Login

New Registration

?>

Similarly, the output of the above code will look like this.

Login form

Making a Dashboard Page

Once user login we will redirect to the user dashboard page. Create a PHP file named dashboard.php and paste the below code in it.

dashboard.php
        

Hey, !

You are now user dashboard page.

Logout

After the user login, you will see the following user dashboard screen.

User dashboard screen - Registration and Login System

Similarly, you can create another secure page. Only you need to add the below code first in your PHP file.

Create a Logout (Destroy session)

When clicking on the logout button we have to destroy user sessions. It will redirect to the login page. Thus, create a file named logout.php and add the below code.

logout.php

CSS File Create

Finally, important step for a user experience perspective. Create CSS file style.css and put the below code.

style.php
body < background: #3e4144; >.form < margin: 50px auto; width: 300px; padding: 30px 25px; background: white; >h1.login-title < color: #666; margin: 0px auto 25px; font-size: 25px; font-weight: 300; text-align: center; >.login-input < font-size: 15px; border: 1px solid #ccc; padding: 10px; margin-bottom: 25px; height: 25px; width: calc(100% - 23px); >.login-input:focus < border-color:#6e8095; outline: none; >.login-button < color: #fff; background: #55a1ff; border: 0; outline: 0; width: 100%; height: 50px; font-size: 16px; text-align: center; cursor: pointer; >.link < color: #666; font-size: 15px; text-align: center; margin-bottom: 0px; >.link a < color: #666; >h3

Finally, our login system is ready to use. You can download the source code from the below download link.

Final Thoughts

That all you need to know how to create registration and login system in PHP with MySQL database. Furthermore, we create a registration and login form. Moreover, we create a MySQL connection file to establish a database connection. Thus we create a complete login system with interactive design.

We hope you have found this article helpful. Let us know your questions or feedback if any through the comment section in below. You can subscribe our newsletter and get notified when we publish new WordPress articles for free. Moreover, you can explore here other JavaScript related articles.

Источник

Оцените статью