Hello all, Here is a simple way to create login page with MD5 password using PHP and PDO driver.
DATABASE
CREATE TABLE admin
(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(40) UNIQUE,
password VARCHAR(40)
);
CODE
db_connect.php
<?php
$host = "localhost";// Host name
$db_name = "admin";
$username = "";// Mysql username
$password = "";// Mysql password
try
{
$con = new PDO("mysql:host={$host}; dbname={$db_name}", $username, $password);
}
catch(PDOException $exception)
{
echo "Connection error: " . $exception->getMessage();
}
?>
Login.php
<?php
session_start();
include 'db_connect.php';
if (isset($_POST['submit']))
{
$username = $_POST['username'];
$password = md5($_POST['password']);
$stmt = $con->prepare("SELECT * FROM `test` WHERE username=:username AND password=:password");
$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', $password);
$stmt->execute();
$num = $stmt->rowCount();
if ($num > 0)
{
$row = $stmt->fetch(PDO::FETCH_ASSOC) ;
$_SESSION["username"] = $row['username'];
header("Location: user.php");
}
else
{
header("Location: login.php");
}
}
?>
<html>
<body>
<style>
@charset "UTF-8";
/*A Flat Design Login Page by Codelator.com*/
body{
background-color:#2c3335;
color:#f5f5f5;
text-align:center;
font-family:"Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", "DejaVu Sans", Verdana, sans-serif;
}
a{
color:#fff;
text-decoration:none;
}
#loginform{
margin-top:150px;
margin-left:auto;
margin-right:auto;
width:270px;
}
.input{
width:270px;
padding:15px 25px;
font-family:"Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", "DejaVu Sans", Verdana, sans-serif;
background: #f5f5f5;
border:none;
border-radius: 5px;
color: #333;
font-size: 14px;
margin-top:15px;
}
.loginbutton{
background-color:#ffdd00;
border-radius:5px/5px;
-webkit-border-radius:5px/5px;
-moz-border-radius:5px/5px;
color:#333;
display:inline-block;
font-family:"Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", "DejaVu Sans", Verdana, sans-serif;
font-size:18px;
font-weight:bold;
width:270px;
text-align:center;
line-height:50px;
text-decoration:none;
height:50px;
margin-top:20px;
margin-bottom:20px;
border:none;
outline:0;
cursor: pointer;
}
.loginbutton:active {
position:relative;
top:1px;
}
.loginbutton:hover{
background-color:#e5bf05;
}
</style>
<form id="loginform" method="POST" action="">
<input type="text" class="input" placeholder="Username" name="username" />
<input type="password" class="input" placeholder="Password" name="password" />
<input type="submit" class="loginbutton" name="submit" value="SIGN IN" />
</form>
<a href="#">Click here for new users</a>
</body>
</html>
user.php
<body>
<h1>Welcome </h1>
</body>
Hope it's useful for you,
if you have any query kindly contact https://www.ultimez.com/Ultimez Technology -Web Design Services in Bangalore