Secure Login with PHP & Mysql
Requirement: you need an Apache and MySQL Server database on your device
If your computer has xampp or wamp or mamp or lamp that's great otherwise you need to install Apache and MySQL server
Create a database login
Run this query :
If your computer has xampp or wamp or mamp or lamp that's great otherwise you need to install Apache and MySQL server
Create a database login
Run this query :
CREATE TABLE admin (
id INT(6) AUTO_INCREMENT PRIMARY KEY,
uname VARCHAR(30) NOT NULL,
upass VARCHAR(30) NOT NULL
);
INSERT INTO `admin` (`id`, `uname`, `upass`) VALUES (NULL, 'admin', 'admin1993');
Login Page: login.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Login Getway</title>
<link rel="stylesheet" href="http://fontawesome.io/assets/font-awesome/css/font-awesome.css" type="text/css">
<style>
*
{
margin:0px;
padding:0px;
font-family:arial;
}
body
{
background:#DAECF3;
}
hr
{
background:#012D41;
border:none;
height:10px;
}
#logarea
{
height:250px;
border-radius:5px;
width:400px;
padding:20px;
background:rgba(255,255,255,1.00);
position:absolute;
top:0px; bottom:0px; left:0px; right:0px;
margin:auto;
-webkit-box-shadow: 0px 1px 1px 1px rgba(0,0,0,0.15);
-moz-box-shadow: 0px 1px 1px 1px rgba(0,0,0,0.15);
box-shadow: 0px 1px 1px 1px rgba(0,0,0,0.15);
}
h1
{
text-align:center;
font-weight:normal;
color:#012D41;
padding:10px;
font-size:24px;
border-bottom:1px solid #012D41;
width:300px;
margin:auto;
}
form
{
padding:20px 40px;
}
form input[type='text'],form input[type='password']
{
padding:10px;
margin-bottom:20px;
width:295px;
font-size:18px;
border-radius:5px;
border:none;
background:#DAECF3;
color:rgba(81,81,81,1.00);
text-align:center;
}
button
{
padding:10px 20px;
background:#012D41;
color:rgba(255,255,255,1.00);
border:none;
border-radius:5px;
}
button:nth-child(1)
{
background:#FF616C;
}
#logarea p
{
position:absolute;
bottom:10px;
left:0px;
width:100%;
text-align:center;
}
#logarea span
{
position:absolute;
bottom:-10px;
left:0px;
background:#012D41;
width:100%;
color:rgba(255,255,255,1.00);
text-align:center;
height:10px;
}
.fa-refresh
{
animation: loadingpw 1s linear infinite;
}
@keyframes loadingpw{
0%{ transform:rotate(0deg)}
100%{transform:rotate(360deg)}
}
</style>
</head>
<body>
<hr>
<div id="logarea">
<h1>Login</h1>
<form method="post" action="?tyringTologing" enctype="multipart/form-data">
<input type="text" name="username" placeholder="username">
<input type="password" name="password" placeholder="password">
<center>
<button type="submit" name="loginbutn">
<i class="fa fa-unlock" aria-hidden="true"></i> Login
</button>
<button type="reset">
<i class="fa fa-repeat" aria-hidden="true"></i> Retype
</button>
</center>
</form>
<p>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "login";
$conn = mysqli_connect($servername, $username, $password,$database);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
session_start();
if(isset($_POST['loginbutn']))
{
$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = mysqli_real_escape_string($conn, $_POST['password']);
$sqlQuery = "SELECT uname, upass FROM admin WHERE uname=? AND upass=?";
$stmt = mysqli_prepare($conn, $sqlQuery);
if($stmt)
{
mysqli_stmt_bind_param($stmt,"ss", $username, $password);
mysqli_stmt_bind_result($stmt, $accUsername, $accPassword);
mysqli_stmt_execute($stmt);
mysqli_stmt_fetch($stmt);
if(!empty($accUsername) AND !empty($accPassword))
{
echo 'Please wait while we redirect you <i class="fa fa-refresh"></i>';
$_SESSION["standerdUse"] = $accUsername;
header("location: index.php");
}
else
{
echo 'Please check your Username and Password';
}
mysqli_stmt_close($stmt);
}
else
{
echo 'Something is worng';
}
}
?>
</p>
<span></span>
</div>
</body>
</html>
<?php
session_start();
if(!$_SESSION['standerdUse'])
{
header('Location:login.php');
echo "<script> window.open('login.php','_self') </script>";
}
$servername = "localhost";
$username = "root";
$password = "";
$database = "login";
$conn = mysqli_connect($servername, $username, $password,$database);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Dash Board</title>
</head>
<body>
<?php
echo "<h1 align='center'>".$_SESSION['standerdUse']." you are successfully come in your admin panel</h1>";
?>
<center><a href="?logout">Logout</a></center>
<?php
if(isset($_GET['logout']))
{
echo "your session logout";
session_destroy();
header('Location:login.php');
echo "<script> window.open('login.php','_self') </script>";
}
?>
</body>
</html>
Comments
Post a Comment