Download as pdf or txt
Download as pdf or txt
You are on page 1of 94

IWD 4340704

Practical 1

(i) Install and configure PHP, Web Server and MySQL database using
XAMPP/WAMP/LAMP/MAMP.
(ii) Create a web page that displays “Hello World.”

(i)
Step 1 : Downloading XAMPP on Windows

Click on first link

Click to Download the


latest version of
XAMPP.

Krina Amar Parikh 1 226040307065


IWD 4340704

Step 2 : Installing the XAMPP

Click here to Install

Click on Next

Click “NEXT” Until The


Final Installation
Starts

Krina Amar Parikh 2 226040307065


IWD 4340704

Step 3 : After Installation, Create a new folder in the htdocs folder in XAMPP

Create a New Folder

Step 4 : Open XAMPP Control Panel and Start the Apache Server

Start the
Apache
Server
Krina Amar Parikh 3 226040307065
IWD 4340704

Step 5 : Open localhost on Browser, by Default it will open dashboard

- Open localhost/Practicals* to run php files.


*Folder which we had created earlier in the htdocs folder in XAMPP.

Krina Amar Parikh 4 226040307065


IWD 4340704

(ii)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=devicewidth,initialscale=1.0">
<title>First php Page</title>
</head>
<body>
<?php
echo "Hello World";
?>
</body>
</html>
Input

Output

Hello World

Krina Amar Parikh 5 226040307065


IWD 4340704

Practical 2
Create a web page that collects user information using a form and displays it
when the user clicks the submit button
Input

Krina Amar Parikh 6 226040307065


IWD 4340704

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Web Page</title>
<style>
.myclass{

border-color: black;
max-width: 600px;
margin: 0 auto;
padding: 50px;
box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px ;
}
.form-group{
margin-block: 30px;
}
</style>
</head>
<body>
<div class="myclass">
<form action="" method="post">
<label for="fname"><b>FIRST NAME:</b></label>
<input type="text" name="name">

<br><label for="lname"><b>LAST NAME:</b></label>


<input type="text" name="lname">

<br><label for="address"><b>ADDRESS:</b></label>
<input type="text" name="address">

<br><label for="no"><b>CONTACT NO.:</b></label>


<input type="number" name="no">

Krina Amar Parikh 7 226040307065


IWD 4340704

<br><label for="gender"><b>GENDER:</b></label>
<input type="radio" id="Male" name="gender" value="Male">
<label for="Male"><b>Male</b></label>

<input type="radio" id="Female" name="gender" value="Female">


<label for="Female"><b>Female</b></label>

<input type="radio" id="Other" name="gender" value="Other">


<label for="Other"><b>Other</b></label>
<br><input type="submit" name="SUBMIT">
<b>
<br><?php $fname=$_POST['name'];?>
<?php $lname=$_POST['lname'];?>
<?php echo $fname;?>&nbsp;<?php echo $lname;?>
<br><?php $add=$_POST['address']; ?>
<?php echo $add; ?>
<br><?php $phno=$_POST['no'];?>
<?php echo $phno; ?>

</b>
</div>
</form>
</body>
</html>

Krina Amar Parikh 8 226040307065


IWD 4340704

Output

Practical 3
(i) Write a script to implement a simple calculator for mathematical operations.

Krina Amar Parikh 9 226040307065


IWD 4340704

Input
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Calculator</title>
<?php $ans
= "";
if(isset($_POST['sub'])) {
$num1 = $_POST['fnum'];
$num2 = $_POST['snum'];
$op = $_POST['sub']; if($op
== '+'){
$ans = $num1 + $num2;
} else if($op == '-') {
$ans = $num1 - $num2;
} else if($op == 'x') {
$ans = $num1 * $num2;
} else if($op == '/') {
$ans = $num1 / $num2;
}
} ?>
</head>
<body>
<form action="" method="post">
<label for="firstNum">First Number: </label>
<input type="text" name="fnum"> <br><br>
<label for="secNum">Second Number: </label>
<input type="text" name="snum"> <br><br>
<input type="submit" name="sub" value="+">
<input type="submit" name="sub" value="-">
<input type="submit" name="sub" value="x">
<input type="submit" name="sub" value="/">
<br><br>
Result: <input type="text" value="<?php echo

Krina Amar Parikh 10 226040307065


IWD 4340704

$ans; ?>">
</form>
</body>
</html>

Output

Krina Amar Parikh 11 226040307065


IWD 4340704

(ii) A company has following payment scheme for their staff: a.


Net Salary = Gross Salary – Deduction
b. Gross Salary = Basic pay + DA + HRA + Medical
c. Deduction = Insurance + PF
Where, DA (Dearness Allowance) = 50% of Basic pay
HRA (House Rent Allowance) = 10% of Basic pay
Medical = 4% of Basic pay
Insurance = 7% of Gross salary
PF (Provident Fund) = 5% of Gross salary.

Krina Amar Parikh 12 226040307065


IWD 4340704

Input
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Salary Calculate</title>
<?php
$bs = $da = $hra = $ma = $gs = $ins = $pf = $de =
$ns = '';

if(isset($_POST['sub'])) {
$bs = $_POST['bs'];
$da = $bs * 0.50;
$hra = $bs * 0.10;
$ma = $bs * 0.04;
$gs = $bs + $da + $hra + $ma;

$ins = $gs * 0.07;


$pf = $gs * 0.05;
$de = $ins + $pf;

$ns = $gs - $de;


}
?>
</head>
<body>
<form action="" method="post">

Krina Amar Parikh 13 226040307065


IWD 4340704

Basic Salary <input type="text"


name="bs" required value="<?php echo $bs ?>">
<input type="submit" name="sub">
<br><br>
Dearness Allowance<input type="text" name="da"
value="<?php echo $da ?>">
<br><br>
Gross Salary <input type="text" name="gs"
value="<?php echo $gs ?>"> <br><br>
House Rent Allowance <input type="text"
name="hra" value="<?php echo $hra ?>">
<br><br>
Medical Allowance <input type="text" name="ma"
value="<?php echo $ma ?> ">
<br><br>
Insurance <input type="text" name="ins"
value="<?php echo $ins ?>">
<br><br>
Dedcution Salary <input type="text" name="de"
value="<?php echo $de ?>">
<br><br>
Provident Fund(PF) <input type="text" name="pf"
value="<?php echo $pf?>">
<br><br>
Net Salary <input type="text" value="<?php echo
$ns ?>">
</form>
</body>
</html>

Krina Amar Parikh 14 226040307065


IWD 4340704

Output

Krina Amar Parikh 15 226040307065


IWD 4340704

Practical 4

(i)Write a script that reads the name of the car and displays the name
of the company the car belongs to as per the below table:

Car Company
Safari,Nexon,Tigor,Tiago Tata
XUV700,XUV300,Bolero Mahindra
i20,Verna,Venue,Creta Hyundai
Swift,Alto,Baleno,Brezza Suzuki

Krina Amar Parikh 16 226040307065


IWD 4340704

Input
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<form action="" method="POST">
enter car name
<input type="text" name="car">
<input type="submit" name="submit">
<br>
<br>
<?php

$car=$_POST['car']; switch ($car) { case


($car=="safari" || $car=="nexon" || $car=="tigor" ||
$car=="tiago") :
echo "tata";
break;
case ($car=="i20" || $car=="verna" || $car=="creata" ||
$car=="venue") :
echo "Hyundai"; break; case ($car=="XUV700"
|| $car=="XUV300" || $car=="BOLERO") : echo "Mahindra";

Krina Amar Parikh 17 226040307065


IWD 4340704

break;
case ($car=="Swift" || $car=="Alto" || $car=="Baleno" ||
$car=="Brezza") :
echo "Suzuki";
break;
default: echo
"invalid car";
break;
}
?>
</form>
</body>
</html>

Output

Krina Amar Parikh 18 226040307065


IWD 4340704

(ii) Write a script to read the marks of 4 subjects and display the result as
per the below instructions

(a) Each of the four subjects is worth 100 marks.

(b) If a student gets less than 35 marks in any subject, then he/she will be
marked as FAIL, otherwise he/she will be marked as PASS.
The result contains the grade of each individual subject in tabular format as
per the above table.

Input

Krina Amar Parikh 19 226040307065


IWD 4340704

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>

<form action="xyz.php" method="POST">


ENTER MARKS FOR SUBJECT 1<input type="number" name="sub1">
<br>
ENTER MARKS FOR SUBJECT 2<input type="number" name="sub2">
<br>
ENTER MARKS FOR SUBJECT 3<input type="number" name="sub3">
<br>
ENTER MARKS FOR SUBJECT 4<input type="number" name="sub4">
<br>
<input type="submit" name="submit">
<br>
</form>
</body>
</html>
<<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>

Krina Amar Parikh 20 226040307065


IWD 4340704

</head>
<body>

<?php
if(isset($_POST['submit']))
{
$sub1=$_POST['sub1'];
$sub2=$_POST['sub2'];
$sub3=$_POST['sub3'];
$sub4=$_POST['sub4'];
switch (true) {
case($sub1>"100"):
$g1="invalid";
$r1="invalid";
break;
case ($sub1>="90"):
$g1="AA";
$r1="Pass";
break; case
($sub1>="80"):
$g1="AB";
$r1="Pass";
break; case
($sub1>="75"):
$g1="BB";
$r1="Pass";

Krina Amar Parikh 21 226040307065


IWD 4340704

break; case
($sub1>="70"):
$g1="BC";
$r1="Pass";
break; case
($sub1>="65"):
$g1="CC";
$r1="Pass";
break; case
($sub1>="60"):
$g1="CD";
$r1="Pass";
break; case
($sub1>="40"):
$g1="DD";
$r1="Pass";
break;

default:
$g1="FF";
$r1="FAIL";
break;
}
switch (true) {
case($sub2>"100"):
$g2="invalid";
$r2="invalid";
break; case
($sub2>="90"):
$g2="AA";
$r2="Pass";

Krina Amar Parikh 22 226040307065


IWD 4340704

break; case
($sub2>="80"):
$g2="AB";
$r2="Pass";
break; case
($sub2>="75"):
$g2="BB";
$r2="Pass";
break;
case ($sub2>="70"):
$g2="BC";
$r2="Pass";
break;
case ($sub2>="65"):
$g2="CC";
$r2="Pass";
break; case
($sub2>="60"):
$g2="CD";
$r2="Pass";
break; case
($sub2>="40"):
$g2="DD";
$r2="Pass";
break;

default:
$g2="FF";
$r2="FAIL";
break;
}

Krina Amar Parikh 23 226040307065


IWD 4340704

switch (true) {
case($sub3>"100"):
$g3="invalid";
$r3="invalid";
break; case
($sub3>="90"):
$g3="AA";
$r3="Pass";
break; case
($sub3>="80"):
$g3="AB";
$r3="Pass";
break; case
($sub3>="75"):
$g3="BB";
$r3="Pass";
break; case
($sub3>="70"):
$g3="BC";
$r3="Pass";
break; case
($sub3>="65"):
$g3="CC";
$r3="Pass";
break; case
($sub3>="60"):
$g3="CD";
$r3="Pass";
break; case
($sub3>="40"):

Krina Amar Parikh 24 226040307065


IWD 4340704

$g3="DD";
$r3="Pass";
break;

default:
$g3="FF";
$r3="FAIL";
break;
}
switch (true) {
case($sub4>"100"):
$g4="invalid";
$r4="invalid";
break; case
($sub4>="90"):
$g4="AA";
$r4="Pass";
break; case
($sub4>="80"):
$g4="AB";
$r4="Pass";
break; case
($sub4>="75"):
$g4="BB";
$r4="Pass";
break; case
($sub4>="70"):
$g4="BC";
$r4="Pass";
break; case
($sub4>="65"):

Krina Amar Parikh 25 226040307065


IWD 4340704

$g4="CC";
$r4="Pass";
break; case
($sub4>="60"):
$g4="CD";
$r4="Pass";
break; case
($sub4>="40"):
$g4="DD";
$r4="Pass";
break;

default:
$g4="FF";
$r4="FAIL";
break;
}
}
?>

<table border="5">
<th>Subject</th>
<th>grade</th>
<th>Result</th>
<tr>
<td>sub1</td>
<td><?php echo $g1;?></td>
<td><?php echo $r1;?></td>
</tr>
<tr> <td>sub2</td>

Krina Amar Parikh 26 226040307065


IWD 4340704

<td><?php echo $g2;?></td>


<td><?php echo $r2; ?></td>
</tr>

<tr>
<td>sub3</td>
<td><?php echo $g3;?></td>

Krina Amar Parikh 27 226040307065


IWD 4340704

<td><?php echo $r3; ?></td>


</tr>
<tr>
<td>sub4</td>
<td><?php echo $g4;?></td>
<td><?php echo $r4; ?></td>
</tr>
</table>
</body>
</html>

Output

Krina Amar Parikh 28 226040307065


IWD 4340704

(iii) Write a script to display Fibonacci numbers up to a given term

Input
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<form action="" method="POST">
ENTER NUMBER :<input type="number" name="no">
<input type="submit" name="submit">
<br>
<?php
if($_POST)
{
$f=$_POST['no'];
$f1=0;
$f2=1;

echo "fibonacci series upto $f:<br>";


for ($i=0;$i<$f;$i++)
{
$sum=$f1+$f2;
echo "$f1"."<br>";
$f1=$f2;
$f2=$sum;
}
}
?>
</form>
</body>
</html>

Krina Amar Parikh 29 226040307065


IWD 4340704

Output

Krina Amar Parikh 30 226040307065


IWD 4340704

(iv) Write a script to display a multiplication table for the given number.

Krina Amar Parikh 31 226040307065


IWD 4340704

Input
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multiplication Table</title>
<style>
table, th, td{
border: 1px solid black;
border-collapse: collapse;
padding: 5px;
}
</style>
</head>
<body>
<form action="" method="post">
Enter a Number: <input type="text" name="num">
<input type="submit" name="sub">
</form> <?php
if(isset($_POST['sub'])) {
$n = $_POST['num'];

echo "<table>";
for($i = 1; $i <= 10; $i++) {
echo "<tr>"; echo
"<td>$n x $i = "; echo
($n * $i) . "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
</body></html>

Krina Amar Parikh 32 226040307065


IWD 4340704

Output

Krina Amar Parikh 33 226040307065


IWD 4340704

Practical 5

(i) Write a script to calculate the length of a string and count the
number of words in the given string without using string functions. Input
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>String Length and Word Count</title>
</head>
<body>
<h1>Enter a string:</h1>
<form action="" method="post">
<input type="text" name="string" required>
<input type="submit" value="Calculate">
</form>

<?php if
(isset($_POST['string'])) {
$str = $_POST['string'];
$length = 0; $words
= 1; while
(isset($str[$length])) { if
($str[$length] == ' ') {
$words++;
}
$length++;
}

echo "<h2>Results</h2>";
echo "String length: $length<br>";
echo "Number of words: $words";
}
?>
</body>
</html>

Krina Amar Parikh 34 226040307065


IWD 4340704

Output

Krina Amar Parikh 35 226040307065


IWD 4340704

(ii) Write a script to sort a given indexed array.

Input

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SORTING OF ARRAY</title>
<?php
$result = " ";
$array = array();
if (isset($_POST['SUB'])) {
$array = array(10, 30, 20, 50, 40, 70, 60, 90, 80, 100);
sort($array);
$result = implode(", ", $array);
}
?>
</head>
<body>
<form action = "" method = "POST">
<div class="container">
<label for="org">ORIGINAL ARRAY</label>
<textarea name = "org"><?php echo "10, 30, 20, 50, 40, 70, 60, 90, 80,
100";?></textarea>
<button type="submit" name="SUB">SUBMIT</button>
</div>
<div class="container">
<label for = "result">RESULTANT ARRAY</label>
<textarea name = "result"><?php echo $result;?></textarea>
</div>
</form>
IWD 4340704
</body>
</html>

Krina Amar Parikh 28 226040307065

Krina Amar Parikh 37 226040307065


IWD 4340704

Output

Krina Amar Parikh 38 226040307065


IWD 4340704
(iii) Write a script to perform 3 x 3 matrix Multiplication.
Input
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=1.0">
<title>3 X 3 MATRIX MULTIPLICATION</title>
<?php
$res = " ";
if (isset($_POST['SUB1'])) {
$arr1 = $_POST['arr1'];
$arr2 = $_POST['arr2'];
$result = array(); for
($i = 0; $i <= 2; $i++) { for
($j = 0; $j <= 2; $j++) {
$result[$i][$j] = 0;
for ($k = 0; $k <= 2; $k++) {
$result[$i][$j] += $arr1[$i][$k] * $arr2[$k][$j];
}
}
}
echo "<table>";
for ($i = 0; $i <= 2; $i++) {
echo "<tr>";
for ($j = 0; $j <= 2; $j++) {
echo "<td><input type='text' value='" . $result[$i][$j] . "'
readonly></td>";
}
echo "</tr>";
}
echo "</table>";

$res = json_encode($result);
}
?>
</head>

Krina Amar Parikh 39 226040307065


IWD 4340704
<body>
<form action="" method="POST">
<div class="container">
<table>
<th>MATRIX - 01</th>
<tr>
<td><input type="text" name="arr1[0][0]" placeholder="10"
required></td>
<td><input type="text" name="arr1[0][1]" placeholder="20"
required></td>
<td><input type="text" name="arr1[0][2]" placeholder="30"
required></td>
</tr>

<tr>
<td><input type="text" name="arr1[1][0]" placeholder="10"
required></td>
<td><input type="text" name="arr1[1][1]" placeholder="20"
required></td>
<td><input type="text" name="arr1[1][2]" placeholder="30"
required></td>
</tr>

<tr>
<td><input type="text" name="arr1[2][0]" placeholder="10"
required></td>
<td><input type="text" name="arr1[2][1]" placeholder="20"
required></td>
<td><input type="text" name="arr1[2][2]" placeholder="30"
required></td>
</tr>
<tr>
<td>
<br>
<button type="submit" name="SUB1">SUBMIT</button>
</td>
</tr>
<th>MATRIX - 02</th>
<tr>
<td><input type="text" name="arr2[0][0]" placeholder="10"
Krina Amar Parikh 40 226040307065
IWD 4340704
required></td>
<td><input type="text" name="arr2[0][1]" placeholder="20"
required></td>
<td><input type="text" name="arr2[0][2]" placeholder="30"
required></td>
</tr>

<tr>
<td><input type="text" name="arr2[1][0]" placeholder="10"
required></td>
<td><input type="text" name="arr2[1][1]" placeholder="20"
required></td>
<td><input type="text" name="arr2[1][2]" placeholder="30"
required></td>
</tr>

<tr>
<td><input type="text" name="arr2[2][0]" placeholder="10"
required></td>
<td><input type="text" name="arr2[2][1]" placeholder="20"
required></td>
<td><input type="text" name="arr2[2][2]" placeholder="30"
required></td>
</tr>
<tr>
<td>
<br>
<button type="submit" name="SUB">SUBMIT</button>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

Krina Amar Parikh 41 226040307065


IWD 4340704
Output

(iv) Write a script to encode a given message into equivalent Morse code.

Krina Amar Parikh 42 226040307065


IWD 4340704
Input
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1">
<title>Morse Code</title>
</head>
<body>
<?php
$string = “KRINA";
$string_lower = strtolower($string);
$assoc_array = array(
"a"=>".-",
"b"=>"-...",
"c"=>"-.-.",
"d"=>"-..",
"e"=>".",
"f"=>"..-.",
"g"=>"--.",
"h"=>"....",
"i"=>"..",
"j"=>".---",
"k"=>"-.-",
"l"=>".-..",
"m"=>"--",
"n"=>"-.",
"o"=>"---",
"p"=>".--.",
"q"=>"--.-",
"r"=>".-.", "s"=>"...",
"t"=>"-",
"u"=>"..-",
"v"=>"...-",
"w"=>".--",
"x"=>"-..-",

Krina Amar Parikh 43 226040307065


IWD 4340704
"y"=>"-.--",
"z"=>"--..",
"0"=>"-----",
"1"=>".----",
"2"=>"..---",
"3"=>"...--",
"4"=>"....-",
"5"=>".....",
"6"=>"-....",
"7"=>"--...",
"8"=>"---..",
"9"=>"----.",
"."=>".-.-.-",
","=>"--..--",
"?"=>"..--..",
"/"=>"-..-.",
" "=>" ");
echo "KRINA";echo"<br/>";
for($i=0;$i<strlen($string_lower);$i++){ foreach($assoc_array
as $letter => $code){
if($letter == $string_lower[$i]){
echo "$code";echo "<br/>";
}
}
}
?>

</body>
</html>

Output

Krina Amar Parikh 44 226040307065


IWD 4340704
Practical 6

(i) Consider a currency system in which there are notes of 7


denominations, namely Rs. 1, Rs. 2, Rs. 5, Rs. 10, Rs. 20, Rs. 50 2 4
Introduction to Web Development Course Code: 4340704 GTU -
COGC-2021 Curriculum Page 4 of 13 and Rs. 100. Write a function
that computes the smallest number of notes that will combine for a
given amount of money.
Input
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=devicewidth, initial-scale=1">
<title>Smallest No of Notes</title>
</head>
<body>
<form method="post">
<label for="amount">Enter the amount in
Rs.</label>
<input type="number" name="amount" required>
<button type="submit">Calculate</button>
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$amount = $_POST["amount"];

function calculateNotes($amount) {
$denominations = [100, 50, 20, 10, 5, 2, 1];

echo "<h3>Smallest number of notes for Rs. $amount:</h3>";


echo "<ul>";

foreach ($denominations as $denomination) {


if ($amount >= $denomination) {
$noteCount = floor($amount / $denomination);
$amount = $amount % $denomination;

Krina Amar Parikh 45 226040307065


IWD 4340704

if ($noteCount > 0) { echo


"<li>Rs. $denomination notes:

$noteCount</li>";
}
}
}

echo "</ul>";
}

calculateNotes($amount);
}
?>

</body>
</html>

Output

(ii) Write scripts using string functions:


(A). to check if the given string is lowercase or not.
(b). to reverse the given string.
Krina Amar Parikh 46 226040307065
IWD 4340704
(c). to remove white spaces from the given string.
(d). to replace the given word from the given string.
Input
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Strings Functions</title>
</head>
<body>
<form action="" method="post">
Enter String:<input type="text" name="str">
<input type="submit" name="btn"><br>
<?php
$str=$_POST['str']; if(isset($_POST['str']))
{
str_replace(" ","",$str);
if(ctype_lower($str))
{
echo "true";
}
else
{
echo "false";
}
echo "<br>";
echo "String after reverse";
echo "<br>"; echo
strrev($str); echo "<br>";
echo "String after removing whitespaces"; echo
"<br>";
echo str_replace(" ","",$str);

Krina Amar Parikh 47 226040307065


IWD 4340704
echo "<br>";
echo "String before replace";
echo "<br>"; echo $str;
echo "<br>";
echo "String after replace";
echo "<br>";
echo str_replace('Dave', 'Prachi', $str);

?>

</form>

</body>
</html>

Output

(iii) Write scripts using math functions:


(A). to generate a random number between the given range. (B).
to display the binary, octal and hexadecimal of a given decimal
number.
Krina Amar Parikh 48 226040307065
IWD 4340704
(C). to display the sin, cos and tan of the given angle.
Input
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MATH FUNCTIONS</title>
</head>
<body>
<form action = "" method = "POST">
<label for = "n1">ENTER FIRST NUMBER</label>
<input type = "text" name = "n1" placeholder = "START RANGE"
required>
<br><br>
<label for = "n2">ENTER SECOND NUMBER</label>
<input type = "text" name = "n2" placeholder = "END RANGE" required>
<br><br>
<label for = "num">ENTER A NUMBER</label>
<input type = "text" name = "num" placeholder = "DECIMAL NUMBER
TO CONVERT" required>
<br><br>
<label for = "angle">ENTER AN ANGLE</label>
<input type = "text" name = "angle" placeholder = "EG: 80" required>
<br>
<br>
<button type="submit" name="SUB">SUBMIT</button>
</form>
</body> <?php
if (isset($_POST['SUB'])) {
$num1 = $_POST['n1'];$num2 = $_POST['n2'];$num =
$_POST['num'];
$angle = $_POST['angle'];
$random_num = rand($num1, $num2); echo "<input
type = 'text' name = 'random' value = 'RANDOM
NUMBER GENERATED IS: $random_num' readonly>"."<br>";

Krina Amar Parikh 49 226040307065


IWD 4340704

$num_bin = decbin($num); $num_oct = decoct($num); $num_hex =


dechex($num);
echo "<input type = 'text' name = 'decbin' value = 'BINARY VALUE
OF $num IS: $num_bin' readonly>"."<br>";
echo "<input type = 'text' name = 'decoct' value = 'OCTAL VALUE OF
$num IS: $num_oct' readonly>"."<br>";
echo "<input type = 'text' name = 'dechex' value = 'HEXADECIMAL
VALUE OF $num IS: $num_hex' readonly>"."<br>";

$angle_sin = sin($angle); $angle_cos = cos($angle); $angle_tan =


tan($angle);
echo "<input type = 'text' name = 'sin' value = 'SINE VALUE OF
$angle IS: $angle_sin' readonly>"."<br>";
echo "<input type = 'text' name = 'cos' value = 'OCTAL VALUE OF
$angle IS: $angle_cos' readonly>"."<br>";
echo "<input type = 'text' name = 'tan' value = 'HEXADECIMAL
VALUE OF $angle IS: $angle_tan' readonly>"."<br>";
}
?>
</html>

Output

(iv) Write a script to display the current date and time in different
formats.
Krina Amar Parikh 50 226040307065
IWD 4340704
Input
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=devicewidth, initial-
scale=1">
<title>Date Formats</title>
</head>
<body>
<?php
echo "Today's date is
".date("d/m/Y"); echo "<br>"; echo
date("l"); echo "<br>";
date_default_timezone_set("Asia/Calcutta");
echo "The time is " . date("h:i:sa");
?>

</body>
</html>

Output

Practical 7

(i) Write a script to:


(A). Define a class with constructor and destructor.
(b). Create an object of a class and access its public properties
and methods.

Krina Amar Parikh 51 226040307065


IWD 4340704
Input
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CONSTRUCTOR</title>
</head>
<body>
<?php
class person
{
function __construct()
{
echo "This is a ".__CLASS__." Constructor."."<br>";
}function __destruct()
echo "This is Destructor";
}
}
$obj = new person();
?>
</body>
</html>

Output

(b). Create an object of a class and access its public properties and methods.
Input

Krina Amar Parikh 52 226040307065


IWD 4340704
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Access Specifier</title>
</head>
<body>
<?php
class A{
public $enroll,$name;

function getdata(){ $enroll=100;


$name="Hello"; echo "My Name is "
. $name ."<br>" ; echo "Enrollment
no. is " . $enroll;
}
}
$obj = new A();
$obj->getdata();
?>
</body>
</html>

(ii) Write a script that uses the set attribute and get attribute
methods to access a class’s private attributes of a class.

Krina Amar Parikh 53 226040307065


IWD 4340704
Input
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SET AND GET ATTRIBUTES</title>
</head>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
class privateattribute{
private $user;
function setUser($user){
$this->user = $user;
}
function getUser(){
echo $this->user;
}
}
$use = new privateattribute();
$user = $_POST["userName"];
$use->setUser($user);
}
?>
<div class="container">
<h1>Accessing Private Variables Using functions</h1>
<form action="" method="post">
<div class="form-group">
<label for="ans">Enter a Name :</label>
<input type="text" name="userName" required><br><br>
<button type="submit">Submit</button><br><br>
<label for="ans">Your Name is :</label>
<input type="text" name="displayUser" value="<?php $use->getUser();
?>" readonly>
</div>
</form>

Krina Amar Parikh 54 226040307065


IWD 4340704
</body>
</html>

Output

(iii) Write a script to demonstrate single inheritance.


Input
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Single Level Inheritance</title>

Krina Amar Parikh 55 226040307065


IWD 4340704
</head>
<body>
<?php
class A{
function Data(){
echo "This is Class A" . "<br>";
}
}
class B extends A{ function Add(){
echo "This is Class B which inherits class A";
}

}
$obj = new B();
$obj->Data();
$obj->Add();
?>
</body>
</html>

Output

(iv) Write a script to demonstrate multiple inheritance.

Krina Amar Parikh 56 226040307065


IWD 4340704
Input
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,
initialscale=1.0">
<title>Multiple Inheritance</title>
</head>
<body>
<?php
class Apple{
public $name = "iPhone 13 Pro";
}

trait iPhone{
public $model = "A15 Bionic";
}

class Main extends Apple{


use iPhone;

function show(){
echo "<h4>Apple</h4> " . $this->name;
}

function display(){
echo "<h4>Apple Chip</h4> " . $this->model;
}
}

$obj = new Main();


$obj->show(); echo
"<br>"; $obj-
>display();
?>
</body>
</html>

Krina Amar Parikh 57 226040307065


IWD 4340704
Output

(v). Write a script to demonstrate multilevel inheritance.


Input
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Multi-Level Inheritance</title>
</head>
<body>
<?php
class A{

function getdata()
{
echo "This is method of class A <br>";
}
Krina Amar Parikh 58 226040307065
IWD 4340704
}
class B extends A{

function getdata1()
{
echo "This is method of class B which inherits class
A <br>";
}
}
class C extends B{

function getdata2()
{
echo "This is method of class C which inherits class
B <br>"; }

}
$ob = new C();
$ob->getdata();
$ob->getdata1();
$ob->getdata2();
?>
</body>
</html>

Output

Krina Amar Parikh 59 226040307065


IWD 4340704

(v) Write a script to demonstrate method overriding.


Input
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Method Overriding</title>
</head>
<body>
<?php
class A
{
function displayAns($a, $b)
{
$result=$a*$b;
echo "Multiplication of $a and $b is: " . $result;
}
}
class B
{
function displayAns($a, $b)
{
$result=$a+$b;
echo "Addition of $a and $b is: " . "<b>". $result;
}
}
$obj = new B();
$obj->displayAns(10, 15);
?>
</body>
</html>

Krina Amar Parikh 60 226040307065


IWD 4340704
Output

(vi) Write a script to demonstrate method overloading based on


the number of arguments.
Input
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Method Overloading</title>
</head>
<body>
<?php
class Vehicle {
const DEFAULT_WHEELS = 4;
function __call($name, $args){
if ($name == 'getWheels') {
switch(count($args)){
case 0: return Vehicle::DEFAULT_WHEELS;
case 1: return $args[0];
}
}
}
}

$car = new Vehicle();


echo "Car has <b>" . $car->getWheels() . "</b> wheels.<br/>";
$bike = new Vehicle();
echo "Bike has <b>" . $bike->getWheels(2) . "</b> wheels.";
?>

</body>
</html>

Krina Amar Parikh 61 226040307065


IWD 4340704
Output

(viii). Write a script to demonstrate a simple interface.


Input
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,
initialscale=1.0">
<title>Interface Use</title>
</head>
<body>
<?php
interface First {
function one();
}

interface Second {
function two();
}

class Main implements First, Second {


function one() {
echo "<b>PHP</b> is a ";
}
function two() {
echo "is a <i>Server-Side</i> Scripting Language";
}
}
$obj = new Main();
$obj->one();
$obj->two();
?>
</body>
</html>
Output

Krina Amar Parikh 62 226040307065


IWD 4340704
(ix). Write a script to demonstrate a simple abstract class.
Input
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SINGLE INHERITANCE</title>
</head>
<body>
<form action = "" method = "POST">
<div class="container">
<input type = "text" name = "msg" value = "SINGLE
INHERITANCE" readonly></div> <div class="container">
<label for = "num1">ENTER FIRST NUMBER</label>
<input type = "number" name = "num1" required>
<label for = "num2">ENTER SECOND NUMBER</label>
<input type = "number" name = "num2" required><br>

<button type = "submit" name = "submit">SUBMIT</button><br>


<?php
if(isset($_POST['submit'])){
class base{
public $num1;
public $num2;
function __construct($num1, $num2){
$this->num1 = $num1;
$this->num2 = $num2;
}
function add(){
$ans1 = $this->num1 + $this->num2;
echo "ADDITION: " . $ans1;
}
}
class child extends base{
function sub(){
$ans2 = $this->num1 - $this->num2;

Krina Amar Parikh 63 226040307065


IWD 4340704
echo "SUBTRACTION: " . $ans2;}}
$ob = new child($_POST['num1'], $_POST['num2']);
}
?>
<input type = 'text' value = '<?php $ob->add();?>' readonly>
<input type = 'text' value = '<?php $ob->sub();?>' readonly> </div>
</form>
</body>
</html>

Output

(x). Write a script to demonstrate cloning of objects.

Krina Amar Parikh 64 226040307065


IWD 4340704
Input
<?php

class MyClass {
public $name;

public function __construct($name){


$this->name = $name;
}

public function __clone() {


}
}

$obj1 = new MyClass("Original Object");

$obj2 = clone $obj1;

$obj2->name = "Cloned Object";

echo "Original Object Name: " . $obj1->name . "<br>";

echo "Cloned Object Name: " . $obj2->name;


?>

Output

Krina Amar Parikh 65 226040307065


IWD 4340704

PRACTICAL – 08
Aim: - A] Create a web page using a form to collect employee
information.
CODE: -
<!DOCTYPE html>
<html lang="en">
<head>
<title>Employee Information Form</title>
</head>
<body>
<h2>Employee Information Form</h2>
<form action="" method="POST">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name" required><br><br>

<label for="email">Email:</label><br>
<input type="email" id="email" name="email"
required><br><br>

<label for="designation">Designation:</label><br>
<input type="text" id="designation" name="designation"
required>
<br><br>
<label for="department">Department:</label><br>
<input type="text" id="department" name="department"
required>
<br><br>

<label for="salary">Salary:</label><br>
<input type="number" id="salary" name="salary"
required><br><br>
<input type="submit" name="sub" value="Submit">
</form>
<?php
if (isset($_POST['sub'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$designation = $_POST['designation'];
$department = $_POST['department'];
$salary = $_POST['salary'];

echo "<h2>Employee Information Submitted Successfully</h2>";


echo "<p>Name: $name</p>";
echo "<p>Email: $email</p>";
echo "<p>Designation: $designation</p>";
echo "<p>Department: $department</p>";

Krina Amar Parikh 66 226040307065


IWD 4340704
echo "<p>Salary: $salary</p>";
}
?>
</body>
</html>

OUTPUT: -

Name: Krina
Email: Krina@cysec.com
Designation: Team Lead
Department: Technology
Salary: 54000

Name: Krina
Email: Krina@cysec.com
Designation: Team Lead
Department: Technology
Salary: 54000

Krina Amar Parikh 67 226040307065


IWD 4340704
Aim: - B] Extend practical - 8(A) to validate user information
using regular expressions.

CODE: -
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Employee Information Form</title>
<style>
p{
color: red;
}
</style>
</head>
<body>
<h2>Employee Information Form</h2>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name"><br><br>
Email: <input type="text" name="email"><br><br>
Designation: <input type="text" name="designation"><br><br>
Department: <input type="text" name="department"><br><br>
Salary: <input type="text" name="salary"><br><br>
<input type="submit" name="submit" value="Submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$error = false;

$name = $_POST['name'];
$email = $_POST['email'];
$designation = $_POST['designation'];
$department = $_POST['department'];
$salary = $_POST['salary'];

if (!preg_match("/^[a-zA-Z ]*$/", $name)) {


echo "<p>Only letters and white space allowed in
name</p><br>";
$error = true;
}

if (!preg_match("/^[a-zA-Z ]*$/", $department)) {


echo "<p>Only letters and white spaces allowed in
Department</p> <<br>";
$error = true;
Krina Amar Parikh 68 226040307065
IWD 4340704
}

if (!preg_match("/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-
Z]{2,}$/", $email)) {
echo "<p>Invalid email format</p><br>";
$error = true;
}

if (!is_numeric($salary)) {
echo "<p>Salary must be a Number</p><br>";
$error = true;
}
if (!preg_match("/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-
Z]{2,}$/", $email)) {
echo "<p>Invalid email format</p><br>";
$error = true;
}

if (!is_numeric($salary)) {
echo "<p>Salary must be a Number</p><br>";
$error = true;
}

if (!preg_match("/^[a-zA-Z ]*$/", $designation)) {


echo "<p>Only letters and white spaces allowed in
Designation <br>";
$error = true;
}

if (!$error) {
echo "<h2>Employee Information Submitted
Successfully</h2>";
echo "<p>Name: $name</p>";
echo "<p>Email: $email</p>";
echo "<p>Designation: $designation</p>";
echo "<p>Department: $department</p>";
echo "<p>Salary: $salary</p>";
}
}
?>
</body>
</html>

Krina Amar Parikh 69 226040307065


IWD 4340704
OUTPUT: -

Name: Krina 5
Email: Krina13$gmail.com
Designation: Team Lead
Department: Technology
Salary: 54000ONLY

Krina Amar Parikh 70 226040307065


IWD 4340704
Aim: - C] Create two distinct web pages to demonstrate
information passing between them using URL - Get method.

CODE: -

PAGE-1
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page 1</title>
</head>
<body>
<h1>Page 1</h1>
<form action="page2.php" method="get">
<label for="name">Enter your name:</label>
<input type="text" id="name" name="name">
<br><br>
<label for="age">Enter your age:</label>
<input type="number" id="age" name="age">
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

PAGE-2
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page 2</title>
</head>
<body>
<h1>Page 2</h1>
<?php
// Retrieving data from URL using GET method
if(isset($_GET['name']) && isset($_GET['age'])){
$name = $_GET['name'];
$age = $_GET['age'];
echo "<p>Hello, $name! Your age is $age.</p>";
} else {
echo "<p>No data received.</p>";
}
?>
<p><a href="page1.php">Go back to Page 1</a></p></body></html>

Krina Amar Parikh 71 226040307065


IWD 4340704
OUTPUT:-

Enter your name: Krina


Enter your age: 17

Hello, Krina Your age is: 17.

Krina Amar Parikh 72 226040307065


IWD 4340704
PRACTICAL – 09

Aim: - A] Create web pages to demonstrate passing information


using Session.

CODE: -
PAGE-1
<?php
session_start();

$name = "Krina Parikh";


$email = "Krina@cysec.com";

$_SESSION["username"] = $name;
$_SESSION["email"] = $email;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page 1</title>
</head>
<body>
<h1>Session - Page 1</h1>
<p>Session variables have been set.</p>
<p><a href='session2.php'>Go to Page 2</a></p>
</body>
</html>

PAGE-2
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page 2</title>
</head>
<body>
<h1>Session - Page 2</h1>
<?php
echo "<p>Welcome, " . $_SESSION["username"] . "!</p>";
echo "<p>Your email is: " . $_SESSION["email"] . "</p>";
?>
<p><a href="session1.php">Go back to Page 1</a></p>
</body>
</html>

Krina Amar Parikh 73 226040307065


IWD 4340704
OUTPUT: -

Welcome, Krina Parikh


Your email is: Krina@cysec.com

Krina Amar Parikh 74 226040307065


IWD 4340704

Aim: - B] Write a script to demonstrate storing and retrieving


information from cookies.

CODE: -
PAGE-1
<!DOCTYPE html>
<html lang="en">
<head>
<title>Cookie Demo</title>
</head>
<body>
<h1>Cookie Demo</h1>
<form action="storeCookie.php" method="post">
<label for="username">Enter your username:</label>
<input type="text" id="username" name="username">
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

PAGE-2
<!DOCTYPE html>
<html lang="en">
<head>
<title>Cookie Demo - Retrieve</title>
</head>
<body>
<h1>Cookie Demo - Retrieve</h1>
<?php
// Check if the username cookie is set
if(isset($_COOKIE['username'])){
$username = $_COOKIE['username'];
echo "<p>Hello<i>, <h3><u>$username</h3></u></i> Your
username is retrieved from the cookie.</p>";
} else {
echo "<p>Username not found in cookie.</p>";
}
?>
</body>
</html>

PAGE-3
<?php
if(isset($_POST['username'])){
$username = $_POST['username'];
setcookie("username", $username, time() + (86400 * 30), "/");
Krina Amar Parikh 75 226040307065
IWD 4340704
header("Location: retrieveCookie.php");
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Cookie Demo - Stored</title>
</head>
<body>
<h1>Cookie Demo - Stored</h1>
<?php
if(isset($_COOKIE['username'])){
$username = $_COOKIE['username'];
echo "<p>Hello, $username! Your username is stored in a
cookie.</p>";
echo "<p><a href='retrieveCookie.php'>Retrieve username from
cookie</a></p>";
} else {
echo "<p>Username not stored in cookie.</p>";
}
?>
</body> /html>

OUTPUT: -

Enter your username: KrinaParikh1310

Krina Amar Parikh 76 226040307065


IWD 4340704

Hello, KrinaParikh1310 Your username is stored in a cookie.

KrinaParikh1310

Krina Amar Parikh 77 226040307065


IWD 4340704

PRACTICAL - 10
Aim: - A] Create a web page that reads employee information
using a form and stores it in the database.

CODE: -
<!DOCTYPE html>
<html lang="en">
<head>
<title>Add Employee</title>
<link
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/c
ss/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h2 class="mt-5 mb-4">Add Employee</h2>
<form action="" method="post">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control"
id="username" name="username" required>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-
control" id="password" name="password"
required>
</div>
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control"
id="name" name="name" required>
</div>
<div class="form-group">
<label
for="department">Department:</label>
<input type="text" class="form-control"
id="department" name="department"
required>
</div>
<div class="form-group">
Krina Amar Parikh 78 226040307065
IWD 4340704
<label for="salary">Salary:</label>
<input type="number" class="form-control"
id="salary" name="salary" required> </div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control"
id="email" name="email" required>
</div>
<button type="submit" style="margin-bottom: 10px;"
class="btn btn- primary">Submit</button>
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "krinadb";

// Create connection
$conn = new mysqli($servername, $username, $password,
$dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Get form data


$username = isset($_POST['username']) ?
$_POST['username'] : "";
$password = isset($_POST['password']) ?
$_POST['password'] : "";
$name = isset($_POST['name']) ? $_POST['name'] : "";
$department = isset($_POST['department']) ?
$_POST['department'] : "";
$salary = isset($_POST['salary']) ? $_POST['salary'] :
"";
$email = isset($_POST['email']) ? $_POST['email'] :
"";

Krina Amar Parikh 79 226040307065


IWD 4340704
// Insert data into database
$sql = "INSERT INTO employee (username, password,
name, department, salary, email)
VALUES ('$username', '$password', '$name',
'$department', '$salary', '$email')";

if ($conn->query($sql) === TRUE) {


echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
}
?>
</body>
</html>

OUTPUT: -

Krina1310

Krina1310

Lead

130100

Krina Amar Parikh 80 226040307065


IWD 4340704
Aim: - B] Create a web page for employee log-in.
CODE: -
<!DOCTYPE html>
<html lang="en">
<head>
<title>Login</title>
<link href =
"https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bo
otstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h2 class="mt-5 mb-4">Login</h2>
<form action="" method="post">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control"
id="username" name="username"
required>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-
control" id="password" name="password"
required>
</div>
<button type="submit" class="btn btn-primary"
name="submit">Login</button>
</form>
<?php
session_start();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "krinadb";

$conn = new mysqli($servername, $username, $password,


$dbname);
if ($conn->connect_error) {

Krina Amar Parikh 81 226040307065


IWD 4340704
die("Connection failed: " . $conn-
>connect_error);
}

$username = isset($_POST['username']) ?
$_POST['username'] : "";
$password = isset($_POST['password']) ?
$_POST['password'] : "";

$username = mysqli_real_escape_string($conn,
$username);
$password = mysqli_real_escape_string($conn,
$password);

$sql = "SELECT * FROM employee WHERE


username='$username' AND
password='$password'";
$result = $conn->query($sql);

if ($result->num_rows == 1) {
echo "<center><h2 class='text-
success'>Logged In
Successfully</center></h2>";
} else {
echo "<center><h2 class='text-
danger'>Invalid username or
password</h2></center>";
}
$conn->close();
}
?>
</div>
</body>
</html>

Krina Amar Parikh 82 226040307065


IWD 4340704
OUTPUT: -

Krina131
0

Logged In Successfully

Krina Amar Parikh 83 226040307065


IWD 4340704
Aim: - C] After an employee logs in, create a Home web page that
displays basic employee information.
CODE: -
<!DOCTYPE html>
<html lang="en">
<head>
<title>Employee Information</title>
<!-- Bootstrap CSS -->
<link
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/c
ss/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h2 class="mt-5 mb-4">Employee Information</h2>
<?php
if(isset($_GET['username'])) {
$username = $_GET['username'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "krinadb";
$conn = new mysqli($servername, $username,
$password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn-
>connect_error);
}
$sql = "SELECT * FROM employee WHERE
username='username'";
$result = $conn->query($sql);
if ($result->num_rows == 1) {
echo "<div class='table-responsive'>";
echo "<table class='table table-
bordered'>";
echo "<thead class='thead-dark'>";
echo "<tr>";
echo "<th>Username</th>";
echo "<th>Name</th>";
echo "<th>Department</th>";
echo "<th>Salary</th>";
echo "<th>Email</th>";
echo "</tr>";

Krina Amar Parikh 84 226040307065


IWD 4340704
echo "</thead>";
echo "<tbody>";
$row = $result->fetch_assoc();
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['department'] .
"</td>";
echo "<td>$" . $row['salary'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
echo "</tbody>";
echo "</table>";
echo "</div>";
} else {
echo "<p class='text-danger'>User
information not found</p>";
}

$conn->close();
} else {
// Redirect to login page if username
parameter is not set
header("Location: login.php");
exit(); // Ensure no more output is sent
}
?>
</div>
</body>
</html>

OUTPUT: -

Krina1310 Krina Parikh Lead 130100 Krinademo@bbit.ac.in

Krina Amar Parikh 85 226040307065


IWD 4340704
Aim: - D] Create a web page to delete employee profiles from the
database.
CODE: -
<!DOCTYPE html>
<html lang="en">
<head>
<title>Delete Employee Profile</title>
<link
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/c
ss/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h2 class="mt-5 mb-4">Delete Employee Profile</h2>
<form method="post">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control"
id="username" name="username" required>
</div>
<button type="submit" class="btn btn-danger"
name="submit">Delete Profile</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "krinadb";

$conn = new mysqli($servername, $username,


$password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn-
>connect_error);
}

$username = isset($_POST['username']) ?
$_POST['username'] : "";

$username = mysqli_real_escape_string($conn,
$username);

Krina Amar Parikh 86 226040307065


IWD 4340704
$sql = "DELETE FROM employee WHERE
username='$username'";
if ($conn->query($sql) === TRUE) {
echo "<div class='alert alert-success mt-
3' role='alert'>Employee profile
deleted successfully.</div>";
} else {
echo "<div class='alert alert-danger mt-3'
role='alert'>Error deleting profile: " .
$conn->error . "</div>";
}
$conn->close();
}
?>
</div>
</body>
</html>

OUTPUT: -

Krina1310

Krina Amar Parikh 87 226040307065


IWD 4340704
Aim: - D] Create a web page that allows employees to change their password.

CODE: -
<!DOCTYPE html>
<html lang="en">
<head>

<title>Change Password</title>
<link
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/c
ss/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h2 class="mt-5 mb-4">Change Password</h2>
<form method="post">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control"
id="username" name="username"
required>
</div>
<div class="form-group">
<label for="current_password">Current
Password:</label>
<input type="password" class="form-
control" id="current_password"
name="current_password" required>
</div>
<div class="form-group">
<label for="new_password">New
Password:</label>
<input type="password" class="form-
control" id="new_password" name="new_password"
required>
</div>
<button type="submit" class="btn btn-primary"
name="submit">Change Password</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$servername = "localhost";
$username = "root";

Krina Amar Parikh 88 226040307065


IWD 4340704
$password = "";
$dbname = "krinadb";

$conn = new mysqli($servername, $username,


$password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn-
>connect_error);
}
$username = isset($_POST['username']) ?
$_POST['username'] : "";
$current_password =
isset($_POST['current_password']) ?
$_POST['current_password'] : "";
$new_password = isset($_POST['new_password'])
? $_POST['new_password'] : "";
$username = mysqli_real_escape_string($conn,
$username);
$current_password =
mysqli_real_escape_string($conn,
$current_password);
$new_password =
mysqli_real_escape_string($conn, $new_password);
$sql = "SELECT * FROM employee WHERE
username='$username' AND
password='$current_password'";
$result = $conn->query($sql);
if ($result->num_rows == 1) {
$update_sql = "UPDATE employee SET
password='$new_password' WHERE
username='$username'";
if ($conn->query($update_sql) === TRUE) {
echo "<div class='alert alert-success
mt-3' role='alert'>Password
changed successfully.</div>";
} else {
echo "<div class='alert alert-danger
mt-3' role='alert'>Error changing
password: " . $conn->error . "</div>";
}
} else {

Krina Amar Parikh 89 226040307065


IWD 4340704
echo "<div class='alert alert-danger mt-3'
role='alert'>Incorrect
current password.</div>";
}
$conn->close();
}
?>
</div>
</body>
</html>

OUTPUT: -

Krina1310

Krina Amar Parikh 90 226040307065


IWD 4340704

PRACTICAL - 11
Aim: - Write a script to generate a salary slip for an employee in
PDF format.

CODE: -
<?php
require_once 'vendor/autoload.php';

use Dompdf\Dompdf;
use Dompdf\Options;

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "krinadb";

$conn = new mysqli($servername, $username, $password,


$dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM employee WHERE username='krina07'";


$result = $conn->query($sql);

if ($result->num_rows == 1) {
$row = $result->fetch_assoc();
$name = $row['name'];
$department = $row['department'];
$salary = $row['salary'];

function generatePDF($name, $department, $salary)


{
$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('isPhpEnabled', true);

$dompdf = new Dompdf($options);


$html = '<!DOCTYPE html>
<html lang="en">
Krina Amar Parikh 91 226040307065
IWD 4340704
<head>
<title>Salary Slip</title>
</head>
<body>
<div class="container">
<h2>Salary Slip</h2>
<div class="salary-details">
<p><strong>Name:</strong>
'.$name.'</p>
<p><strong>Department:</strong>
'.$department.'</p>
<p><strong>Salary:</strong>
$'.$salary.'</p>
</div>
</div>
</body>
</html>';

$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream("salary_slip.pdf",
array("Attachment" => false));
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Employee Information</title>
<link
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/c
ss/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h2 class="mt-5 mb-4">Employee Information</h2>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Department</th>
<th>Salary</th>
</tr>
</thead>
Krina Amar Parikh 92 226040307065
IWD 4340704
<tbody>
<th>Department</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $name; ?></td>
<td><?php echo $department; ?></td>
<td><?php echo $salary; ?></td>
</tr>
</tbody>
</table>
<form method="post">
<button type="submit" class="btn btn-primary"
name="generate">Generate PDF</button>
</form>
</div>
</body>
</html>

<?php
if (isset($_POST['generate'])) {
generatePDF($name, $department, $salary);
}
} else {
echo "Employee not found.";
}

$conn->close();
?>

Krina Amar Parikh 93 226040307065


IWD 4340704

OUTPUT: -

Krina Parikh
CSE 130100

Krina Parikh
CSE

130100

Krina Amar Parikh 94 226040307065

You might also like