April 24, 2016
How to Create A Simple Login Form in PHP & MySQLi Tutorial

Hello, friends in this tutorial I show you how to how to create a simple login form in PHP & MySQL. A login web page in PHP is very easy, and need to create MySQL database to save the user data such as username & password. We have a table to save users data then we can create a script in PHP for checking the user’s data, who want to login into our web page.
login.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<?php include("loginserv.php"); // Include loginserv for checking username and password ?> <!doctype html> <html> <head> <meta charset="UTF-8"> <title>Login</title> <style> .login{ width:360px; margin:50px auto; font:Cambria, "Hoefler Text", "Liberation Serif", Times, "Times New Roman", serif; border-radius:10px; border:2px solid #ccc; padding:10px 40px 25px; margin-top:70px; } input[type=text], input[type=password]{ width:99%; padding:10px; margin-top:8px; border:1px solid #ccc; padding-left:5px; font-size:16px; font-family:Cambria, "Hoefler Text", "Liberation Serif", Times, "Times New Roman", serif; } input[type=submit]{ width:100%; background-color:#009; color:#fff; border:2px solid #06F; padding:10px; font-size:20px; cursor:pointer; border-radius:5px; margin-bottom:15px; } </style> </head> <body> <div class="login"> <h1 align="center">Login</h1> <form action="" method="post" style="text-align:center;"> <input type="text" placeholder="Username" id="user" name="user"><br/><br/> <input type="password" placeholder="Password" id="pass" name="pass"><br/><br/> <input type="submit" value="Login" name="submit"> <!-- Error Message --> <span><?php echo $error; ?></span> </body> </html> |
loginserv.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php $error=''; //Variable to Store error message; if(isset($_POST['submit'])){ if(empty($_POST['user']) || empty($_POST['pass'])){ $error = "Username or Password is Invalid"; } else { //Define $user and $pass $user=$_POST['user']; $pass=$_POST['pass']; //Establishing Connection with server by passing server_name, user_id and pass as a patameter $conn = mysqli_connect("localhost", "root", ""); //Selecting Database $db = mysqli_select_db($conn, "test"); //sql query to fetch information of registerd user and finds user match. $query = mysqli_query($conn, "SELECT * FROM userpass WHERE pass='$pass' AND user='$user'"); $rows = mysqli_num_rows($query); if($rows == 1){ header("Location: welcome.php"); // Redirecting to other page } else { $error = "Username of Password is Invalid"; } mysqli_close($conn); // Closing connection } } ?> |
welcome.php
1 2 3 4 5 6 7 8 9 10 11 |
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>Welcome to SoftAOX</title> </head> <body> <h1>Welcome to softAOX</h1> <p>This is Login page</p> </body> </html> |
Download
Mraj
Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts. If you have any questions let me drop an email with the article name to the following email id: [email protected]