.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> General >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Login Page in Php

Posted By:satyapriyanayak       Posted Date: January 22, 2014    Points: 200    Category: General    URL: http://www.dotnetspark.com  

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 wi
 

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, 'Satyapriya@gmail.com', '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/


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend