PHP MySQL Login System Using SESSION Source Codes
![]() |
| PHP MySQL Login With SESSION |
Hi Guys I hope you are doing Well. Today We will learn How to Create a SESSION Based Login System Using PHP.
It's very Easy and Simple.I Hope you will Enjoy This Article.
Database and Files Structure:
![]() |
| PHP MySQL Login With SESSION |
Database Name: codenair
Table Name: user
use Below SQL Codes for Create Table called test.
CREATE TABLE IF NOT EXISTS `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) NOT NULL, `password` varchar(50) NOT NULL, `fullname` varchar(100) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
Now we Will Insert some rows in test table using Below codes.
INSERT INTO `user` (`id`, `username`, `password`, `fullname`) VALUES (1, 'RAJ', '12345', 'ANJAN KUMAR DHALI'), (2, 'ABCD', '12345', 'I Am Superstar');
Now We have Successfully Completed our Works on Database.
![]() |
| PHP MySQL Login With SESSION |
1. connect.php
2. index.php
3. profile.php
4. logout.php
5. style.css
Now Copy The below codes for your Different files.
1. connect.php
connect.php file to create connection with database and sever<?php
// Creating connection test is our database name
$conn = new mysqli('localhost', 'root', '','codenair');
// Checking connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
2. index.php
we use index page to display login form and process the user logged information.if the user session is activated user will automatically redirect profile page instead of login page.<?php
session_start();
//checking for current user session
if(isset($_SESSION['user'])){
//Redirect Active Session user to Profile page
header("location: profile.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 MySQL Login Session Example</h1>
<form action="" method="post">
<table>
<tr>
<td><span>UserName :</span></td>
<td><input id="name" name="username" type="text" required></td>
</tr>
<tr>
<td><span>Password :</span></td>
<td><input id="password" name="password" type="password" required></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="login" value="Login"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
<?php
//processing the login information
include("connect.php");
if (isset($_POST['login'])) {
if (empty($_POST['username']) || empty($_POST['password'])) {
echo "Username or Password is Empty";
}else{
// Define $username and $password
$username = mysqli_real_escape_string($conn,$_POST['username']);
$password = mysqli_real_escape_string($conn,$_POST['password']);
// SQL query to fetch information of registerd users and finds user match.
$result =mysqli_query($conn,"select * from user where password='$password' AND username='$username'");
$row =$result->num_rows;
//if user match $row must be 1
if ($row == 1) {
$_SESSION['user']=$username; // Registering SESSION for logged user
header("location: profile.php"); // Redirecting To Other Page
}else{
echo "Username or Password is invalid";
}
}
}
?>
3. profile.php
Displaying Logged user Information<?php
session_start();
//Checking for Current user session
if(empty($_SESSION['user'])){
header('location:index.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Welcome to <?php echo $_SESSION['user']; ?> Profiles</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="profile">
<b id="welcome">Welcome : <i><?php echo $_SESSION['user']; ?></i></b>
<b id="logout"><a href="logout.php">Log Out</a></b>
<?php
include('connect.php');
//Getting Active SESSION User Data From Database
$result=mysqli_query($conn,"SELECT*FROM user WHERE username='".$_SESSION['user']."'");
$row=$result->fetch_assoc();?>
<h3>Your Username is: <?php echo $row['username'];?></h3>
<h3>Your Password is: <?php echo $row['password'];?></h3>
<h3>Your Full name is: <?php echo $row->fullname;?></h3>
<?php ?>
</div>
</body>
</html>
4. logout.php
Destroying current User Session.<?php
session_start();
session_destroy();
header('location:index.php');
?>
7.Codes for style.css
Style our index and profile page.input[type=text],[type=password]{
width: 250px;
padding: 8px;
margin: 4px 0;
box-sizing: border-box;
border: 1px solid #808080;
-webkit-transition: 0.5s;
transition: 0.5s;
font-size:20px;
}
span{
font-size:22px;
color:green;
font-weight:bold;
}
input[type=text]:focus {
border: 1px solid green;
}
input[type=submit]{
background:black;
color:white;
padding:6px 8px;
font-weight:bold;
cursor:pointer;
border-radius:5px;
}
#profile{
background:#ccc;
width:500px;
height:200px;
margin:auto;
border:1px solid green;
}
#logout{
border:1px solid green;
font-size:20px;
}
#main{
width:600px;
box-shadow:3px 4px 14px 11px #ccc;
margin:auto;
}
h3{
color:green;
}
I hope you have Successfully Created PHP MySQL SESSION Based Login System.if You face any problems please comments and Keep Visiting for Next Tutorial.Thank You.




No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.