User Authentication using Cookies

Assume four users user1, user2, user3 and user4 having the passwords pwd1, pwd2, pwd3 and pwd4 respectively. Write a PHP for doing the following.
1. Create a Cookie and add these four user id’s and passwords to this Cookie.
2. Read the user id and passwords entered in the Login form (week1) and authenticate with the values (user id and passwords) available in the cookies. If he is a valid user (i.e., user-name and password match) you should welcome him by name (user-name) else you should display "You are not an authenticated user".

Login.php
<?php
setcookie("uid1","user1");
setcookie("pwd1","pwd1");
setcookie("uid2","user2");
setcookie("pwd2","pwd2");
setcookie("uid3","user3");
setcookie("pwd3","pwd3");
setcookie("uid4","user4");
setcookie("pwd4","pwd4");
?>
<!DOCTYPE html>
<html>
<head>
<title>User Authentication</title>
</head>
<body>
<h1 align="center">LOGIN</h1>
<form action="Auth.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" name="submit" value="Login"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="reset" value="Clear">
</td>
</tr>
</table>
</form>
</body>
</html>

Auth.php
<!DOCTYPE html>
<html>
<head>
<title>User Authentication</title>
</head>
<body>
<?php

if ($_POST["uid"]==$_COOKIE["uid1"] && $_POST["pwd"]==$_COOKIE["pwd1"]) {
echo "Welcome ".$_POST["uid"];
}
else if ($_POST["uid"]==$_COOKIE["uid2"] && $_POST["pwd"]==$_COOKIE["pwd2"]) {
echo "Welcome ".$_POST["uid"];
}
else if ($_POST["uid"]==$_COOKIE["uid3"] && $_POST["pwd"]==$_COOKIE["pwd3"]) {
echo "Welcome ".$_POST["uid"];
}
else if ($_POST["uid"]==$_COOKIE["uid4"] && $_POST["pwd"]==$_COOKIE["pwd4"]) {
echo "Welcome ".$_POST["uid"];
}
else{
echo "You are not an authenticated user.";
}

?>
</body>
</html>

No comments:

Post a Comment

Total Pageviews