How we can create a Login and Logout Form in Php and mysql

Php Script to create a Login and Logout Form in Php


Login and Logout Form in PHP:  Hi readers, today we came up with the topic Login and Logout form using PHP and Mysql. In the beginning, we will provide the details of creating Login Form and ends with Logout Form with full source code. The session is an important concept in Login in terms of security. The session helps to keep the current Login status of the user until the session is destroyed. At the end of the tutorial, we have provided the Zipped file of Login and Logout Form in Php and Mysql which will helps you in any case.Here we start session on Login and Logout Form in Php

Session is an interaction between two end points in a single connection. In web pages session is generally used to store values which can be further used in multiple pages for a single user.

Note: For using session either in creating or destroying one must first start the session

session_start();                               //start a session

Here we have created the Simple Login form (login.php) and then the session is created in another PHP file (loginmodule.php) where username is stored by session in a variable and then redirect to Home page (home.php) if an authenticated users logged in otherwise it will redirect to the Login Page (login.php) again.

Login Form (login.php):

Before creating a login.php we should have a registartion form in PHP .Click here to create Registration Page


<!Doctype html>
 <html>
 <head>
 <title>User Login</title>
 <style>
 .margin
 {
  margin-top:15px;
 }
 </style>
 </head>

<body>
 <!--LOGIN FORM-->
 <form action="loginmodule.php" method="post">


<!--LOGIN FORM-->
<form action="loginmodule.php" method="post">
<div class="margin">
Enter username :
<input type="text"  name="username" value=""/>
</div>
<div class="margin">
Enter Password:
<input type="password"  name="password" value="" />
</div>
<div class="margin">
<input type="submit"  name="login" value="Login"/>
</div>
</form>
</html>

Creating a session:


loginmodule.php
<!Doctype html>
 <html>
 <head><title>Login Module to create a session</title>
 </head>
 <body>
 <?php
 //create a connection with session database in MYSQLi

$conn=mysqli_connect("localhost","root","","session");
 if(!$conn)
 {
  die("error in establishing the connection :". mysqli_connect_error());
 }
 //check the user or check if the form has submitted or not
 if(isset($_POST['login']))
 {
  //removing special charachter from username and password field of Login form in order to free from sql injection 
  $username=mysqli_real_escape_string($conn,$_POST['username']);
  $password=mysqli_real_escape_string($conn,$_POST['password']);

//query for selecting records which matches the entered username and password field of register table
  $sql="select * from register where username='".$username."' AND password='".$password."'";

//run the above query using mysqli_query()
  $run_query=mysqli_query($conn,$sql);
  //check user
  if((mysqli_num_rows($run_query))>0)
  {
  //create a session
  session_start();

 //store the username in SESSION global variable
  $_SESSION['user_name']=$username;

 $session=$_SESSION['user_name'];

 //redirect to HOME page
  header('Location:home.php');
  }
  else
  echo "wrong username or password";

}

?>
 <!--php code ends-->
 </body>
 </html>

HOME Page:


create a file named home.PHP and copy below lines of code and save it.

Here in this file first session is started using session_start() then getting the values from the $_SESSION global variable and stored in another variable which is further used to check if not containing any value by using empty() then redirect to the Login page again otherwise redirect to home page.

At the end, a link is also provided to Logout.

Full Source Code

<!Doctype html>
 <html>
 <head>
 <title>Home Page</title>
 </head>
 <body>
 <?php
 //start a session
 session_start();

//sore the value of user in session super globals
 $userName=$_SESSION['user_name'];

//check if userName is empty then redirect to login page
 if(empty($userName))
 {
  //redirect to Login page
  header('Location:login.php');
 }

//else print username with welcome message 
  echo "welcome $userName"; 
 ?>
 <a href="logout.php">LOGOUT</a>
 </body>
 </html>

After creating the Login form in PHP through session one must also destroy the session otherwise it will destroy automatically after a certain period of time.

Here at the end, we come up with the LOGOUT module so the logged users must log out in order achieve the goal of security.

Logout:


create a PHP file logout.php and save the below lines of code 

<?php
 //start a session
 session_start();
 //desroy the session to logout
 session_destroy();

?>

A snapshot view of Login and Logout Form in Php:

Login and Logout Form in Php
Login and Logout Form in Php
Login and Logout Form in Php
Login and Logout Form in Php

 


Download source code

login-form-in-php.zip

 

I hope you would like this tutorial on Login and Logout Form in Php and MySQL!

Leave a Comment