User Authentication using Database

Insert the details of users who register with the web site by using registration form. Authenticate the user when he submits the login form using the user name and password from the database.
STEPS:
    1. Create database in MYSQL
            create database onlineBookStore;
    2. Select the database
            use onlineBookStore;
    3. Create table
            create table reg(username varchar(30), password varchar(30), email varchar(50), contact bigint);
newUser.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Signup</title>
</head>
<body>
<form action="reg.php" method="post">
<h1 align="center"> REGISTRATION </h1>
<table align="center" cellspacing="10">
<tr>
<td>Name::</td>
<td> <input name="uname" type="text"> </td>
</tr>
<tr>
<td>Password::</td>
<td> <input name="pwd" type="password"> </td>
</tr>
<tr>
<td> E-mail ID:: </td>
<td> <input name="email" type="text"> </td>
</tr>
<tr>
<td> Phone Number:: </td>
<td> <input name="phno" type="number" maxlength="10"> </td>
</tr>
<tr>
<td colspan="2" align="center"> <input type="submit" value="Register">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="reset" value="Cancel"> </td>
</tr>
</table>
</form>
</body>
</html>
reg.php
<?php
//Accessing registration form details
$uname=$_POST["uname"];
$pass=$_POST["pwd"];
$email=$_POST["email"];
$mno=$_POST["phno"];
//Connection establishment
$conn=mysql_connect('localhost:3306','root','admin');
if(!$conn){
die("Connection Failed".mysql_error());
}
//Selecting database
$selectdb=mysql_select_db("onlineBookStore");
if(!$selectdb){
die("Database not found".mysql_error());
}
//Inserting new user details
$q="insert into reg values('".$uname."','".$pass."','".$email."',".$mno.");";
$insert=mysql_query($q);
mysql_close($conn);
//Redirect to login page when successfully register otherwise stay in same page
if(!$insert){
echo '<script>alert("Registration Failed."); window.location.href="newUser.html";</script>';
}
echo '<script>alert("Successfully registered."); window.location.href="login.html";</script>';
?>
login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login</title>
</head>
<body>
<h1 align="center">LOGIN</h1>
<form action="welcome.php" method="post">
<table align="center" cellspacing="10">
<tr>
<td><b>User ID</b></td>
<td><input name="uid" type="text"></td>
</tr>
<tr>
<td><b>Password</b></td>
<td><input name="pwd" type="password"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="Submit" value="Login"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="reset">
</td>
</tr>
<tr>
<td colspan="2" align="right"><i><a href="newUser.html">New User Click Here</a></i></td>
</tr>
</table>
</form>
</body>
</html>
welcome.php
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<?php
//Access login details
$uid=$_POST["uid"];
$pass=$_POST["pwd"];
//Connection establishment
$conn=mysql_connect('localhost:3306','root','admin');
if(!$conn){
die("Connection Failed".mysql_error());
}
//Selecting database
$selectdb=mysql_select_db("onlineBookStore");
if(!$selectdb){
die("Database not found".mysql_error());
}
//Executing query
$q="select * from reg;";
$retrecs=mysql_query($q);
//Retrieving records from register table and compare with login details
while ($rec=mysql_fetch_array($retrecs,MYSQL_ASSOC)) {
if ($rec["username"]==$uid && $rec["password"]==$pass) {
$flag=1;
break;
}
}
mysql_close($conn);
//Welcome message for valid details
if($flag==1){
echo "Welcome ".$uid;
}
//Warning message for invalid login details and redirect to login page
else{
echo '<script>alert("Invalid User ID/Password."); window.location.href="login.html";</script>';
}
?>
</body>
</html>

No comments:

Post a Comment

Total Pageviews