Skip to content
pakainfo

Pakainfo

Web Development & Good Online education

  • Home
  • Blog
  • Categories
  • Tools
  • Full Form
  • Guest Post
  • Advertise
  • About
  • Contact Us

PHP login logout script with session Example

February 3, 2021 Pakainfo Laravel, Mysql, Mysqli, php Leave a comment

Today, We want to share with you session in php example for login and logout.In this post we will show you how to use session in php for login form with example?, hear for php simple login logout we will give you demo and example for implement.In this post, we will learn about registration and login form in php and mysql with an example.

PHP Login logout example with session

Contents

  • PHP Login logout example with session
    • Create Database Configuration file
    • HTML Login Form
    • After Form Submit PHP Code
    • After Successful Login
    • Session Destroy
    • Error Message (index.php)
    • Successfully Logout Message
    • Related posts

Create Database:

Create database pakainfo_app;

Create Database Table:

CREATE TABLE `employees` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`employee_nm` VARCHAR(255) NULL DEFAULT NULL,
`employee_snm` VARCHAR(255) NULL DEFAULT NULL,
`employee_mail_address` VARCHAR(255) NULL DEFAULT NULL,
`phone` VARCHAR(255) NOT NULL,
`password` VARCHAR(255) NULL DEFAULT NULL,
`created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=7
;

Create Database Configuration file:

config.php

<?php 
	$host = 'localhost';
	$DBUser = "root";
	$DBPassword = '[email protected]';
	$db = 'pakainfo_app';
	
	$link = mysqli_connect($host,$DBUser, $DBPassword, $db);
	
	if(!$link)
	{
		die(mysqli_error());
	}
	
?>

HTML Login Form:

index.php

<div class="container">
		<h2>PHP Login and Logout with Session - www.pakainfo.com</h2>
		<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
			<div class="field-container">
				<label>Employee Email</label>
				<input type="email" name="employee_mail_address" required placeholder="Enter Your Employee Email">
			</div>
			<div class="field-container">
				<label>Employee Password</label>
				<input type="password" name="password" required placeholder="Enter Your Employee Password">
			</div>
			<div class="field-container">
				<button type="submit" name="submit">Submit</button>
			</div>
			
		</form>
	</div>

Style.css:

body{
	font-family:verdana;
	background-color:#aaaae3;
}

.container{
	width:40%;
	margin:10% auto;
	border:1px solid #eeeeee;
	background:#ffffff;
}

.container-dashboard{
	width:90%;
	border:1px solid #eeeeee;
	background:#ffffff;
	padding:10px;
}

.field-container{
	margin:10px auto;
	width:400px;
}
	
h1{
	text-align:center;
	line-height:30px;
	font-size:24px;
	color:#061e5a;
}

label{
	display:block;
	padding-bottom:5px;
	color:#F05519;
	font-weight:500;
}

input[type=email],input[type=password]{
	border:1px solid #eeeeee;
	width:100%;
	height:30px;
	padding-left:4px;
	
}

button{
	background:#061e5a;
	border:1px solid #061e5a;
	color:#ffffff;
	margin:10px 0px;
	padding:5px;
}

button:hover{
	background:#F05519;
	border:1px solid #F05519;
}


.error-msg{
	border:1px solid #ee0000;
	background:#ee0000;
	color:#ffffff;
	padding:2px;
	font-size:13px;
}

.success-msg{
	border:1px solid #0ebc6f;
	background:#0ebc6f;
	color:#ffffff;
	font-size:13px;
	padding:2px;
}


.employee-name{
	color:#ee0000;
}

.signout-link{
	margin-top:10px;
	display:block;
	background:#061e5a;
	border:1px solid #061e5a;
	color:#ffffff;
	width:48px;
	padding:5px;
	text-decoration:none;
	font-size:13px;
}

After Form Submit PHP Code:

index.php

<?php 
	require('config.php');
	session_start();
	
	
	if(isset($_POST['submit']))
	{
		if((isset($_POST['employee_mail_address']) && $_POST['employee_mail_address'] !='') && (isset($_POST['password']) && $_POST['password'] !=''))
		{
			$employee_mail_address = trim($_POST['employee_mail_address']);
			$password = trim($_POST['password']);
			
			$sqlEmail = "select * from employees where employee_mail_address = '".$employee_mail_address."'";
			$rs = mysqli_query($link,$sqlEmail);
			$numRows = mysqli_num_rows($rs);
			
			if($numRows  == 1)
			{
				$row = mysqli_fetch_assoc($rs);
				
				if(password_verify($password,$row['password']))
				{
					$_SESSION['employee_id'] = $row['id'];
					$_SESSION['employee_nm'] = $row['employee_nm'];
					$_SESSION['employee_snm'] = $row['employee_snm'];
					
					header('location:dashboard.php');
					exit;
					
				}
				else
				{
					$errorMsg =  "Wrong Email Or Password";
				}
			}
			else
			{
				$errorMsg =  "No User Found";
			}
		}
	}
?>

After Successful Login:

Dashboard.php

<?php 
	session_start();
		
	if(!isset($_SESSION))
	{
		header('location:index.php');
		exit;
	}
	
?>

<!DOCTYPE html>
<html>
<head>
<title>Dashboard | PHP Login and logout example with session</title>
<link rel="stylesheet" href="style.css">
</head>

<body>
	<div class="container-dashboard">
		Welcome to the dashboard! <span class="employee-name"><?php echo ucwords($_SESSION['employee_nm'])?> <?php echo ucwords($_SESSION['employee_snm']);?> </span> 
		<br>
		
		<a href="signout.php?signout=true" class="signout-link">Logout</a>
	</div>
</body>
</html>

Session Destroy:

signout.php

<?php 

if(isset($_GET['signout']))
{
	session_destroy();
	header('location:index.php?signout=true');
	exit;
}
?>

Error Message: (index.php)

index.php

<?php 
	if(isset($errorMsg))
	{
		echo "<div class='error-msg'>";
		echo $errorMsg;
		echo "</div>";
		unset($errorMsg);
	}
?>

Successfully Logout Message:

index.php

<?php 
	if(isset($_GET['signout']))
	{
		echo "<div class='success-msg'>";
		echo "You have successfully signout";
		echo "</div>";
		
	}
?>

I hope you get an idea about dynamic login page in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or opinions about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Related posts:

  1. PHP Login logout example with session
  2. Secure Login System with PHP and MySQLi – login page in php
  3. Vue.js Simple Login Script using PHP MySQLi Bootstrap
  4. jQuery Ajax Secure Login Registration System in PHP and MySQL
  5. Automatically Timeout-Logout Destroy Session inactivity Using PHP
  6. Create a Registration and Login System with PHP and MySQL
  7. jQuery Ajax Login Script using PHP MySQLi
  8. how to delete data from database in php using button?
Also Read This 👉   Laravel 6 change date format example Tutorial
dynamic login page in phphow to use session in php for login form with examplelogin and logout using session in php and mysqlilogin form in php with session and validationphp login session with databasephp simple login logoutsession in php example for login and logout without databasesimple login form in php

Post navigation

Previous Post:Simple php rating system with database Example
Next Post:how to add edit and delete button in datatable using php?

Advertise With Us

Increase visibility and sales with advertising. Let us promote you online.
Click Here

Write For Us

We’re accepting well-written informative guest posts and this is a great opportunity to collaborate.
Submit a guest post to [email protected]
Contact Us

Freelance web developer

Do you want to build a modern, lightweight, responsive website quickly?
Need a Website Or Web Application Contact : [email protected]
Note: Paid Service
Contact Me

Categories

3movierulz (64) Ajax (464) AngularJS (377) ASP.NET (61) Bio (109) Bollywood (108) Codeigniter (175) CSS (98) Earn Money (69) Education (61) Entertainment (130) fullform (86) Google Adsense (63) Highcharts (77) History (40) Hollywood (109) JavaScript (1357) Jobs (42) jQuery (1423) Laravel (1088) LifeStyle (53) movierulz4 (63) Mysql (1029) Mysqli (890) php (2121) Programming (2332) Python (97) Software (166) Software (88) Stories (98) tamilrockers (104) Tamilrockers kannada (64) Tamilrockers telugu (61) Tech (142) Technology (2392) Tips and Tricks (119) Tools (203) Top10 (478) Trading (90) Trending (71) VueJs (250) Web Technology (105) webtools (191) wordpress (166) World (322)

A To Z Full Forms

Access a complete full forms list with the meaning, definition, and example of the acronym or abbreviation.
Click Here
  • Home
  • About Us
  • Terms And Conditions
  • Write For Us
  • Advertise
  • Contact Us
  • Youtube Tag Extractor
  • Info Grepper
  • Guest Posting Sites
  • Increase Domain Authority
  • Social Media Marketing
  • Freelance web developer
  • Tools
Pakainfo 9-OLD, Ganesh Sco, Kothariya Ring Road, Chokadi, Rajkot - 360002 India
E-mail : [email protected]
Pakainfo

© 2023 Pakainfo. All rights reserved.

Top
Subscribe On YouTube : Download Source Code
We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype Guest Posting Sites