How to make a simple html login form using php that match username & password from database than start season.
In this tutorial we are simply creating an login form with UserName and Password and after that matching that username-password to already stored username-password.If both values are correct and match from database then it will login us and redirect to dashboard.php page or any of them is wrong then it will show us an error message. So here is the complete step by step tutorial for Create login page in PHP with MySQL db with session tutorial download.
List of files that are used in this project :
- index.php
- dbconfig.php
- dashboard.php
- logout.php
- login-style.css
Note: The default login username is admin and password is admin .
How to Create login page in PHP with MySQL db with session tutorial download.
Code for index.php file.
<html> <head> <title>User Login Form</title> </head> <link href="style/login-style.css" rel="stylesheet" type="text/css"> <body> <form method="post" action=""> <table border="2" cellpadding="15" cellspacing="2" width="400" align="center"> <tr class="tabheader"> <td align="center" colspan="2">Please Enter UserName & Password to Login.</td> </tr> <tr class="row"> <td align="center">UserName</td> <td><input type="text" name="user_name"></td> </tr> <tr class="row"> <td align="center">Password</td> <td><input type="password" name="password"></td> </tr> <tr class="tabheader"> <td align="center" colspan="2"> <input type="submit" name="submit" value="Submit"></td> </tr> </table> </form> <?php if(isset($_POST["submit"])) { if(count($_POST)>0) { //Including dbconfig file. include 'dbconfig.php'; $username = $_POST["user_name"]; $password = $_POST["password"]; $finalResult = mysql_query("SELECT * FROM login WHERE UserName='" . $_POST["user_name"] . "' and Password = '". $_POST["password"]."'"); $confirm = mysql_fetch_array($finalResult); if(is_array($confirm)) { session_start(); $_SESSION['sid']=session_id(); header("location:dashboard.php"); } else { echo '<center>' . "Wrong UserName or Password..." . '</center>'; } } } ?> </body></html>
Code for dbconfig.php file.
<?php //Define your host here. $hostname = "localhost"; //Define your database username here. $username = "root"; //Define your database password here. $password = ""; //Define your database name here. $dbname = "test"; $conn = mysql_connect($hostname, $username, $password); if (!$conn) { die('Could not connect: ' . mysql_error()); } mysql_select_db($dbname, $conn); ?>
Code for dashboard.php file.
<?php session_start(); if($_SESSION['sid']==session_id()) { echo "<a href='logout.php'>Logout</a>"; } else { header("location:index.php"); } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Home Page</title> </head> <body> <table border="2" cellpadding="15" cellspacing="2" width="400" align="center"> <tr><td align="center"> <h2>Welcome User</h2></td> <tr><td align="center"> <a href='logout.php'>Logout</a> </td></tr> </tr> </table> </body> </html>
Code for logout.php file.
<?php echo "Logged out scuccessfully"; session_start(); session_destroy(); setcookie(session_id(),time()-1); header("location:index.php"); ?>
Code for login-style.css file.
@charset "utf-8"; /* CSS Document */ .tabheader { background-color: #01fbcf; color:b #000000; font-weight:bold; } .row { background-color: #02caa7; color:white; }
Screenshots: