- Take input from user
- Post data to a php script for user verification
- Access the mysql database, and verify the user.
- Print message on the screen
Recoended read,
Get username and password for login
The user enters his username and password and hits on the Login button. The form action is redirected to login.php again where the login script is written in php.Here, we have included an errors.php page which prints the error on the screen during the login process.
Login form
<form method="post" action="login.php"> <p></p> <?php include('errors.php') ?> <p></p> <div class="logininput-group1"> <label for="username">Username</label> <input type="text" name="username" placeholder="Enter username" value="<?php echo $username; ?>" required > </div> <div class="logininput-group1"> <label for="password">Password </label> <input type="password" placeholder="Enter Password" name="password" required> </div> </br> <div class="logininput-group1"> <button type="submit" class="btn" name="login_user">Login</button> </div> <p></p> <p style="text-align: center; font-size: 15px;"><a href="password_recovery.php">Forgot password?</a> </p> <p></p> <p style="text-align: center; font-size: 15px;"> Not yet a member? <a href="register.php">Sign up</a> </p> </form>
Php script access mysql database and verifies user
First, we start a php session and check whether the username session variable is set or not. If it is already set, the user is automatically redirected to the Welcome page, otherwise, the user verification process starts. We check for the username and password field is empty or not. If it is empty, errors are pushed into the array, and later displayed on the screen. If there is no error we build a database connection and make a MySQL select query to check for username match in the database. If we found the matching user is redirected to the welcome page otherwise errors are displayed on the screen.
Login script in php
<?php session_start(); if(isset($_SESSION['username'])) { header("location:welcome.php"); die(); } $username = ""; $password = ""; $errors = array(); // connect to the database $db = mysqli_connect('localhost', 'db-user', '(db-password)', 'database-name'); // login user if (isset($_POST['login_user'])) { $username = mysqli_real_escape_string($db, $_POST['username']); $password = mysqli_real_escape_string($db, $_POST['password']); if (empty($username)) { array_push($errors, "Username is required"); } if (empty($password)) { array_push($errors, "Password is required"); } if (count($errors) == 0) { $password = md5($password); $query = "SELECT * FROM reg_user WHERE username='$username' AND password='$password'"; $results = mysqli_query($db, $query); if (mysqli_num_rows($results) == 1) { $_SESSION['username'] = $username; header("location: welcome.php"); }else { array_push($errors, "Wrong username./password combination"); } } } ?>
If you face any issue while implementing the code, you can contact me. Thanks for patient reading.
If you like this article, share with your friends. It will inspire me to write more.