Login Page in Php

Satyapriyanayak
Posted by Satyapriyanayak under Others category on | Points: 40 | Views : 2173
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

Server: localhost Database: onlinetest
-- phpMyAdmin SQL Dump
-- version 2.10.1
-- http://www.phpmyadmin.net
-- Host: localhost
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`

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>

Comments or Responses

Login to post response