0% found this document useful (0 votes)
115 views11 pages

Program 8

The document describes an experiment to authenticate users by storing user IDs and passwords in a cookie. It includes: 1) Creating a cookie with four user/password pairs and adding it to the browser 2) Comparing the login form entries to the cookie values and welcoming the user by name if they match 3) Storing the user/password pairs in an ini file and accessing them using parse_ini_file() to authenticate with init parameters instead of a cookie.

Uploaded by

sri charan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views11 pages

Program 8

The document describes an experiment to authenticate users by storing user IDs and passwords in a cookie. It includes: 1) Creating a cookie with four user/password pairs and adding it to the browser 2) Comparing the login form entries to the cookie values and welcoming the user by name if they match 3) Storing the user/password pairs in an ini file and accessing them using parse_ini_file() to authenticate with init parameters instead of a cookie.

Uploaded by

sri charan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

EXPERIMENT- 8

User Authentication:
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”.
Use init-parameters to do this.

[Link]

<html>
<head>
<title>Login Page</title>
</head>
<body>
<center>
<h3> Login Page</h3>
<form name="f1" action="[Link]" method="post">
<table frame="border">
<tr>
<td>User</td>
<td><input type="text" name="user" size="20" value=""/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pwd" size="20" value=""/></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" value="Sign in"/>
<input type="reset" /></td>
</tr>
</table>
</form>
</body></center>
</html>

[Link]

<html>
<head>

</head>

<title>Set
Cookie</t
itle
<body>
<?php
$user = $_POST['user'];
$pwd = $_POST['pwd'];
setcookie("user",$user,time()+3600*2); setcookie("pwd",$pwd,time()
+3600*2);
echo "<h3>The Cookie Added...</h3>";
?>
</body>
</html>

[Link]

<html>
<head>
<title>Get Cookie</title>
</head>
<body>
<?php

$user = $_COOKIE['user'];
$pwd = $_COOKIE['pwd'];
if($user=="user1")
{
if($pwd == "pwd1")
echo "<h2>Welcome User1 </h2>";
else
echo "<h2>You are not an authenticated user.</h2>";
}
elseif($user=="user2")
{
if($pwd == "pwd2")
echo "<h2>Welcome User2</h2>";
else
echo "<h2>You are not an authenticated user.</h2>";
}
elseif($user=="user3")
{
if($pwd == "pwd3")
echo "<h2>Welcome User3</h2>";
else
echo "<h2>You are not an authenticated user.</h2>";
}
elseif($user=="user4")
{
if($pwd == "pwd4")
echo "<h2>Welcome User4</h2>";
else
echo "<h2>You are not an authenticated user.</h2>";
}
else
echo "<h2>Invalid Username/Password</h2>";
?>
</body>
</html>
OUTPUT 1 for Valid User:
OUTPUT 2 for Invalid User:
OUTPUT 3 for Non-existing user:
b) AIM: Use init-parameters to do the above task. Store the user-names and passwords in the
“[Link]” file and access them in php page by using the “parse_init_file()” method.

[Link]

<html>
<head>
<title>Login Page</title>
</head>
<body>
<center>
<h3> Login Page</h3>
<form name="f1" action="[Link]" method="post">
<table frame="border">
<tr>
<td>User</td>
<td><input type="text" name="user" size="20" value=""/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pwd" size="20" value=""/></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" value="Sign in"/>
<input type="reset" /></td>
</tr>
</table>
</form>
</body></center>
</html>

[Link]

;This is a users .ini file

[first_section]
u1=user1
p1=pwd1

[Link]

<html>
<head>
<title>Init Parameters</title>
</head>
<body>
<?php
$user = $_POST['user'];
$pwd = $_POST['pwd'];
$temp = parse_ini_file("[Link]");
foreach($temp as $value)
{
$users[] = $value;
}
if($user==$users[$i])
{
if($pwd==$users[$i+1])
echo "<h3>Welcome $users[$i]</h3>";
else
echo "<h3>You are not an authenticated user</h3>";
}
else
echo "<h3>Invalid Username/Password</h3>";
}
?>
</body>
</html>
OUTPUT for Valid user:
OUTPUT for Invalid user:

You might also like