PHP Example
Screen shot
Screen shot
Create page index.php

<?php
session_start();
?>
<html>
<head>
<title>PHP Login script</title>
</head>
<body>
<form name=\"frm\" action=\"login.php\" method=\"post\">
<table border=\"1\">
<tr>
<td colspan=\"2\" align=\"center\">
User Login
<br>
<span style=\"color:#FF0000\">
<?php
if(isset($_SESSION[\'msg\']))
{
echo $_SESSION[\'msg\'];
unset($_SESSION[\'msg\']);
}
?>
</span>
</td>
</tr>
<tr>
<td>username</td><td><input name=\"username\" type=\"text\" /></td>
</tr>
<tr>
<td>userpassword</td><td><input name=\"userpassword\" type=\"password\" /></td>
</tr>
<tr>
<td>&nbsp;</td><td><input name=\"btnsubmit\" type=\"submit\" value=\"Login\" /></td>
</tr>
</table>
</form>
</body>
</html>

Create page login.php

<?php
session_start();
$con=mysql_connect(\"localhost\",\"root\",\"\") or die(\"Mysql not connected\");
$database=mysql_select_db(\"test\",$con) or die(\"Database not connected\");

$username=$_POST[\'username\'];
$userpassword=$_POST[\'userpassword\'];

$query=\"select * from users where username=\'$username\' and userpassword=\'$userpassword\'\";

$result=mysql_query($query);

$row=mysql_num_rows($result);

if($row>0)
{
$_SESSION[\'username\']=$username;
$_SESSION[\'userpassword\']=$userpassword;
header(\"Location:home.php\");
}
else
{
$_SESSION[\'msg\']=\"Invalid user\";
header(\"Location:index.php\");
}

?>

Create page home.php

<?php
session_start();
if($_SESSION[\'username\']==\'\' && $_SESSION[\'userpassword\']==\'\')
{
$_SESSION[\'msg\']=\"Please enter login details\";
header(\"Location:index.php\");
}
?>
<html>
<head>
<title>user home page</title>
</head>

<body bgcolor=\"#FFFFCC\">
Welcome USER <a href=\"logout.php\">Logout</a>
</body>
</html>

Create page logout.php

<?php
session_start();
if($_SESSION[\'username\']!=\'\' && $_SESSION[\'userpassword\']!=\'\')
{
unset($_SESSION[\'username\']);
unset($_SESSION[\'userpassword\']);
$_SESSION[\'msg\']=\"You are successfully logout\";
header(\"Location:index.php\");
}
?>

Database

 Database: `test`
 Table structure for table `users`


CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) NOT NULL,
  `userpassword` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;


 Dumping data for table `users`


INSERT INTO `users` (`id`, `username`, `userpassword`) VALUES
(1, \'test123\', \'my123\'),
(2, \'shishir06_2007\', \'123456\');

You can download source code here