main_login.php
<html>
<body bgcolor=”C69022″>
<table width=”300″ border=”0″ align=”center” cellpadding=”0″ cellspacing=”1″ bgcolor=”#CCCCCC”>
<tr>
<form name=”form1″ method=”post” action=”checklogin.php”>
<td>
<table width=”100%” border=”1″ cellpadding=”10″ cellspacing=”1″ bgcolor=”EED0F5″>
<tr>
<td colspan=”5″><strong><h1><center>Login Form</center></h1></strong></td>
</tr>
<tr>
<td width=”78″>Username</td>
<td width=”6″>:</td>
<td width=”294″><input name=”myusername” type=”text” id=”myusername”></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name=”mypassword” type=”text” id=”mypassword”></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type=”submit” name=”Submit” value=”Login”></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>
checklogin.php
<?php
$host=”localhost”; // Host name
$username=”root”; // Mysql username
$password=””; // Mysql password
$db_name=”test”; // Database name
$tbl_name=”members”; // Table name
// Connect to server and select databse.
mysql_connect(“$host”, “$username”, “$password”)or die(“cannot connect”);
mysql_select_db(“$db_name”)or die(“cannot select DB”);
// username and password sent from form
$myusername=$_POST[‘myusername’];
$mypassword=$_POST[‘mypassword’];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql=”SELECT * FROM $tbl_name WHERE myusername=’$myusername’ and mypassword=’$mypassword'”;
$result=mysql_query($sql)or die(mysql_error());
//var_dump($result);die();
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file “login_success.php”
$_SESSION[‘myusername’]==$myusername;
$_SESSION[‘mypassword’]==$mypassword;
//session_register(“myusername”);
//session_register(“mypassword”);
header(“location:login_success.php”);
}
else {
echo “Wrong Username or Password”;
}
?>
login_success.php
<?php
session_start();
if(!$_SESSION[‘myusername’]= “myusername”){
header(“location:main_login.php”);
}
else
{
header(“location:success.php”);
}
?>
success.php
<html>
<body bgcolor=”C69022″>
Login Successfully………..
<a href=”Logout.php”>Logout</a>
</body>
</html>
Logout.php
<?php
session_start();
session_destroy();
header(“location:main_login.php”);
?>