Introduction
This article explains login and logout with session in PHP. You will first create a database and a table named login and then create a login form with simply two fields, username and password. Then you will make a connection with your MySQL table "login" and enter some PHP code. I will use a session for authentication purposes in login and logout.

Example
First of all, create the "index.php" file as in the following:
<html>
<head>
<style>
a{
float:left;
text-decoration:none;
padding:0px 2px 0px;
}
h1{
color:#008844;
font-size:20px;
text-align:center;
}input{
background-color:#33FFFF;
color:#000;
}
</style>
</head>
<body>
<div style="border: 1px solid #4A0000;color:#4A0000;margin: auto;width:700px;height:100px;background-color: #CCCCCC;">
<div style="float:right;">
<a href="signup.php"><input type="submit" name="submit" value="Signup"></a> <a href="Login.php"><input type="submit" name="submit" value="login"></a>
</div>
</div>
<div style="border: 1px solid #4A0000;color:#4A0000;margin: auto;width:700px;height:500px;background-color: #CCCCCC;">
<h1>Welcome to my website </h1>
</div>
</body>
</html>
Output

This is your "login.php" file.
<?php
$error="";
mysql_connect("localhost","root","")or dir(mysql_error());
mysql_select_db('demo');
session_start();
if(isset($_POST['submit'])){
$username = $_POST['username'];
$password = $_POST['password'];
if(isset($username) && isset($password)){
$query="SELECT id FROM login WHERE username='$username' and password='$password'";
$result=mysql_query($query);
$row=mysql_fetch_array($result);
$id=$row['id'];
$count=mysql_num_rows($result);
if($count==1)
{
//session_register("username");
$_SESSION['name']=$username;
header("location: welcome.php");
}else{
$error = 'please enter username and password';
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />





kira wehaPosted Dec 18, 2014, 4:15 AM
really wonderfull
Hamid GafforovPosted Apr 15, 2014, 11:11 AM
Thanks...
Vinod KumarPosted Mar 28, 2014, 12:17 AM
you welcome...
Nilesh BargajePosted Mar 26, 2014, 5:17 AM
thnks...