How to Create a Simple Login with Validation PHP/MySQLi
Submitted by nurhodelta_17 on Saturday, August 19, 2017 - 11:02.
Language
In this tutorial will show you how to create a simple login with user input validation using 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 our 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 our 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 our 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
- // Check connection
- {
- }
- ?>
Creating our 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>
- <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;
- }
- ?>
- <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
- Username: <input type="text" name="username" required>
- Password: <input type="password" name="password" required>
- <input type="submit" name="submit">
- </form>
- <span class="message">
- <?php
- if ($Message=="Login Successful!"){
- echo $Message;
- echo 'Welcome, '.$row['fullname'];
- }
- else{
- echo $Message;
- }
- ?>
- </span>
- </body>
- </html>
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Add new comment
- 1878 views