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

Practical File Sec-A Harshul

Uploaded by

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

Practical File Sec-A Harshul

Uploaded by

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

VIVEKANANDA INSTITUTE OF PROFESSIONAL

STUDIES- Technical Campus


VIVEKANANDA SCHOOL OF INFORMATION
TECHNOLOGY

PRACTICAL FILE
Web Based Programming
(BCA 104)
Bachelor of Computer Application
(BATCH: 2023-2026)
Affiliated to
Guru Gobind Singh Indraprastha University, Delhi

SUBMITTED TO: SUBMITTED BY:


Dr. Anupama Jha NAME: Harshul Kharbanda
Associate Professor, VIPS Enrollment no.: 05917702023
Vivekananda Institute of Professional Studies, New Delhi

Subject Faculty: Dr. Anupama Jha

Programme: BCA Paper Code: 104 Academic Year: 2023-26

S_NO. Title Date Sign


1 WAP to show the usage of nested if statement
to find the maximum of 3 numbers.
2 WAP to show the usage of switch-case and
for/while/do while loop to create a menu driven
program for calculator.
3 WAP to implement all the inbuilt array functions
like array_pad(), array_slice(), array_splice(),
list() functions. (use foreach wherever
applicable)
4 Write a program to perform all four types of
sorting.
5 WAP using PHP to implement regular
expressions including modifiers, operators and
metacharacters
6 Design a login form and validate using PHP
programming. (Use of regular expression, Super
global variables such as $_GET(), $_POST(),
$_REQUEST() andappropriate functions)
7 Design a login form and validate using PHP
programming (use of PHP Filters).
8 WAP that passes the control to another page
(include, require, exit and die function).
9 Explain Cookie and how to make login logout
PHP script using Cookie. The following first
screenshot is for file named login.php. Do
necessary validation on form inputs: user’s
email and password.
10 Explain Session and write a PHP script to count the
number of visits of web pageby the user session.

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.
12 Design a form that upload, download and
display image file using PHP script.
13 WAP to implement OOPS Concept.
14 Write the following scripts using PHP and
MySQL: i) To create a MySQL Database ii) To
create a table and insert few records into it
using forms iii) To select all the records and
display it in table. iv) To modify a table
(add/delete/modify columns)
15

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;
}

echo "The maximum of $a, $b, and $c is: $max";


?>
Assignment 2
WAP to show the usage of switch-case and for/while/do while
loop to create a menu driven program for calculator.
<?php
do {
$operation = readline("\nEnter the operation (+, -, *, /): ");

$num1 = readline("Enter the first number: ");


$num2 = readline("Enter the second number: ");

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.

The screenshot for second.php is as per the format after


successfully logged into the page. Implement Logout option
with cookie.

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']++;

echo "You have visited this page ".$_SESSION['visit_count']." time(s).";

} 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">

<button type="submit" name="destroy_session">Destroy Session</button>

</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: ");

$source = fopen($sourceFile, "r");


$destination = fopen($destinationFile, "w");

if ($source && $destination) {


copy($sourceFile, $destinationFile);
fclose($source);
fclose($destination);
echo "File copied successfully.\n";
} else {
echo "Error copying the file.\n";
}
}
function displayFile() {
$filename = readline("Enter the name of the file to display: ");
$file = fopen($filename, "r");
if ($file) {
while (!feof($file)) {
echo fgets($file);
}
echo "\n";
fclose($file);
} else {
echo "Error opening the file.\n";
}
}
function modifyFile() {
$filename = readline("Enter the name of the file to modify: ");
$file = fopen($filename, "r+");
if ($file) {
$content = readline("Enter new content for the file: ");
ftruncate($file, 0); // Clear file contents
fwrite($file, $content);
fclose($file);
echo "File modified successfully.\n";
} else {
echo "Error opening the file.\n";
}
}
// Main program
while (true) {
echo "Menu:\n";
echo "1) Create a file\n";
echo "2) Copy a file\n";
echo "3) Display the content of a file\n";
echo "4) Modify the content of a file\n";
echo "5) Exit\n";

$choice = strtolower(readline("Enter your choice: "));


switch ($choice) {
case 1:
createFile();
break;
case 2:
copyFile();
break;
case 3:
displayFile();
break;
case 4:
modifyFile();
break;
case 5:
echo "Exiting program.\n";
exit();
default:
echo "Invalid choice. Please try again.\n";
}
}?>
Assignment 12
Design a form that upload, download and display image file
using PHP script.

<!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";
}
}

// Strawberry is inherited from Fruit


class Strawberry extends Fruit {
public function message() {
echo "Inherited The propertise of Fruit\n";
}
}
$strawberry = new Strawberry("Strawberry", "red");
$strawberry->message();
$strawberry->intro();
?>
Assignment 14
Write the following scripts using PHP and MySQL: i) To create
a MySQL Database ii) To create a table and insert few records
into it using forms iii) To select all the records and display it in
table. iv) To modify a table (add/delete/modify columns)

<!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>

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


</form>
</body>
</html>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully<br>";
$result = $conn->query("show databases like 'mydb'");
if ($result->num_rows == 0){
$query = "create database mydb";
if($conn->query($query)){
echo "Database Created Successfully<br>";
} else{
echo "Error ". $conn->error;
}
} else {
echo "Database Already Exists<br>";
}
$conn->query("use mydb");
$result = $conn->query("show tables like 'userdata'");
if ($result->num_rows == 0){
$query = "create table userdata(UID int Primary Key,
Fname varchar(20),
Sname Varchar(20),
gmail varchar(60)
)";
if($conn->query($query)){
echo "User Table Created Successfully<br>";
} else{
echo "Error ". $conn->error;
}
} else {
echo "User Table Already Exists<br>";
}
if(isset($_POST["submit"])){
$UID = $_POST["UID"];
$Fname = $_POST["firstname"];
$Sname = $_POST["lastname"];
$Email = $_POST["email"];
if($conn->query("insert into userdata values($UID,'$Fname','$Sname','$Email')")){
echo "User Added Successfully";
} else {
echo "Error ". $conn->error;
}
}
$result = $conn -> query("select * from userdata");
echo "<table border=\"1\">";
echo "<tr><th>UID</th><th>First Name</th><th>Last Name</th><th>Gmail</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["UID"]."</td><td>".$row["Fname"]."</td><td>".
$row["Sname"]."</td><td>".$row["gmail"]."</td></tr>";
}
echo "</table>";
echo "<form action='' method='post'>";
echo "<input type='submit' name='Remove' value='Remove Gmail Column'>";
echo "<input type='submit' name='ADD' value='ADD Gmail Column'>";
echo "<input type='submit' name='Modify' value='Modify Gmail Column'>";
echo "</form>";
if (isset($_POST["Remove"])){
if($conn->query("alter table userdata drop gmail")){
echo "Column Removed Succesfully\n";
} else{
echo "Error ". $conn->error;
}
} else if(isset($_POST["ADD"])){
if($conn->query("alter table userdata add gmail varchar(60)")){
echo "Column Added Succesfully\n";
} else{
echo "Error ". $conn->error;
}
} else if(isset($_POST["Modify"])){
if($conn->query("alter table userdata modify gmail char(60)")){
echo "Column Modified Succesfully\n";
} else{
echo "Error ". $conn->error;
}
}
$conn->close();
?>

You might also like