here is a code for authentication system( user
registratoin and login functionality).
First of all set your database configuration in Db.php file, and then create table in database and Add all content in specified file name.
First you need to do the registration then after you can login in to the system. After
login you redirect on the page Welcome.php
here you can find Logout link for logout from the system.
In this I havn’t create index file so you need to go through
direct link.
Create Table
CREATE TABLE IF NOT EXISTS `login` (
`id` int(11) NOT
NULL AUTO_INCREMENT,
`firstname`
varchar(20) NOT NULL,
`lastname`
varchar(20) NOT NULL,
`gender` varchar(5)
NOT NULL,
`mobileno`
varchar(20) NOT NULL,
`email` varchar(50)
NOT NULL,
`password`
varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Create files specified file name Db.php, Registraton.php, Login.php, Welcome.php.
Copy and Past the code in the files and then run the example.
DB.php
Copy and Past the code in the files and then run the example.
DB.php
<?php
$conn =
mysql_connect("localhost","root","");
mysql_select_db("practice",$conn);
?>
Registration.php
<?php include('Db.php'); ?>
<html><head><title>Registration</title></head>
<style type="text/css">
.title { font-family: Arial, Helvetica, sans-serif; text-align:center;width:350px;
}
.tableDesign { font-size:16px;font-family:Arial, Helvetica,
sans-serif; width:350px; height:400px; }
table { background-color:#CCC; }
table input { height:25px;font-size:16px; }
table tr { height:15px; }
.errorMessage { color:red;text-align:center; }
</style>
<body>
<form id="form1" name="form1"
method="post" action="">
<label><div
class="title"><u>Registration
Form</u></div></label>
<table
style="" class="tableDesign">
<tr><td
colspan="2"><?php if(isset($_GET['err_message'])){ echo
"<div
class='errorMessage'>".$_GET['err_message'].'</div>'; }
?></td></tr>
<tr><td>First Name: </td><td><input
type="text" name="txtFirstName" /></td></tr>
<tr><td>Last Name: </td><td><input
type="text" name="txtLastName" /></td></tr>
<tr><td>Gender:</td><td><input
type="radio" name="txtGender" value="Male" />
Male <input type="radio"
name="txtGender" value="Female"
/>Female</td></tr>
<tr><td>Mobile No.: </td><td><input
type="text" name="txtMobileNo" /></td></tr>
<tr><td>E-mail: </td><td><input
type="text" name="txtEmail" /></td></tr>
<tr><td>Password: </td><td><input
type="password" name="txtPassword"
/></td></tr>
<tr><td> </td><td><input
type="submit" name="submit" value="Submit"
/></td></tr>
<tr><td> </td><td><a
href="Login.php">Back To Login</a></td></tr>
</table>
</form>
<?php if(isset($_POST['submit']))
{
$resultExists
= mysql_query("select count(*) userExists from login where email =
'".$_POST['txtEmail']."'")or die(mysql_error());
$rowExists
= mysql_fetch_array($resultExists);
if($rowExists["userExists"]
> 0){ header("Location:Registration.php?err_message=Email Already
Exists.!!!"); }
else {
$result
= mysql_query("insert into login(firstname, lastname, gender,
mobileno,email, password)
values('".$_POST['txtFirstName']."','".$_POST['txtLastName']."','".$_POST['txtGender']."','".$_POST['txtMobileNo']."','".$_POST['txtEmail']."','".md5($_POST['txtPassword'])."')")or
die(mysql_error());
$row_count
= mysql_affected_rows();
if($row_count
== 1){ header("Location:Login.php?message=Registration
Successfull.!!!"); }
else
{ echo "Not Insert...!!!"; }
}
}?>
</body>
</html>
Login.php
<?php include('Db.php'); session_start(); ?>
<html><head><title>Login</title></head>
<script type="text/javascript">
function validation() {
var f
=document.loginForm;
var error =
"Please Check Errors";
var flag =
true;
if(f.txtUserName.value == "") {
error +=
"\nEnter User Name";
flag = false;
}
if(f.txtPassword.value == "") {
error +=
"\nEnter Password";
flag = false;
}
if(flag == false)
{ alert(error); }
return flag;
}
</script>
<style type="text/css">
.title { font-family: Arial, Helvetica, sans-serif;
text-align:center;width:350px; }
.tableDesign { font-size:16px;font-family:Arial, Helvetica,
sans-serif; width:350px; height:150px; }
table { background-color:#CCC; }
table input { height:25px;font-size:16px; }
table tr { height:15px; }
.errorMessage { color:red;text-align:center; }
.message { color:green; }
</style>
<body>
<?php if(isset($_GET['action'])&& isset($_SESSION['userAuthenticated'])){
unset($_SESSION['userAuthenticated']); } ?>
<form name="loginForm" method="post"
onsubmit="return validation()">
<label><div
class="title"><u>Login</u>
</div></label>
<table class="tableDesign">
<tr><td
colspan="2"><?php if(isset($_GET['err_message'])){ echo
"<div
class='errorMessage'>".$_GET['err_message'].'</div>'; }
else if(isset($_GET['message'])){ echo
"<div class='message'>".$_GET['message'].'</div>'; } ?></td></tr>
<tr><td>UserName</td><td><input
type="text" name="txtUserName"></td></tr>
<tr><td>Password</td><td><input
type="password"
name="txtPassword"></td></tr>
<tr><td> </td><td><input
type="submit" name="submit"
value="submit"></td></tr>
<tr><td> </td><td><a href="Registration.php">Registration</a></td></tr>
</table>
</form><?php
if(isset($_POST['submit']))
{
$username
= $_POST['txtUserName'];
$password
= $_POST['txtPassword'];
$result
= mysql_query("select count(*) as authenticated from login where email =
'{$username}' and password = '".md5($password)."'")or
die(mysql_error());
$row =
mysql_fetch_array($result);
if(
$row["authenticated"] == 1){ $_SESSION['userAuthenticated'] =
$username; header("Location:Welcome.php?name='".$username."'");
}
else {
header("Location:Login.php?err_message=username or password is
wrong.!!!"); }
}?>
</body>
</html>
Welcome.php
<?php session_start();
if(!isset($_SESSION['userAuthenticated'])
&&$_SESSION['userAuthenticated'] = true)
{ echo header("Location:Login.php?err_message=First Do
The Login.!!!"); ; }
echo "Welcome : ";
if(isset($_GET['name'])){ echo $_GET['name']; }
?>
<a
href="Login.php?action=logout">Logout</a>

No comments:
Post a Comment