simple login system in php MySql database with live demo



Structure of login system 

Database name:-logindb
Database table name:-logintab

simple login system created 4 files 
1.signin.php
2.login.php
3.forget.php
4.home.php

First, we will create the database logindb.

Then after we will create a table.
 

Table structure for table `logintab`

CREATE TABLE IF NOT EXISTS `logintab` (
`id` int(8) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL, `email` varchar(50) NOT NULL, `password` varchar(50) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;


Config.php

Now, we will connect the database to the server like localhost
<?php

$host="localhost";
$user="root";
$pwd="";
$dbname="logindb";

$conn=mysqli_connect($host,$user,$pwd,$dbname);
if(!$conn){
die('could not connected');
}
?>

 

signin.php

 The final main topic is to create a sign-in form.

 Allows the user to enter their data into the database for sign-in.

You also Like 

We will create a login link if the user is already signed, then the user will be redirected to the login page via the link.



<?php
include('config.php');
if(isset($_POST['submit'])){

$name=$_POST['txtn'];
$email=$_POST['txte'];
$pwd=md5($_POST['txtp']);

$query="INSERT INTO logintab(name,email,password)values('$name','$email','$pwd')";

$result=mysqli_query($conn,$query);
header('location:login.php');
}
?>

<form method="post"action="">
Name
<input type="text" name="txtn"><p>
Email
<input type="email" name="txte"><p>
Password
<input type="password" name="txtp"><p>

<input type="submit" name="submit" value="submit">
Already account <a href="login.php">Login</a><br>

</form>

login.php

The user automatically redirects to the login page after the sign-in has finished.

 Here are two links.

 One is a sign-in link and another is a forgotten password link.

If the user is not signed in, the user clicks on the link and redirects to the sign-in page.

 If the user has forgotten his password, the user clicks on the Forgot Password link.

Forgot the user password page.



<?php
include('config.php');

session_start();
if(isset($_POST['login'])){

$email=$_POST['txte'];
$pwd=md5($_POST['txtp']);

$query="SELECT *FROM logintab where email='$email' AND password='$pwd'";

$result=mysqli_query($conn,$query);

$num=mysqli_fetch_array($result);
if($num>0){
$_SESSION['email']=$email;
//echo"<script>location.window.href='home.php'</script>";
header('location:home.php');
}else{
echo"<script>alert('something wrong try again');</script>";
}
}
?>

<form method="post" action="">
Email
<input type="email" name="txte"><p>

Password
<input type="password" name="txtp"><p>

<input type="submit" name="login" value="login"><p>
No account <a href="signin.php">sing-in</a><br>
<a href="forget.php">forget password</a>
</form>

forget.php

Forgot password, in the preview we showed you that if a user has forgotten his password, the user will redirect to this page.


<?php 
include('config.php');
session_start();
if(isset($_POST['change'])){

$name=$_POST['txtn'];
$email=$_POST['txte'];
$pwd=md5($_POST['txtp']);

$query="select *from logintab where name='$name' AND email='$email'";

$result=mysqli_query($conn,$query);
$num=mysqli_fetch_array($result);
if($num>0){
mysqli_query($conn,"update logintab set password='$pwd' where email='$email' AND name='$name'");
$_SESSION['email']=$email;
header('location:login.php');
}else{
echo"<script>alert('something wrong try again')</script>";
}
}
?>

<form method="post" action="">
Name
<input type="text" name="txtn"><p>

Email
<input type="email" name="txte"><p>

New password
<input type="password" name="txtp"><p>

<input type="submit" name="change" value"Reset">
</form>
If the user login successfully.
The user is able to view the home page.
Here, we will create a logout button with the session.
If a user clicks on the logout button, the session will be destroyed and redirected to the user login page.

home.php


<?php
session_start(); if(!isset($_SESSION['email'])){ header('location:login.php'); } session_destroy(); ?> <h1>welcome to home page</h1> <a href="login.php">logout</a>


Live Demo





  Download Source code

Comments