php-loginform-session

Login Form: Simple PHP Login Form With Session.

Hello Friends,

In this blog post, I will let you know how to make a simple login form with a session using PHP?

Login is the very important facility in any kind of form.

it provides an authentication and authorization to both user and owner.

If you are selling a product or running a subscription base service then it becomes very important to have a login form so that

we could maintain our user profile and a user can explore in the domain of their account without touching the other’s data.

In this function, you will see a login form and when you will enter the username and password then you will be redirected to your account or profile with a session created.

In your account, you will get the option for logout. When you will click the logout option then you will come out of your profile with a session destroyed. See the below image for the same.


login1

login2


What is the procedure to make a simple login form in PHP?

You just need to follow the below step by step procedure for the same.

First, you should know the software used for developing this functionality. Though you can use the either the same or similar software for the same.

Install NetBeans IDE
Install Xampp Server

Start Apache server and SQL server using your XAmpp.

If you don’t know how to start the Xampp server then you can go with this link.

Now open PHP MyAdmin and create a database name as a test and then create a table login. You can also use the below script for creating the database.

Now insert one record as username and password in the database so that it can be used while you will make login to your form.

Open the NetBeans IDE and create a project named as Login _Form, though you can keep any other name as per your wish.

Now the create few files as shown in the below image in the source folder of your project.

Once you create all the files then copy and paste the code one by one in the respective files which are given below.


index.php:

<?php
include('login.php'); // Here we are including login script

if(isset($_SESSION['login_user'])){
header("location: myprofile.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login Form in PHP with Session</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="main">
<h1>PHP Login Session Example</h1>
<div id="login">
<h2>Login Form</h2>
<form action="" method="post">
<label>UserName :</label>
<input id="name" name="username" placeholder="username" type="text">
<label>Password :</label>
<input id="password" name="password" placeholder="**********" type="password">
<input name="submit" type="submit" value=" Login ">
<span><?php echo $error; ?></span>
</form>
</div>
</div>
</body>
</html>

login.php:

<?php
session_start(); // Here we are starting a session
$error=''; // This variable is to store the error message.
if (isset($_POST['submit'])) {
if (empty($_POST['username']) || empty($_POST['password'])) {
$error = "Username or Password is invalid";
}
else
{
// Here define username and password.
$username=$_POST['username'];
$password=$_POST['password'];
// Here we are establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("localhost", "root", "");
// This is to protect MySQL injection for security purpose.
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
// Selecting Database
$db = mysql_select_db("test", $connection);
// This SQL query will fetch the registered user and will match the login user.
$query = mysql_query("select * from login where password='$password' AND username='$username'", $connection);
$rows = mysql_num_rows($query);
if ($rows == 1) {
$_SESSION['login_user']=$username; // Here we are Initializing Session
header("location: myprofile.php"); // Redirection To Other Page
} else {
$error = "Username or Password is invalid";
}
mysql_close($connection); // Closing Connection
}
}
?>

session.php:

<?php
// Establishiing the server connection by passing server id and password
$connection = mysql_connect("localhost", "root", "");
// Selecting Database
$db = mysql_select_db("test", $connection);
session_start();// Starting Session
// Storing Session
$user_check=$_SESSION['login_user'];
// This sql query will fetch all the user information.
$ses_sql=mysql_query("select username from login where username='$user_check'", $connection);
$row = mysql_fetch_assoc($ses_sql);
$login_session =$row['username'];
if(!isset($login_session)){
mysql_close($connection); // Closing Connection
header('Location: index.php'); // Redirection To Home Page
}
?>

myprofile.php:

<?php
include('session.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Your Home Page</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="profile">
<b id="welcome">Welcome : <i><?php echo $login_session; ?></i></b>
<b id="logout"><a href="logout.php">Log Out</a></b>
</div>
</body>
</html>

logout.php:

<?php
session_start();
if(session_destroy()) // Here we are destrying all the session.
{
header("Location: index.php"); // Redirecting To Home Page
}
?>

style.css:

@import url('https://fonts.googleapis.com/css?family=Spectral');
/*----------------------------------------------
CSS Settings For HTML Div ExactCenter
------------------------------------------------*/
#main {
width:960px;
margin:50px auto;
font-family: 'Spectral', serif;
}
span {
color:red
}
h2 {
background-color:#e4e3ea;
text-align:center;
border-radius:10px 10px 0 0;
margin:-10px -40px;
padding:15px;
color: rebeccapurple;
}
hr {
border:0;
border-bottom:1px solid #ccc;
margin:10px -40px;
margin-bottom:30px
}
#login {
width:300px;
float:left;
border-radius:10px;
font-family:raleway;
border:2px solid #ccc;
padding:10px 40px 25px;
margin-top:70px;
background-color: #d7d6dc;
}
input[type=text],input[type=password] {
width:99.5%;
padding:10px;
margin-top:8px;
border:1px solid #ccc;
padding-left:5px;
font-size:16px;
font-family: 'Spectral', serif;
}
input[type=submit] {
width:100%;
background-color:#999aca;
color:#fff;
border:2px solid #a7a6d2;
padding:10px;
font-size:20px;
cursor:pointer;
border-radius:5px;
margin-bottom:15px
}
#profile {
padding:50px;
border:1px dashed grey;
font-size:20px;
background-color:#DCE6F7
}
#logout {
float:right;
padding:5px;
border:dashed 1px gray
}
a {
text-decoration:none;
color:#6495ed
}
i {
color:#6495ed
}

In a case of any queries, you can write us at anurag@a5theory.com we will get back to you ASAP.

Hope! you would have enjoyed this post.

Please feel free to give your important feedbacks in the comment section below.

Have a great time! Sayonara!