Simple and Perfect Login and Logout Method Using Php and Mysql with Session. | Sanwebcorner

Simple and Perfect Login and Logout Method Using Php and Mysql with Session.

 This article shows how to make a login logout script using php and mysql with session. This login form is designed by using css and html, you may change the view of the form using your own css code.

In this below example i shown to create database and table manually and insert the data manually in database. index.php shows the login form design, login.php is the main php code to check the username and password is valid or not. If it is correct home.php file will shown to the client, otherwise the error will displayed to the client.



simple login logout method

Create the database

create database sanwebcorner ;

Create the table


CREATE TABLE IF NOT EXISTS `login` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `UserName` varchar(200) NOT NULL,
  `Password` varchar(200) NOT NULL,
  `FName` varchar(200) NOT NULL,
  `LName` varchar(200) NOT NULL,
  PRIMARY KEY (`id`)
);

insert the data:

INSERT INTO `login` (`id`, `UserName`, `Password`, `FName`, `LName`) VALUES
(1, 'sandil', 'sandil', 'sandil', 'sand'),
(2, 'jk', 'jk', 'Naveen', 'benny');

connection.php:


<?php
$conn=mysql_connect("localhost","root","") or die("database not connected");
$db=mysql_select_db("sanwebcorner",$conn) or die("database not connected");
?>

index.php:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Simple Login and logout with session using php and mysql</title>
<style type="text/css">
body
{
padding:0px;
margin:0px;
}
.sanstyle
{
width:500px;
background-color:#EFEFEF;
margin-left:auto;
margin-right:auto;
padding:20px;
border:1px solid #969696;
margin-top:100px;
border-radius:15px;
font-size:18px;
}
.head
{
width:auto;
height:80px;
background-color:#AECC3D;
margin-top:0px;
}

.foot
{
margin-top:200px;
width:auto;
height:80px;
background-color:#AECC3D;
}

.textbox
{
height:30px;
width:200px;
}
.login
{
width:80px;
height:35px;
}
</style>
</head>
<body>
<div class="head"><br><marquee><h3 style="color:#FFFFFF;"> Simple Login Logout Method using Php mysql with session.</h3></marquee></div>
<div class="sanstyle">
<h2 style="text-decoration:underline; text-align:center;">Login Here</h2>
<div align="center"><img src="lock.png" width="100"></div>
<form name="login" action="login.php" method="post" >
<table cellpadding="15px"  align="center">

<tr>
<td>

Name:</td><td><input type="text" name="UserName"  class="textbox"></td></tr>
<tr><td>Password:</td><td><input type="password" name="Password" class="textbox"></td></tr>
</table>
<center> <input type="submit" name="submit" value="login" class="login"></center>

</form>
</div>
<div class="foot"><a href="http://www.sanwebtutorials.blogspot.in/">San Web Corner</div>
</body>
</html>

login.php:

<?php
include('connection.php');
if (isset($_POST['submit']))
{
$UserName=$_POST['UserName'];
$Password=$_POST['Password'];
$result=mysql_query("select * from login where UserName='$UserName' and Password='$Password'") or die(mysql_error());
$count=mysql_num_rows($result);
$row=mysql_fetch_array($result);

if($count > 0){
session_start();
$_SESSION['id']=$row['id'];
header('location:home.php');
}
else
{


echo "<script>alert('Your UserName or Password is Wrong Please Retype UserName and Password Again!')</script>";

echo "<script>window.location = 'index.php'</script>"; 



}
}

?>



home.php:


<?php
include('connection.php');
session_start();
if (!isset($_SESSION['id']))
{
header('location:index.php');
}
?>
<div align="right"><a href="logout.php" style="margin-right:80px;">logout</a></div>

<br><br>
<?php
$id=$_SESSION['id'];

$result=mysql_query("select * from login where id='$id'")or die(mysql_error);
$row=mysql_fetch_array($result);

$fname=$row['fname'];
$lname=$row['lname'];

echo "Welcome ". $fname." ".$lname;
?>

logout.php:

<?php
session_start();
session_destroy();
header('location:index.php');
?>

Download Full Coding Here

1 comments:

Post a Comment

Labels

360 degree view product image 3d rotate 3d text 3step-form Accordion menu ajax All Browsers Animated Background Gradient animation audio controller auto complete Best accordion menu bootstrap border bounce effect on hover Breadcrumb calculate age by dob calendar Captcha change image on mouse move color chooser countdown Counter on scroll create random number Css css animation css buttons css hover css3 customized upload Button Date picker delete data from table Different scroll speed email otp verification email verifier Embed eventlist Expanding Menu feedbck form flash news section close button full screen menu Google custom search Google Maps Google reCAPTCHA Google site search gradient hide menu Horizontal News ticker Hover Htm Html html dropdown html form html5 html5 form validation Iframe image hover increment decrement textbox value based on select box Javascript Jquery js Layered image effect on mouse hover lock & unlock page scroll marquee marquee with close button mobile number validation mobile otp login Multi-level-drop-down-menu multi-level-form multi-select-checkbox Multi-Step-form Multiple File Upload in PHP mysql News ticker on hover change text to image our team page password generator password strength checker Photoshop PHP php mailer populate html selectbox Popup image on click the link pure css Question type form random number generate Random password Redirect page Based on the select box while submit the form responsive responsive menu responsive popup box Responsive Vertical Time line design rotate image selectbox based on select box simple coming soon page simple countdown based on end date simple typing effect slider sliding tab sms otp login statistic counter on scroll switch tag shape team member Time picker toggle toggle view transparent button underline animation up down slide validation vertical fixed sidebar Webtips whatsapp link website Youtube