Practical File Sec-A Harshul
Practical File Sec-A Harshul
PRACTICAL FILE
Web Based Programming
(BCA 104)
Bachelor of Computer Application
(BATCH: 2023-2026)
Affiliated to
Guru Gobind Singh Indraprastha University, Delhi
Assignment 1
WAP to show the usage of nested if statement to find the
maximum of 3 numbers.
<?php
$a = readline("Enter a number: ");;
$b = readline("Enter a number: ");;
$c = readline("Enter a number: ");;
if ($a >= $b) {
if($a >= $c)
$max = $a;
} elseif ($b >= $a) {
if($b >= $c)
$max = $b;
} else {
$max = $c;
}
switch ($operation) {
case '+':
$result = $num1 + $num2;
echo "Result: $num1 + $num2 = $result";
break;
case '-':
$result = $num1 - $num2;
echo "Result: $num1 - $num2 = $result";
break;
case '*':
$result = $num1 * $num2;
echo "Result: $num1 * $num2 = $result";
break;
case '/':
if ($num2 != 0) {
$result = $num1 / $num2;
echo "Result: $num1 / $num2 = $result";
} else {
echo "Error: Division by zero is not allowed.";
}
break;
case 0:
echo "Exiting....";
break;
default:
echo "Error: Invalid operation.";
}
} while($operation != 0)
?>
Assignment 3
WAP to implement all the inbuilt array functions like
array_pad(), array_slice(), array_splice(), list() functions. (use
foreach wherever applicable)
<?php
$arr = array(10,20,40);
$stri = "Hello World";
echo "Menu:
1) array_pad
2) array_slice
3) array_splice
4) array_chunk
5) list
6) explode
7) implode
\n";
$user_inp = readline("Enter Choice:");
switch($user_inp){
case 1:
print_r(array_pad($arr,5,0));
break;
case 2:
print_r(array_slice($arr,2));
break;
case 3:
print_r(array_splice($arr,1));
break;
case 4:
print_r(array_chunk($arr,1));
break;
case 5:
list($a,$b,$c) = $arr;
echo $a,"\t",$b,"\t",$c;
break;
case 6:
print_r(explode(" ",$stri));
break;
case 7:
print_r(implode($arr));
break;
default:
echo "Error 404: Self Destructing";
break;
}
?>
Assignment 4
Write a program to perform all four types of sorting.
<?php
$intarr = array(50, 12, 10, 23, 15);
$strarr = array("India"=>"Delhi", "Japan"=>"Tokyo", "UK"=>"London",
"Nepal"=>"Kathmandu",
"Bangladseh"=>"Dhaka");
echo "Menu:
1) Sort
2) rsort
3) asort
4) arsort
5) ksort
6) krsort
\n";
$user_inp = readline("Enter Choice: ");
switch ($user_inp){
case 1:
sort($intarr);
print_r($intarr);
break;
case 2:
rsort($intarr);
print_r($intarr);
break;
case 3:
asort($strarr);
print_r($strarr);
break;
case 4:
arsort($strarr);
print_r($strarr);
break;
case 5:
ksort($strarr);
print_r($strarr);
break;
case 6:
krsort($strarr);
print_r($strarr);
break;
default:
echo "Error 404: Self Destructing";
break;
}
?>
Assignment 5
WAP using PHP to implement regular expressions including
modifiers, operators and metacharacters.
<?php
$text = '$colors = array(110,1,120,150,4);
$sum = 0;
foreach ($colors as $x)
{$sum += $x;}
';
$count = 0;
$text = explode("\n",$text);
foreach ($text as $x){
$count = $count + preg_match_all("/$.*;?/i", $x);
}
echo "Number of Variables are :".$count;
?>
Assignment 6
Design a login form and validate using PHP programming. (Use
of regular expression, Super global variables such as $_GET(),
$_POST(), $_REQUEST() andappropriate functions)
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login Form</h2>
<form method="post" action="">
<label for="username">Username:</label><br>
<input type="text" id="username" name="username" required><br>
<label for="password">Password:</label><br>
<input type="password" id="password" name="password" required><br>
<input type="submit" value="Login">
</form>
</body>
</html>
<?php
$passregex = "/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$/";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST['username'];
$password = $_POST['password'];
if (preg_match($passregex,$password)){
echo "UserName: $username\n";
echo "Password: $password";
} else{
echo "Password Denied";
}
}
?>
Assignment 7
Design a login form and validate using PHP programming (use
of PHP Filters).
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
</head>
<body>
<form action="" method="post">
<h2>Login</h2>
<?php if(isset($_GET['error']) && $_GET['error'] == 'true'): ?>
<p class="error">Invalid username or password!</p>
<?php endif; ?>
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<input type="submit" value="Login">
</form>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
$password = filter_var($_POST['password'], FILTER_SANITIZE_STRING);
$valid_username = "user";
$valid_password = "password";
if ($username === $valid_username && $password === $valid_password) {
echo "USER LOGIN SUCCESS";
exit;
} else {
header("Location: Prac7.php?error=true");
exit;
}
}
?>
Assignment 8
WAP that passes the control to another page (include, require,
exit and die function).
<!DOCTYPE html>
<html>
<body>
<h1>Welcome </h1>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$option = $_POST['option'];
switch ($option) {
case '1':
include 'page1.php'; break;
case '2':
require 'page1.php'; break;
case '3':
exit();
case '4':
die("Exiting Page Using DIE");
default:
echo "Invalid option selected.";
}
} ?>
<form method="post">
<label>Select an option:</label>
<select name="option">
<option value="1">Include Page 1</option>
<option value="2">Require Page 1</option>
<option value="3">Exit</option>
<option value="4">Die</option>
</select>
<input type="submit" value="Go">
</form>
</body>
</html>
Assignment 9
Explain Cookie and how to make login logout PHP script using
Cookie. The following first screenshot is for a file named
login.php. Do necessary validation on form inputs: user’s email
and password.
Index.html
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
$password = filter_var($_POST['password'], FILTER_SANITIZE_STRING);
setcookie("User_Logged",$password,time() + 60*60,"/");
header("Location: secound.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
</head>
<body>
<form action="" method="post">
<h2>Login</h2>
<?php if(isset($_GET['error']) && $_GET['error'] == 'true'): ?>
<p class="error">Invalid username or password!</p>
<?php endif; ?>
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<input type="submit" value="Login">
</form>
</body>
</html>
Index2.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<form method="post" action="">
<input type="submit" name="logout" value="Logout">
</form>
<h2>Welcome User</h2>
</body>
<?php
if (!isset($_COOKIE['User_Logged'])) {
header("Location: login.php");
exit;
}
if (isset($_POST["logout"])){
setcookie("User_Logged", "", time() - 3600, "/");
header("Location: login.php");
} ?>
</html>
Assignment 10
Explain Session and write a PHP script to count the number of
visits of web page by the user session
<?php
session_start();
if(isset($_SESSION['visit_count'])) {
$_SESSION['visit_count']++;
} else {
$_SESSION['visit_count'] = 1;
if(isset($_POST['destroy_session'])) {
session_destroy();
header("Location: ".$_SERVER['PHP_SELF']);
exit;
?>
<!DOCTYPE html>
<html>
<head>
<title>Destroy Session</title>
</head>
<body>
<form method="post">
</form>
</body>
</html>
Assignment 11
Write a menu-driven program for:Choice a) Create a file.
Choice b) To read such file and then copy the content of this
file to another file. Choice c) To read and display the content of
the previously created file. Choice d) To modify the content of a
file.
<?php
function createFile() {
$filename = readline("Enter the name of the file to create: ");
$file = fopen($filename, "w");
if ($file) {
$content = readline("Enter new content for the file: ");
fwrite($file, $content);
fclose($file);
echo "File created successfully.\n";
} else {
echo "Error creating the file.\n";
}
}
function copyFile() {
$sourceFile = readline("Enter the name of the source file: ");
$destinationFile = readline("Enter the name of the destination file: ");
<!DOCTYPE html>
<head>
<title>File Upload Form</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<h2>Upload File</h2>
<label for="fileSelect">Image:</label>
<input type="file" name="photo" id="fileSelect">
<input type="submit" name="submit" value="Upload">
<p><strong>Note:</strong>Only .JPG, .jpg, .jpeg, .png formats allowed
to a max size of 2MB.</p>
</form>
<h1>Display uploaded Image:</h1>
<?php if (isset($_FILES["photo"])) : ?>
<img src="<?php echo "upload/" . $_FILES["photo"]["name"]; ?>" height="200px"
alt="Uploaded Image">
<?php $imagePath = "upload/" . $_FILES["photo"]["name"]; ?>
<a href="<?php echo $imagePath; ?>" download>Download Image</a>
<?php endif; ?>
</body>
</html>
<?php
if (isset($_SERVER["REQUEST_METHOD"]) && ($_SERVER["REQUEST_METHOD"] ==
"POST")) {
if (isset($_POST['submit'])) {
echo "<pre>";
print_r($_FILES);
echo "</pre>";
// Check if file was uploaded without errors
if (isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0) {
$allowed_ext = array(
"JPG" => "image/jpg",
"jpg" => "image/jpg",
"jpeg" => "image/jpeg",
"gif" => "image/gif",
"png" => "image/png"
);
$file_name = $_FILES["photo"]["name"];
$file_type = $_FILES["photo"]["type"];
$file_size = $_FILES["photo"]["size"];
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
if (!array_key_exists($ext, $allowed_ext))
die("Error: Please select a valid file format.");
$maxsize = 2 * 1024 * 1024;
if ($file_size > $maxsize)
die("Error: File size is larger than the allowed limit of 2MB");
if (in_array($file_type, $allowed_ext)) {
if (file_exists("upload/" . $_FILES["photo"]["name"]))
echo $_FILES["photo"]["name"] . " already exists!";
else {
move_uploaded_file(
$_FILES["photo"]["tmp_name"],
"upload/" . $_FILES["photo"]["name"]
);
echo "Your file uploaded successfully.";
}
} else {
echo "Error: Please try again!";
}
} else {
echo "Error: " . $_FILES["photo"]["error"];
}
}
}
?>
Assignment 13
WAP to implement OOPS Concept.
<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.\n";
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add New Guest</title>
</head>
<body>
<h2>Add User</h2>
<form action="" method="post">
<label for="UID">Enter User ID:</label><br>
<input type="number" id="UID" name="UID" required><br>
<label for="firstname">First Name:</label><br>
<input type="text" id="firstname" name="firstname" required><br>
<label for="lastname">Last Name:</label><br>
<input type="text" id="lastname" name="lastname" required><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email"><br>