Table of Contents
Tutorial: How to Create a Login Form with Validation in PHP and MySQL with Source Code
About How to Create a Login Form with Validation in PHP/MySQLi
In this tutorial will show you How to Create a Login Form with Validation in PHP/MySQLi. This tutorial does not include a good design but will give you an idea on how to create a simple Login using PHP/MySQLi.
Creating a Database
First, we’re going to create a database that contains the user data.
1. Open phpMyAdmin.
2. Click databases, create a database and name it as “login”.
3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.
CREATE TABLE `user` ( `userid` INT(11) NOT NULL AUTO_INCREMENT, `username` VARCHAR(30) NOT NULL, `password` VARCHAR(30) NOT NULL, `fullname` VARCHAR(60) NOT NULL, PRIMARY KEY (`userid`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Inserting Data into a Database
Next, we insert a data into our database to check whether a user is found in our database.
1. Click our database “login”.
2. Click SQL and paste the below code.
INSERT INTO `user` (`username`, `password`, `fullname`) VALUES ('user', 'user', 'hello world');
Creating a Connection
Next step is to create a database connection and save it as “conn.php“. This file will serve as our bridge between our form and our database. To create the file, open your HTML code editor and paste the code below after the tag.
<?php $conn = mysqli_connect("localhost","root","","login"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } ?>
Creating a Login Form and Login Script
Lastly, we create our Login Form with the login script and save it as “index.php“. In this form, the inputted data of the user will be checked if it is found in the database that we created earlier. To create the form, open your HTML code editor and paste the code below after the tag.
<!DOCTYPE html> <html> <head> <title>Login with Validation PHP, MySQLi</title> <style> .message {color: #FF0000;} </style> </head> <body> <?php // define variables and set to empty values $Message = $ErrorUname = $ErrorPass = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $username = check_input($_POST["username"]); if (!preg_match("/^[a-zA-Z0-9_]*$/",$username)) { $ErrorUname = "Space and special characters not allowed but you can use underscore(_)."; } else{ $fusername=$username; } $fpassword = check_input($_POST["password"]); if ($ErrorUname!=""){ $Message = "Login failed! Errors found"; } else{ include('conn.php'); $query=mysqli_query($conn,"select * from `user` where username='$fusername' && password='$fpassword'"); $num_rows=mysqli_num_rows($query); $row=mysqli_fetch_array($query); if ($num_rows>0){ $Message = "Login Successful!"; } else{ $Message = "Login Failed! User not found"; } } } function check_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <h2>Login Form</h2> <p><span class="message">* required field.</span></p> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> Username: <input type="text" name="username" required> <span class="message">* <?php echo $ErrorUname;?></span> <br><br> Password: <input type="password" name="password" required> <span class="message">* <?php echo $ErrorPass;?></span> <br><br> <input type="submit" name="submit"> <br><br> </form> <span class="message"> <?php if ($Message=="Login Successful!"){ echo $Message; echo 'Welcome, '.$row['fullname']; } else{ echo $Message; } ?> </span> </body> </html>
Happy Coding! How to Create a Login Form with Validation in PHP and MySQL is now complete!
Related Tutorials: Login with Session using AngularJS in PHP/MySQLi, Display Username After Login in PHP, Prevent the Return to Login Page/Disable Back after Login in PHP, Setting up Cookie upon Login using PHP/MySQLi, How To Create Login with Remember Me Function in PHP, User Login Using AngularJS in PHP, How to Create Login and Logout Page with Session and Cookies in PHP
w;x,