PHP Lab Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

1.

Implement the following web applications using (a) PHP


i. A user validation web application, where the user submits the login name
and password to the server. The name and password are checked against the
data already available in Database and if the data matches, a successful login
page is returned. Otherwise a failure message is shown to the user.
ii. Modify the above program to use and xml file instead of database.

Login.html

<html>

<head><title>Login page</title>

</head>

<body>

<center>

<h1>LOGIN FORM</h1>

<form action="verify_login.php" method="post">

<table>

<tr>

<td><label>USERNAME:</label></td>

<td><input type="text" name="uname"></td>

</tr>

<tr>

<td><label>PASSWORD:</label></td>

<td><input type="password" name="pwd"></td>

</tr>

<tr>

<td><input type="submit" value="LOGIN"></td>

<td><input type="reset" valuee="RESET"></td>

</tr>
</table>

</form>

</center>

</body>

</html>

verify_login.php

<?php

$myxml=simplexml_load_file("userlogin.xml");

$uname=$_REQUEST['uname'];

$pwd=$_REQUEST['pwd'];

$xmluname="";

$xmlpwd="";

for($i=0;$i<count($myxml);$i++)

{$xmluname=$myxml->user[$i]->uname;

$xmlpwd=$myxml->user[$i]->pwd;

if($xmluname==$uname&&$xmlpwd==$pwd)

{echo"WELCOME $uname!!you have succesfully logged in";

die();

}}

echo "doesnot exist";

?>

userlogin.xml

<?xml version="1.0" encoding="utf-8" ?>

<users><user>

<uname>vvvv</uname>

<pwd>123</pwd>
</user>

<user>

<uname>aaaa</uname>

<pwd>abcd</pwd>

</user>

<user>

<uname>hhhh</uname>

<pwd>xyz</pwd>

</user>

</users>

2. A simple calculator web application that takes two numbers and an


operator(+,-,/,*)rom an HTML page and returns the result page with the operation
performed on the operands.

Cal.html

<html>

<head>

<title>calculator</title></head>

<br><br><center><h1>Basic calculator</h1><br>

<form method="post" action="exec.php">

<input type="text" name="v1" placeholder="enter a value">

<input type="text" name="v2" placeholder="enter a value">

<input type="submit" name="add" value="+">

<input type="submit" name="sub" value="-">

<input type="submit" name="mul" value="*">

<input type="submit" name="div" value="/">

<input type="submit" name="mod" value="%">


</form>

</center>

</html>

Exec.php

<?php

$x=$_REQUEST['v1'];

$y=$_REQUEST['v2'];

if(isset($_POST['add']))

{echo '<h1> the result is '.($x+$y).'</h1>';

elseif(isset($_POST['sub']))

{echo '<h1> the result is '.($x-$y).'</h1>';

elseif(isset($_POST['mul']))

{echo '<h1> the result is '.($x*$y).'</h1>';

elseif(isset($_POST['div']))

{echo '<h1> the result is '.($x/$y).'</h1>';

elseif(isset($_POST['mod']))

{echo '<h1> the result is '.($x%$y).'</h1>';

}
?>

3. Modify the above program such that it stores each query in a database and checks the
database first for the result. If the query is already available in the DB, it returns the
value that was previously computed (from DB) or it computes the result and returns it
after sorting the new query and result in DB.

<html>

<head>

<title>calculator</title></head>

<br><br><center><h1>Basic calculator</h1><br>

<form method="post" action="insert.php">

<input type="text" name="v1" placeholder="enter a value">

<input type="text" name="v2" placeholder="enter a value">

<input type="submit" name="add" value="+">

<input type="submit" name="sub" value="-">

<input type="submit" name="mul" value="*">

<input type="submit" name="div" value="/">

<input type="submit" name="mod" value="%">

</form>

</center>

</html>

Insert.php

<?php

$x=$_REQUEST['v1'];

$y=$_REQUEST['v2'];

if(isset($_POST['add']))
{$op='+';

$res=($x+$y);

elseif(isset($_POST['sub']))

{ $op='-';

$res=($x-$y);

elseif(isset($_POST['mul']))

{ $op='*';

$res=($x*$y);

elseif(isset($_POST['div']))

{ $op='/';

$res=($x/$y);

elseif(isset($_POST['mod']))

{ $op='% ';

$res=($x%$y);

$con=mysqli_connect('localhost',"root","");

$db=mysqli_select_db($con,"veda") or die(mysqli_error());

$query=mysqli_query($con,"SELECT * from calci where v1='$x'and v2='$y' and oper='$op'");

$numrows=mysqli_num_rows($query);

if($numrows!=0)

{ while($row=mysqli_fetch_array($query))
{$val1=$row['v1'];

$val2=$row['v2'];

$oper=$row['oper'];

$result=$row['res'];

if($val1==$x&&$val2==$y&&$op==$oper)

{echo '<h1>Theresult is ','',$result,'<h1>';

}}

else

{ $write=mysqli_query($con,"insert into calci VALUES('$x','$y','$op','$res')");

echo '<h1>inserted and Theresult is ','',$result,'<h1>';

}?>

4. A web application takes a name as input and on submit it shows a hello<name> page
where <name>is taken from the request. It shows the start time at the right top corner
of the page and provides a logout button. On clicking this button, it should show a
logout page with Thank You <name> message with the duration of usage(hint: Use
session to store name and time).

Sess.html

<html>

<body><center><h1>LOGIN PAGE</h1><br><br>

<form action="sess.php" method="post">

enter your name:<input type="text" name="uname"><br><br>

<input type="submit" value="login" name="register">

</form></center>

</body>

</html>

Sess.php
<?php

session_start();

$name=$_POST['uname'];

$_SESSION['uname']=$name;

$time=time();

echo"<p align='right'>$time</p>";

echo "Hello".$name."!!!.You are logged in successfully";

$_SESSION['time']=time();

?>

<form action="log.php" method="post">

<input type="submit" value="logout" name="log">

</form>

Log.php

<?php

session_start();

$nowSince=time()-$_SESSION['time'];

$name=$_SESSION['uname'];

echo "<h1> thank you $name!!!</h1>"."the duration of your session is".$nowSince."seconds";

session_destroy();

?>

5. A web application that takes name and age from an HTML page. If the age is less than
18, it should send a page with “Hello <name>, you are not authorized to visit this site”
message, where <name>should be replaced with the entered name. Otherwise it should
send “Welcome <name> to this site” message.

Acces.html
<html>

<head>

<title>Acess</title></head>

<body>

<br><br><center><h1>User Eligibilty</h1><br>

<form method="post" action="check.php">

<input type="text" name="names" placeholder="enter your name">

<input type="text" name="age" placeholder="enter your age">

<input type="submit" name="sub" value="Submit">

</form>

</center>

</body>

</html>

Check.php

<?php

$age=$_REQUEST['age'];

$names=$_REQUEST['names'];

if($age<=18)

echo '<h1>Hello',' ',$names,' ',',you are not authorised to visit this site</h1>';

else

{echo'<h1>Welcome'.' '.$names.' '.', tothis site</h1>';

}?>
6. A web application that lists all cookies stores in the browser on clicking “List Cookies”
button. Add cookies if necessary.

Cookie_set.php

<?php

$exp=time()+86400;

setcookie("abc","123",$exp);

setcookie("def","456",$exp);

setcookie("aaa","bbb",$exp);

?>

Cookie.php

<center>

<br><br>

<form action="" method="post">

<input type="submit" value="list cookies" name="list">

</form>

</center>

<?php

error_reporting(0);

if($_POST['list'])

{foreach($_COOKIE as $key=>$val)

echo"<center>".$key."is at".$val."<br></center>";

?>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy