Tuesday, 4 October 2016

PHP Login System with MYSQLI Connection

This article aims at a successful login system with username and password using php and mysqli, this script is and error free login script, this script will be use on my on going project ' create a forum like Nairaland '
if you do follow my previous post that is great...

Demo   Download

i am using my previous post database information, there are three php file on this folder
1)config.php
2) index.php
3) home.php
4) verify.php
5) logout.php

Config.php


<?php
session_start();
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'nairaland');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);

?>

index.php


<?php include 'verify.php'; ?>

<div class = "panel panel-default">

<?php echo $error; // display error message?></h1>


<div class ="panel-heading">
<form class="form-inline" action="?" method="post">
<div class="form-group"> <label for="exampleInputEmail2">User name: </label>
<input type="text" required class="form-control" name="username" id="exampleInputEmail3" placeholder="Enter email"> </div>
<div class="form-group"> <label for="exampleInputEmail2"> Password</label>
<input type="password" required class="form-control" name="password" id="exampleInputPassword3" placeholder="Password"> </div>
<button type="submit" name="Logusers" class="btn btn-primary">Login</button>
</form>
</div>
</div>

Home.php


<?php
include 'config.php';

if(!$_SESSION['AutenUsera']){ // if $_SESSION['AutenUsera']=0;

header("location:index.php"); // redirect bacl to index login page
exit; // exist page
}
?>

<div class="section-title">
<h1>You are Welcome to Labs Forum <?php echo $_SESSION['username']; // display logged in username ?>!</h1>
<a href="logout.php">Logout</a>
<div class="divider"></div>
<h3>THis is your secured page!</h3>
</div>

Verify.php

<?php
include 'config.php'; // database configuration settings

$error="";
if(isset($_POST['Logusers'])){
$username = htmlentities($_POST['username']); //Storing username in $username variable.
$password = md5($_POST['password']); //Storing password in $password variable.

$match = $db->query("SELECT * FROM users WHERE username='$username' AND password='$password' AND status='1' ");
$data = mysqli_fetch_array($match);
$user_id= $data['uid'];
$username = $data['username'];
$email = $data['email'];


if(mysqli_num_rows($match)!=1){ //if there is no such username and password.

$error="<small class='alert alert-danger'>Sorry, there is no username with the specified password.</small>";
// error message to display
}

$_SESSION['user_id']=$user_id; //Storing user ID in SESSION variable.
$_SESSION['username']=$username; //Storing USERNAME in SESSION variable.
$_SESSION['email']=$email; //Storing EMAIL in SESSION variable.


$_SESSION['AutenUsera']=0; //SET TO FALSE.
if(($_SESSION['username']=$username) and ($_SESSION['password']=$password)){
$_SESSION['AutenUsera']=1; //SET TO TRUE.
header("location: home.php?user=$username"); //Redirect after successful login.
}
}

?>

logout.php

<?php
//initialize the session
if (!isset($_SESSION)) {
session_start();
}

unset($_SESSION['AutenUsera']);
session_destroy();
header("location: index.php?logout=true");
?>

Love this tutorial share or comment we love that...

1 comment:

  1. Will try all my best to keep u updated... Wait for next script... Subscribe...

    ReplyDelete

Hit me with a comment!