Skip to main content

How to Create a secure login panel with PHP & Mysql using parameters and session

Secure Login with PHP & Mysql

How to Create a secure login panel 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 :


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');

That means you set your username =  admin and password = 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>
Dashboard: index.php


<?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>

If you want to download project zip Click to download.


Comments

Popular posts from this blog

Create dynamic table using bootstrap (filter & find records)

Data Table With Bootstrap Download HTML file Into : In this table, you can short, filtering, find the record and many features you have been used but its very easy to use and configure. so don't get late lets introducing data table CSS:  Data table Stylesheet JS:   Data table js download this for your next project. or you can copy and paste bellow con on your project it's definitely working. Source : <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Data Table</title> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script> <script src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap4.min.js"></script> <script> $(document).ready(function(){       $('#myTable').DataTable(); }); </script> <link...

Which HTML Element helps to SEO Score up viewing result without boost by search engines

Here we discuss SEO and SEO friendly tags in HTML Add caption Meta tags: <meta charset="UTF-8"> <meta name="description" content="Here you can write your website descripsions"> <meta name="keywords" content="here you can write your description and all tahte sapareted by comma like (key1, key2, kay3)"> <meta name="author" content="Here you write website's authers name"> Headline tag: <h1>Headline tag 1</h1> <h2>Headline tag 1</h2> <h3>Headline tag 1</h3> This tag is most importent tag in website for SEO. Use H1 tag in wevery page of your website. its helps to identify content  Attribute : ALT This attribute used to find image form search engine. in this tag, you have to use for your website and type the title of the image. like  <img src="address.jpg" alt="title of your image">   TITLE This attribut...