0% found this document useful (0 votes)
32 views

Source Code Tarun

This document contains a practical file submitted by Tarun Kumar to Dr. Sumit Chauhan for the Web Technology Lab course. It includes 8 programs written in PHP with HTML: 1) A program to print the sum of two numbers, 2) A program to check if a number is prime, 3) A program to check if a number is even or odd, 4) A program to print the multiplication table of a number, 5) A program to print the factorial of a number, 6) A program to reverse a given number, 7) A program to reverse a given string, and 8) A program to swap two numbers with and without a third variable. The file demonstrates PHP programs for basic mathematical and string operations

Uploaded by

Swati
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Source Code Tarun

This document contains a practical file submitted by Tarun Kumar to Dr. Sumit Chauhan for the Web Technology Lab course. It includes 8 programs written in PHP with HTML: 1) A program to print the sum of two numbers, 2) A program to check if a number is prime, 3) A program to check if a number is even or odd, 4) A program to print the multiplication table of a number, 5) A program to print the factorial of a number, 6) A program to reverse a given number, 7) A program to reverse a given string, and 8) A program to swap two numbers with and without a third variable. The file demonstrates PHP programs for basic mathematical and string operations

Uploaded by

Swati
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

[DOCUMENT TITLE]

PRACTICAL FILE
OF
Web Technology Lab
(BCA-175)

SUBMITTED TO:
SUBMITTED BY:
Tarun Kumar
Dr. Sumit Chauhan
Associate Professor(IT)

Course:BCA(2022-2025)

TARUN KUMAR 1
[DOCUMENT TITLE]

PRATICAL NO. 1

PHP program to print sum of digits.

<!DOCTYPE html>
<html lang="en">
<head>
<link rel=stylesheet href="php.css">
<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>Php Website</title>
</head>
<body><div class="container">
<center>
<h2><hr>Addition Program<hr></h2></center></div>
<form method="post" action="<?php echo
$_SERVER['PHP_SELF'];?>">

TARUN KUMAR 2
[DOCUMENT TITLE]

<center>
Enter the first number:
<input type="number" name="num1"><br>
<br>
Enter the secound number :
<input type="number" name="num2"><br>
<br>
<input type="submit" name="submit"><br>
<br>
</center>
</form>
<center>
<?php
if(isset($_POST['submit'])){
$num1=$_POST['num1'];
$num2=$_POST['num2'];
$sum=$num1+$num2;
echo"Your Result is $sum";
}
?>

TARUN KUMAR 3
[DOCUMENT TITLE]

<br>
</center>
</body>
</html>

TARUN KUMAR 4
[DOCUMENT TITLE]

PRATICAL NO. 2

PHP program to check prime number.

<!DOCTYPE html>
<html lang="en">
<head>
<link rel=stylesheet href="php.css">
<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>Php Website</title>
</head>
<body><div class="container">
<center>
<h2><hr>Finding
PrimeNumber<hr></h2></center></div>

TARUN KUMAR 5
[DOCUMENT TITLE]

<form method="post" action="<?php echo


$_SERVER['PHP_SELF'];?>">
<center>
Enter the number:
<input type="number" name="input"/><br>
<br>
<input type="submit" name="submit"/><br>
<br>
</center>
</form>
<center>
<?php
if(isset($_POST['submit'])){
echo checkprime($_POST["input"]);
}
function checkprime($input){
$flag=0;
$input=$_POST['input'];
for($i=2;$i<$input;$i++){

TARUN KUMAR 6
[DOCUMENT TITLE]

if($input%$i==0){
$flag=1;
break;
}}
if($flag==1){
return "$input is not a prime number";
}
else{
return "$input is a prime number";}}
?>
<br>
</center>
</body>
</html>

TARUN KUMAR 7
[DOCUMENT TITLE]

PRATICAL NO. 3

PHP program to check even or odd number.

<!DOCTYPE html>
<html lang="en">
<head>
<link rel=stylesheet href="php.css">
<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>Php Website</title>
</head>
<body><div class="container">
<center>
<h2><hr>Finding Odd or Even
Number<hr></h2></center></div>
<form method="post" action="<?php echo
$_SERVER['PHP_SELF'];?>">
TARUN KUMAR 8
[DOCUMENT TITLE]

<center>
Enter the number:
<input type="number" name="input"/><br>
<br>
<input type="submit" name="submit"/><br>
<br>
</center>
</form>
<center>
<?php
if(isset($_POST['submit'])){
$input = $_POST['input'];
if($input%2==0){
echo "$input is a Even number";
}
else{
echo "$input is Odd number";
}}
?>
<br>

TARUN KUMAR 9
[DOCUMENT TITLE]

</center>
</body>
</html>

TARUN KUMAR 10
[DOCUMENT TITLE]

PRATICAL NO. 4

PHP program to print table of a number.

<!DOCTYPE html>
<html lang="en">
<head>
<link rel=stylesheet href="php.css">
<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>Php Website</title>
</head>
<body><div class="container">
<center>
<h2><hr>Multiplication Table of a
Number<hr></h2></center></div>

TARUN KUMAR 11
[DOCUMENT TITLE]

<form method="post" action="<?php echo


$_SERVER['PHP_SELF'];?>">
<center>
Enter the number:
<input type="number" name="input"/><br>
<br>

<input type="submit" name="submit"/><br>


<br>
</center>
</form>
<center>
<?php
if(isset($_POST["submit"])){
echo Multiplication($_POST["input"]);
}
function Multiplication($input){
$input = $_POST['input'];
for($i=1;$i<=10;$i++)
{ $sum = $i*$input;

TARUN KUMAR 12
[DOCUMENT TITLE]

echo "<br>";
echo "$i X $input = $sum";
}
?>
<br>
</center>
</body>
</html>

TARUN KUMAR 13
[DOCUMENT TITLE]

PRATICAL NO. 5

PHP program to print factorial of a number.

<!DOCTYPE html>
<html lang="en">
<head>
<link rel=stylesheet href="php.css">
<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>Php Website</title>
</head>
<body><div class="container">
<center>
<h2><hr>Factorial of a
Number<hr></h2></center></div>

TARUN KUMAR 14
[DOCUMENT TITLE]

<form method="post" action="<?php echo


$_SERVER['PHP_SELF'];?>">
<center>
Enter the number:
<input type="number" name="input"/><br>
<br>

<input type="submit" name="submit"/><br>


<br>
</center>
</form>
<center>
<?php
if(isset($_POST['submit'])){
$input = $_POST['input'];
$fact=1;
for($i=1;$i<=$input;$i++){
$fact =$fact*$i;
echo "Factorial = $fact";

TARUN KUMAR 15
[DOCUMENT TITLE]

}
?>
<br>
</center>
</body>
</html>

TARUN KUMAR 16
[DOCUMENT TITLE]

PRATICAL NO. 6

PHP program to reverse given number.

<!DOCTYPE html>
<html lang="en">
<head>
<link rel=stylesheet href="php.css">
<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>Php Website</title>
</head>
<body><div class="container">
<center>
<h2><hr>Reverse of a
Number<hr></h2></center></div>

TARUN KUMAR 17
[DOCUMENT TITLE]

<form method="post" action="<?php echo


$_SERVER['PHP_SELF'];?>">
<center>
Enter the number:
<input type="number" name="input"/><br>
<br>
<input type="submit" name="submit"/><br>
<br>
</center>
</form>
<center>
<?php
if(isset($_POST['submit'])){
$input=$_POST['input'];
$result=0;
while($input>1){
$rev=$input%10;
$result = ($result*10)+$rev;
$input=($input/10);}

TARUN KUMAR 18
[DOCUMENT TITLE]

echo "$result";
}
?>
<br>
</center>
</body>
</html>

TARUN KUMAR 19
[DOCUMENT TITLE]

PRATICAL NO. 7

PHP program to reverse given string.

<!DOCTYPE html>
<html lang="en">
<head>
<link rel=stylesheet href="php.css">
<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>Php Website</title>
</head>
<body><div class="container">
<center>
<h2><hr>Reverse of a
String<hr></h2></center></div>
<form method="post" action="<?php echo
$_SERVER['PHP_SELF'];?>">
<center>
TARUN KUMAR 20
[DOCUMENT TITLE]

Enter your text:


<input type="text" name="string"/><br>
<br>
<input type="submit" name="submit"/><br>
<br>
</center>
</form>
<center>
<?php
if(isset($_POST['submit'])){
$string = $_POST["string"];
echo "Reverse is --" .strrev("$string");}
?>
<br></center>
</body>
</html>

TARUN KUMAR 21
[DOCUMENT TITLE]

PRATICAL NO. 8

PHP program to swap two numbers with and without


using third variable.

WITH THIRD VARIABLE

<!DOCTYPE html>
<html lang="en">
<head>
<link rel=stylesheet href="php.css">
<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>Php Website</title>
</head>
<body><div class="container">
<center>

TARUN KUMAR 22
[DOCUMENT TITLE]

<h2><hr>Swaping Number With Third Variable


<hr></h2></center></div>
<form method="post" action="<?php echo
$_SERVER['PHP_SELF'];?>">
<center>
Enter the number:
<input type="number" name="input1"/><br>
<br>
Enter the number:
<input type="number" name="input2"/><br>
<br>
<input type="submit" name="submit"/><br>
<br>
</center>
</form>
<center>
<?php
if(isset($_POST['submit'])){
$input1 = $_POST['input1'];
$input2 = $_POST['input2'];
$temp=$input1;

TARUN KUMAR 23
[DOCUMENT TITLE]

$input1=$input2;
$input2=$temp;
echo "Swaping Is Done Input 1= $input1 And
Input2=$input2";
}
?>
<br>
</center>
</body>
</html>

WITHOUT THIRD VARIABLE

<!DOCTYPE html>
<html lang="en">
<head>
<link rel=stylesheet href="php.css">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">

TARUN KUMAR 24
[DOCUMENT TITLE]

<meta name="viewport" content="width=device-


width, initial-scale=1.0">
<title>Php Website</title>
</head>
<body><div class="container">
<center>
<h2><hr>Swaping Number WithOut Third Variable
<hr></h2></center></div>
<form method="post" action="<?php echo
$_SERVER['PHP_SELF'];?>">
<center>
Enter the number:
<input type="number" name="input1"/><br>
<br>
Enter the number:
<input type="number" name="input2"/><br>
<br>
<input type="submit" name="submit"/><br>
<br>
</center>
</form>
<center>
<?php
TARUN KUMAR 25
[DOCUMENT TITLE]

if(isset($_POST['submit'])){
$input1 = $_POST['input1'];
$input2 = $_POST['input2'];
$input1=$input1+$input2;
$input2=$input1-$input2;
$input1=$input1-$input2;
echo "Swaping Is Done Input 1= $input1 And
Input2=$input2";
}

?>

<br>
</center>
</body>
</html>

TARUN KUMAR 26
[DOCUMENT TITLE]

PRATICAL NO. 9

PHP program to add, subtract, multiply & divide two

numbers using functions.

<!DOCTYPE html>
<html lang="en">
<head>
<link rel=stylesheet href="php.css">
<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>Php Website</title>
</head>
<body><div class="container">
<center>
<h2><hr>Addition,Subtract,Divide,Multiply
<hr></h2></center></div>

TARUN KUMAR 27
[DOCUMENT TITLE]

<form method="post" action="<?php echo


$_SERVER['PHP_SELF'];?>">
<center>
Enter the number:
<input type="number" name="input1"/><br>
<br>
Enter the number:
<input type="number" name="input2"/><br>
<br>
<input type="submit" name="submit"/><br>
<br>
</center>
</form>
<center>
<?php
if(isset($_POST['submit'])){
$input1=$_POST["input1"];
$input2=$_POST["input2"];

function add($input1,$input2){
return $input1+$input2;

TARUN KUMAR 28
[DOCUMENT TITLE]

}
function subtract($input1,$input2){
return $input1-$input2;
}
function multiply($input1,$input2){
return $input1*$input2;
}
function divide($input1,$input2){
return $input1/$input2;
}
{
echo "*The sum of your number $input1 and
$input2 is =" .add($input1,$input2);
echo"<br>";
echo "*The difference of your number $input1 and
$input2 is =" .subtract($input1,$input2);
echo"<br>";
echo "*The product of your number $input1 and
$input2 is =" .multiply($input1,$input2);
echo"<br>";
echo "*The quotient of your number $input1 and
$input2 is =" .divide($input1,$input2);

TARUN KUMAR 29
[DOCUMENT TITLE]

echo"<br>";
}
}

?>

<br>
</center>
</body>
</html>

TARUN KUMAR 30
[DOCUMENT TITLE]

PRATICAL NO. 10

Program to show the usage of SESSION variable

<!DOCTYPE html>
<html lang="en">
<head>
<link rel=stylesheet href="php.css">
<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>Php Website</title>
</head>
<body><div class="container">
<center>
<h2><hr>Usage Of Session
Variable<hr></h2></center></div>
<center>

TARUN KUMAR 31
[DOCUMENT TITLE]

<form method="post" action="<?php echo


$_SERVER['PHP_SELF'];?>">
<center>
<br>
Enter your name :
<input type="text" name="input1"/><br>
<br>
<input type="submit" name="submit"
value="submit"/><br>
<br>
</center>
</form>
<?php
session_start();
$_SESSION["username"]="tarun";
if(isset($_POST['submit'])){
$input1=$_POST["input1"];
if($input1=='tarun'){
header('location:session.php');
die();
}

TARUN KUMAR 32
[DOCUMENT TITLE]

}
?>
<br>
</center>
</body>
</html>

TARUN KUMAR 33
[DOCUMENT TITLE]

PRATICAL NO. 11

Program to show the usage of $_POST , $_GET and

$_REQUEST method.

GET METHOD

<!DOCTYPE html>
<html lang="en">
<head>
<link rel=stylesheet href="php.css">
<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>Php Website</title>
</head>
<body><div class="container">
<center>

TARUN KUMAR 34
[DOCUMENT TITLE]

<h2><hr>Usage Of Get
Method<hr></h2></center></div>
<center>
<form method="get" action="<?php echo
$_SERVER['PHP_SELF'];?>">
<center>
<br>
Enter your name :
<input type="text" name="input1"/><br><br>
Enter your age :
<input type="age" name="input2"/><br><br>
<input type="submit" name="submit"/><br>
<br>
</center>
</form>
Welcome:<?php echo $_GET["input1"];?><br>
Your age is:<?php echo $_GET["input2"];?><br>
<br>
</center>
</body>
</html>

TARUN KUMAR 35
[DOCUMENT TITLE]

POST METHOD

<!DOCTYPE html>
<html lang="en">
<head>
<link rel=stylesheet href="php.css">
<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>Php Website</title>
</head>
<body><div class="container">
<center>
<h2><hr>Usage Of Post
Method<hr></h2></center></div>
<center>
<form method="post" action="<?php echo
$_SERVER['PHP_SELF'];?>">
<center>

TARUN KUMAR 36
[DOCUMENT TITLE]

<br>
Enter your name :
<input type="text" name="input1"/><br><br>
Enter your age :
<input type="age" name="input2"/><br><br>
<input type="submit" name="submit"/><br>
<br>
</center>
</form>
Welcome:<?php echo $_POST["input1"];?><br>
Your age is:<?php echo $_POST["input2"];?><br>
<br>
</center>
</body>
</html>

REQUEST METHOD

<!DOCTYPE html>
<html lang="en">
<head>

TARUN KUMAR 37
[DOCUMENT TITLE]

<link rel=stylesheet href="php.css">


<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>Php Website</title>
</head>
<body><div class="container">
<center>
<h2><hr>Usage Of Request
Method<hr></h2></center></div>
<center>
<form method="post" action="<?php echo
$_SERVER['PHP_SELF'];?>">
<center>
<br>
Enter your name :
<input type="text" name="input1"/><br><br>
Enter your age :
<input type="age" name="input2"/><br><br>
<input type="submit" name="submit"/><br>

TARUN KUMAR 38
[DOCUMENT TITLE]

<br>
</center>
</form>
<?php
if($_SERVER['REQUEST_METHOD']=="POST"){
$input1=$_POST['input1'];
$input2=$_POST['input2'];
echo "Input Collected Sucessfully";
echo"<br>";
echo "Name: $input1";
echo "Age: $input2";
}
?>
<br>
</center>
</body>
</html>

TARUN KUMAR 39
[DOCUMENT TITLE]

PRATICAL NO. 12

login form and validate it through PHP programming.

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="form.css">
<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>login</title>
</head>
<body>
<form>
<h2>Login</h2>
<?php
if(isset($_GET['error'])){?>
<p class="error"><?php echo $_GET['error'];?></p>
TARUN KUMAR 40
[DOCUMENT TITLE]

<?php } ?>
<label>Use Name</label>
<input type="text" name="uname" placeholder="Enter
Name"><br>
<label>Password</label>
<input type="password" name="pass"
placeholder="Enter Password"><br>
<button type="submit">Login</button>
</form>
</body>
</html>

TARUN KUMAR 41
[DOCUMENT TITLE]

PRATICAL NO. 13

Program to create, open and read from a file using PHP.

<?php
$file=fopen("example.txt","w")or die("unable to open
filr");
$txt="Hello";
fwrite($file,$txt);
fclose($file);
$file=fopen("example.txt","r")or die ("unable to open
file");
echo fread($file,filesize("example.txt"));
fclose($file);
?>

TARUN KUMAR 42
[DOCUMENT TITLE]

PRATICAL NO. 14

Program to read & display the content from already

created file using PHP.

<?php
$file="sample.txt";
if(file_exists($file)&& is_readable($file)){
$handel=fopen($file,"r");
$contents=fread($handel,filesize($file));
echo $contents;
fclose($handel);
}
else{
echo "File is not readable";
}
?>

TARUN KUMAR 43
[DOCUMENT TITLE]

PRATICAL NO. 15

Program to modify the content of existing file.

<?php
$file_path = 'sample.txt';
$content_to_add = "\nExample of modify the content of
existing file";
$file_handle = fopen($file_path, 'r+');
fseek($file_handle, 0, SEEK_END);
fwrite($file_handle, $content_to_add);
fclose($file_handle);
echo "content modified sucessfully";
?>

TARUN KUMAR 44
[DOCUMENT TITLE]

PRATICAL NO. 16

Design a form which upload and display image in

PHP.

<!DOCTYPE html>
<html>

<head>
<title>Image Upload Form</title>
</head>

<body>
<h1>Upload an Image</h1>
<form action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]); ?>"
method="post" enctype="multipart/form-data">
<label for="image">Choose an image to
upload:</label>
<input type="file" name="image" id="image">
<br>
<input type="submit" name="submit"
value="Upload">
</form>

TARUN KUMAR 45
[DOCUMENT TITLE]

<?php

if (isset($_POST["submit"])) {

if (isset($_FILES["image"])) {

if ($_FILES["image"]["error"] == 0) {

if (!file_exists("uploads")) {
mkdir("uploads");
}

$target_dir = "uploads/";
$target_file = $target_dir .
basename($_FILES["image"]["name"]);
move_uploaded_file($_FILES["image"]["tmp_nam
e"], $target_file);

echo "<h2>Your Uploaded Image:</h2>";


echo "<img src='$target_file' alt='uploaded
image'>";
} else {

TARUN KUMAR 46
[DOCUMENT TITLE]

echo "There was an error uploading the file. Error


code: " . $_FILES["image"]["error"];
}
}
}
?>
</body>

</html>

TARUN KUMAR 47
[DOCUMENT TITLE]

PRATICAL NO. 17

Program which provides uploading and downloading of

a file.

<!DOCTYPE html>
<html>
<head>
<title>File Upload and Download</title>
</head>
<body>
<h1>File Upload and Download</h1>
<form action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]); ?>"
method="post" enctype="multipart/form-data">
<label for="file">Choose a file to upload:</label>
<input type="file" name="file" id="file">
<br>
<input type="submit" name="submit" value="Upload">
</form>

TARUN KUMAR 48
[DOCUMENT TITLE]

<?php
// Check if the form was submitted
if(isset($_POST["submit"])) {

// Check if a file was uploaded


if(isset($_FILES["file"])) {

// Check if there was no error during the upload


if($_FILES["file"]["error"] == 0) {

// Create the uploads directory if it doesn't already exist


if (!file_exists("uploads")) {
mkdir("uploads");
}

// Save the file to the uploads directory


$target_dir = "uploads/";
$target_file = $target_dir .
basename($_FILES["file"]["name"]);

TARUN KUMAR 49
[DOCUMENT TITLE]

move_uploaded_file($_FILES["file"]["tmp_name"],
$target_file);

echo "<p>File uploaded successfully.</p>";


}
else {
echo "<p>There was an error uploading the file. Error
code: " . $_FILES["file"]["error"] . "</p>";
}
}
}

// List the uploaded files


$files = scandir("uploads");
echo "<h2>Uploaded Files:</h2>";
echo "<ul>";
foreach ($files as $file) {
if ($file != "." && $file != "..") {
echo "<li><a href='" .
htmlspecialchars($_SERVER["PHP_SELF"]) .
"?file=$file'>$file</a></li>";
}

TARUN KUMAR 50
[DOCUMENT TITLE]

}
echo "</ul>";

// Check if a file was requested for download


if(isset($_GET["file"])) {
$file = "uploads/" . $_GET["file"];
if (file_exists($file)) {
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" .
basename($file) . "\"");
readfile($file);
exit;
}
else {
echo "<p>File not found.</p>";
}
}
?>
</body>
</html>

TARUN KUMAR 51
[DOCUMENT TITLE]

TARUN KUMAR 52

You might also like