In this tutorial, I am going to teach you how to make a login in using PHP/MySQL using mysqli. Just follow the instructions below.
Instructions
First, we will create our css code and name it as style.css
body {
color: #fff;
font: 87.5%/1.5em 'Open Sans', sans-serif;
background:url(img/bg.jpg)no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;}
.form-wrapper {
width:300px;
height:370px;
position: absolute;
top: 50%;
left: 48%;
margin: -184px 0px 0px -155px;
background: rgba(0,0,0,0.27);
padding: 15px 25px;
border-radius: 5px;
box-shadow: 0px 1px 0px rgba(0,0,0,0.6),inset 0px 1px 0px rgba(255,255,255,0.04);
}
.form-item {
width:100%;
}
h3{
font-size: 25px;
font-weight: 640;
margin-bottom: 10px;
color: #e7e7e7;
padding:6px;
font-family:'sans-serif','helvetica';
}
.form-item input{
border: none;
background:transparent;
color: #fff;
margin-top:11px;
font-family: 'Open Sans', sans-serif;
font-size: 1.2em;
height: 49px;
padding: 0 16px;
width: 100%;
padding-left: 55px;
}
input[type='password']{
background: transparent url("img/pass.jpg") no-repeat;
background-position: 10px 50%;
}
input[type='text']{
background: transparent url("img/user.jpg") no-repeat;
background-position: 10px 50%;
}
.form-item input:focus {
outline: none;
border:1.4px solid #cfecf0;
}
.button-panel {
margin-bottom: 20px;
padding-top: 10px;
width: 100%;
}
.button-panel .button {
background: #00b6df;
border: none;
color: #fff;
cursor: pointer;
height: 50px;
font-family: 'helvetica','Open Sans', sans-serif;
font-size: 1.2em;
text-align: center;
text-transform: uppercase;
transition: background 0.6s linear;
width: 100%;
margin-top:10px;
}
.button:hover {
background: #6eb7cb;
}
.form-item input, .button-panel .button {
border-radius: 2px
}
.reminder {
text-align: center;
}
.reminder a {
color: #fff;
text-decoration: none;
transition: color 0.3s;
}
.reminder a:hover {
color: #cab6bf;
}
Then, we will be creating our database connection. Copy the code below and name it as dbcon.php
<?php
// Check connection
{
}
?>
Next, creating our html code. Copy the code below and name it as index.php
<?php session_start(); ?>
<?php include('dbcon.php'); ?>
<link rel="stylesheet" type="text/css" href="style.css">
<div class="form-wrapper">
<form action="#" method="post">
<input type="text" name="user" required="required" placeholder="Username" autofocus required></input>
<input type="password" name="pass" required="required" placeholder="Password" required></input>
<div class="button-panel">
<input type="submit" class="button" title="Log In" name="login" value="Login"></input>
<?php include('login.php'); ?>
<p>Not a member?
<a href="#">Sign up now
</a></p>
<p><a href="#">Forgot password?
</a></p>
After creating our index.php page, we will now create our php script and name it as login.php. Don't forget to include it in our index.php.
<?php
if (isset($_POST['login']))
{
$query = mysqli_query($con, "SELECT * FROM users WHERE password='$password' and username='$username'");
if ($num_row > 0)
{
$_SESSION['user_id']=$row['user_id'];
}
else
{
echo 'Invalid Username and Password Combination';
}
}
?>
After the login.php, We will create our home.php page.
<?php
include('dbcon.php');
include('session.php');
$result=mysqli_query($con, "select * from users where user_id='$session_id'")or die('Error In Session');
$row=mysqli_fetch_array($result);
?>
<link rel="stylesheet" type="text/css" href="style.css">
<div class="form-wrapper">
<p><a href="logout.php">Log out
</a></p>
Lastly, Our logout.php page.
Finally, you have created a login system in PHP and MySQL using mysqli. For more information, questions or suggestions just comment below or email me at
[email protected]
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.