Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to create a Login system using HTML, PHP, and MySQL

In this article, you will learn how to create an authentication system with the help of HTML, PHP, and Mysql. Most of the websites are dynamic and to get access to the restricted area user needs a registered user for that site. Therefore, they can log in to get access to members-only areas. The login authentication system is very common for any web application. It is likewise useful when we need to store data for users. It covers everything from the shopping sites, online-course sites, and membership sites, and so forth.


Creating a MySQL table

First of all, you will need to create a new database in MySQL. Please execute below SQL query inside your newly created database. It will create a new users database table.

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;

Create Signup Form

To access restricted area we will need to allow users to register a new account. We will create a PHP file with the name register.php. Please copy the below code and paste it in the register.php file.

 0) {
        $error .= '

The email address is already registered!

'; } else { // Validate password if (strlen($password ) 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) ) { $query = "INSERT INTO users (name, email, password) VALUES ('" . $fullname . "','" . $email . "','" . $password_hash . "');"; if (mysqli_query($db, $query)) { $error .= '

Your registration was successful!

'; } else { $error .= '

Something went wrong!

'; } } } // Close connection mysqli_close($db); } ?> Sign Up

Register

Please fill this form to create an account.

Already have an account? Login here.

Output for above code will look like below Image.

Registration HTML form

In the above code, we have created a simple registration HTML page. It has name, email, password, and confirm password as fields. All fields are required in registration form. We have used the default HTML5 attribute called required for validation. type=”email” validate email address format. Users can not create multiple accounts with the same email address. It will alert users that the account is already registered with an email address. We have also added some validation using the HTML default attribute. You can always use some ready-made login forms to save your time.

In the register.php file, we have included a config.php file. Now, Let’s create new config.php with the below code in it. config.php file is used to connect our application to the MySQL database. Please change the name of the database which you choose when you created the database.

You can also see that we have included the session.php file. So, let’s create a new file session.php and paste the below code in it. This code will start the session and check if the user is already logged in, if yes then the below script will redirect the user to the welcome.php file.

The register.php file will allow users to register themselves into the system. It will first check MySQL table if user is already registered, if yes then alert the user with an error message if any record found in MySQL table with the same email address.

For security reasons, you need to make sure that the password is not stored as plain text in the database. You can see that we have used password_hash() in-built PHP function. This function will create a new password hash using a strong one-way hashing algorithm.

After server-side validation, we will store all the data in the MySQL users table and alert the user with a success message.


Create Login Form

In this section, we will create a login.php file that will allow registered users to get access to a restricted area of the system. Please create a login.php file with the following code in it.

Please enter email.';
    }

    // validate if password is empty
    if (empty($password)) {
        $error .= '

Please enter your password.

'; } if (empty($error)) { $result = mysqli_query($db, "SELECT * FROM users WHERE email=" . $email); if (mysqli_num_rows($result) > 0) { $row = mysqli_fetch_array($result, MYSQLI_ASSOC); if (password_verify($password, $row['password'])) { $_SESSION["userid"] = $row['id']; $_SESSION["user"] = $row; // Redirect the user to welcome page header("location: welcome.php"); } else { $error .= '

The password is not valid.

'; } } else { $error .= '

No User exist with that email address.

'; } } // Close connection mysqli_close($db); } ?> Login

Login

Please fill in your email and password.

Don't have an account? Register here.

Below is the output for Login form.

In the above code, we have created a login form with some PHP script to confirm the user’s login credentials. On form submission, we will execute the SELECT SQL query to find the record in the database by email and password. If any record found from the database, then we will store the user’s details in session and redirect the user to the welcome.php file. If no record found, then the user alert with an error message.


Create a Welcome Page

Users will reach this page after a successful login. We will create a new welcome.php file with the following code in it. The welcome.php script will check if the user is not logged in, then it will redirect the user to login.php file.

Welcome <?php echo $_SESSION["name"]; ?>

Hello, . Welcome to demo site.

Log Out


The Logout Script

At last, we will create a logout.php file with the following code. Once the user clicks on the Log Out link, the above code will destroy all user’s session data, and redirect to the login.php file.


Bottom Line

In this article, I explained how to create an authentication system using HTML, PHP, and MySQL. If you are not aware of HTML code you can use login forms available on HTML5 website template sites. Once you understand the above tutorial, you can also add additional features like reset password, verify email, user’s profile, forgot password, etc.

The post How to create a Login system using HTML, PHP, and MySQL appeared first on .



This post first appeared on Web Design Resources, Guide, Tips, Tools For Designers To Succeed, please read the originial post: here

Share the post

How to create a Login system using HTML, PHP, and MySQL

×

Subscribe to Web Design Resources, Guide, Tips, Tools For Designers To Succeed

Get updates delivered right to your inbox!

Thank you for your subscription

×