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: