Login Page in PHP


In this article we will know how to do a simple login page using PHP. In this login page we use java script for validation of blank form. Here we use a Login PHP page where user can enter his/her credentials (Username and password) and after submitting the button action goes to the Login.PHP page where data is being checked in the database and the valid user name is shown as output in welcome.PHP page.When User enters wrong credentials (Username and password) then Invalid Username or Password will be displayed in the same page(Login.PHP).The connection between PHP scripts and mysql database is done in config.PHP file.

Table creation

Server: localhost Database: onlinetest
-- PHPMyAdmin SQL Dump
-- version 2.10.1
-- http://www.PHPmyadmin.net
--
-- Host: localhost
-- Generation Time: Nov 30, 2011 at 06:42 PM
-- Server version: 5.0.45
-- PHP Version: 5.2.5

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `onlinetest`
--

-- --------------------------------------------------------

--
-- Table structure for table `user`
--

CREATE TABLE `user` (
`name` varchar(255) NOT NULL,
`address` varchar(255) NOT NULL,
`area` varchar(255) NOT NULL,
`city` varchar(255) NOT NULL,
`pin` int(11) NOT NULL,
`state` varchar(255) NOT NULL,
`country` varchar(255) NOT NULL,
`mobile` int(20) NOT NULL,
`email` varchar(255) NOT NULL,
`user` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `user`
--

INSERT INTO `user` (`name`, `address`, `area`, `city`, `pin`, `state`, `country`, `mobile`, `email`, `user`, `password`) VALUES
('Satyapriya', 'Survey of India', 'R.R Laboratory', 'Bhubaneswar', 751013, 'Orissa', 'India', 2147483647, '[email protected]', 'Satyapriya', 'nayak');

config.PHP

<?PHP

/* Database Connection */

$sDbHost = 'localhost';
$sDbName = 'onlinetest';
$sDbUser = 'root';
$sDbPwd = '';

$dbConn = mysql_connect ($sDbHost, $sDbUser, $sDbPwd) or die ('MySQL connect failed. ' . mysql_error());
mysql_select_db($sDbName,$dbConn) or die('Cannot select database. ' . mysql_error());

?>

Login.PHP

<?PHP
session_start();
include("config.PHP");
if($_REQUEST["btn_login"]=="submit")
{
$uname=$_POST["txt_login"];
$password=$_POST["txt_pwd"];
$sql="select * from user where user='".$uname."' and password='".$password."'";
//echo $sql;
//exit;
$res=mysql_query($sql);
if($rs=mysql_num_rows($res)>0)
{
$_SESSION["uname"]=$uname;
//echo $_SESSION["uname"];
//exit;
//echo $rs;
//exit;
echo "<script>location.href='welcome.PHP'</script>";
}
else
{
echo "<script>location.href='Login.PHP?qs=invalid'</script>";
}
}
?>
<script language="javascript" type="text/javascript">
function validate()
{
if(document.frm_login.txt_login.value=="")
{
alert("please Enter your user name");
document.frm_login.txt_login.focus();
return false;
}
if(document.frm_login.txt_pwd.value=="")
{
alert("please Enter your password");
document.frm_login.txt_pwd.focus();
return false;
}
return true;
}
</script>
<form name="frm_login" method="POST" action="Login.PHP" onSubmit="return validate();">
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2" width="100%" align="center"><br><br><b><font size=3>LOGIN FORM</font></b>&nbsp;
<p>&nbsp;</p>
</tr>
<tr>
<td width="25%" align="center"><img src="img/key_img.jpg"></td>
<td width="75%" height="200">
<table border="0" width="100%" cellpadding="4">
<tr>
<td colspan="2" align="center"></td>
</tr>
<tr>
<td colspan="2" align="center"><font color="#EB3C37"></font></td>
</tr>
<tr>
<td colspan="2" align="center"><br></td>
</tr>
<?PHP

if($_REQUEST["qs"]=="invalid")
{
echo "<strong>Invalid Username or Password</strong>";
}
?>
<tr>
<td width="30%" align="right">Username:</td>
<td width="70%"><input type="text" name="txt_login"></td>
</tr>
<tr>
<td align="right">Password:</td>
<td><input type="password" name="txt_pwd"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="btn_login" value="submit"></td>
</tr>
</table>
<tr>
<td>&nbsp;</td>
</tr>
</td>
</tr>
</table>
</form>

welcome.PHP

<?PHP
session_start();
?>
<!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" />
<title>Welcome</title>
</head>

<body>
<form action="welcome.PHP" method="get" name="welcome">
<?PHP echo '<b>Welcome:</b>'.$_SESSION["uname"]; ?>
</form>
</body>
</html>

Running the application

Run the WampServer then write the below line in the URL
http://localhost/Login/

PHP1.gif

PHP2.gif

 


Similar Articles