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

php practical FINAL

The document contains practical programming assignments for a BCA course, focusing on PHP programming concepts such as regular expressions, nested if statements, switch-case structures, loops, sorting algorithms, and array functions. Each program includes code examples and explanations of the logic behind them. The author of the document is Saksham Gupta, with a roll number of 03714202023.

Uploaded by

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

php practical FINAL

The document contains practical programming assignments for a BCA course, focusing on PHP programming concepts such as regular expressions, nested if statements, switch-case structures, loops, sorting algorithms, and array functions. Each program includes code examples and explanations of the logic behind them. The author of the document is Saksham Gupta, with a roll number of 03714202023.

Uploaded by

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

Practical – I [WEB BASED PGM Lab] [BCA-104]

PROGRAM 1

/*Program 1: Write regular expressions including modifiers, operators, and metacharacters


?
Name : SAKSHAM GUPTA
Roll No 03714202023

*/

Solution:

<?php
$pattern = "/fo[rl]ks\?+/";

$text = "Could you please pass the forks? , the folks at the dinner table politely requested.";

$matches = preg_match_all($pattern, $text, $array);

echo $matches . " matches were found.";

?>

In this example, the regular expression fo[rl]ks\?+ matches the string “folks” in the text. The [rl] is a
character class that matches either "r" or "l", and the \? is a metacharacter that matches the "?"
character. The + is a quantifier that matches one or more of the preceding character, so \?+ matches one
or more "?" characters. The i modifier is not used in this example, but it can be added to the end of the
regular expression to make the search case-insensitive.

Here's a breakdown of the regular expression:

fo matches the string "fo"


[rl] matches either "r" or "l"
ks matches the string "ks"
\? matches the "?" character
+ matches one or more of the preceding character

SAKSHAM GUPTA [Roll No: 03714202023] pg. 1


Practical – I [WEB BASED PGM Lab] [BCA-104]

PROGRAM 2

/*Program 2: Write a program to show the usage of nested If statement ?


Name : SAKSHAM GUPTA
Roll No 03714202023

*/

Solution :

Nested_if.html

<html>
<head>
<title> age verification </title>
</head>
<body>

<h2>Check Age</h2>

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


Age: <input type="text" name="age">
<input type="submit">
</form>

</body>
</html>

Nested_if.php

<html>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$age = $_POST["age"];
if (is_numeric($age))

SAKSHAM GUPTA [Roll No: 03714202023] pg. 2


Practical – I [WEB BASED PGM Lab] [BCA-104]

{
if ($age < 18)
{
echo "You are " . $age . " years old, so you are not an adult yet.";
}
else if ($age >= 18 && $age <= 60)
{

echo "You are " . $age . " years old, so you are an adult.";
}
else if ( $age > 60 && $age <= 100 )
{
echo "You are " . $age . " years old, so you are a sinior citizon .";
}
else
{
echo "Invalid age entered. Please enter a valid age";
}
}
else
{
echo "Invalid age entered. Please enter a valid age";
}
}
?>
</body>
</html>

SAKSHAM GUPTA [Roll No: 03714202023] pg. 3


Practical – I [WEB BASED PGM Lab] [BCA-104]

OUTPUT :

SAKSHAM GUPTA [Roll No: 03714202023] pg. 4


Practical – I [WEB BASED PGM Lab] [BCA-104]

PROGRAM 3
/*Program 3 : : Write a program to create a menu driven program and show the
usage of switch-case.?
Name : SAKSHAM GUPTA
Roll No 03714202023

*/
SOLUTION :

SWITCH.HTML
<html>
<head>
<title>calculator</title>
</head>
<body>
<h1>Calculator</h1>

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


Enter first number: <input type="number" name="num1" required><br><br>
Enter second number: <input type="number" name="num2" required><br><br>
Choose an operation:
<select name="operator">
<option>choose option</option>
<option value="add">Addition</option>
<option value="subtract">Subtraction</option>
<option value="multiply">Multiplication</option>
<option value="divide">Division</option>
</select><br><br>

<input type="submit" value="Calculate">


<input type="reset" value="reset values">
</form>
</body>
</html>

SAKSHAM GUPTA [Roll No: 03714202023] pg. 5


Practical – I [WEB BASED PGM Lab] [BCA-104]

SWITCH.PHP
<html>
<head>
<title>Calculator</title>
</head>
<body>
<?php
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$operator = $_POST['operator'];

switch ($operator)
{ case 'add':
$result = $num1 + $num2;
break;
case 'subtract':
$result = $num1 - $num2;
break;
case 'multiply':
$result = $num1 * $num2;
break;
case 'divide':
if ($num2 == 0) {
echo "Error: Division by zero is not allowed.";
exit();
}
$result = $num1 / $num2;
break;
default:
echo "Error: choose an option";
exit();
}

echo "<p>The result is: $result</p>";


?>
<p><a href="switch.html"><button>Back to calculator</button></a></p>
</body>

SAKSHAM GUPTA [Roll No: 03714202023] pg. 6


Practical – I [WEB BASED PGM Lab] [BCA-104]

</html>

OUTPUT:

SAKSHAM GUPTA [Roll No: 03714202023] pg. 7


Practical – I [WEB BASED PGM Lab] [BCA-104]

PROGRAM 4
/*Program 4: Write a program to show the usage of for/while/do while loop ?
Name : SAKSHAM GUPTA
Roll No 03714202023

*/
SOLUTION:

i) FOR LOOP –

<html>
<head>
<title>Print Numbers</title>
</head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"
method="post">
<label for="number">Enter a number:</label>
<input type="number" id="number" name="number" required>
<br><br>
<button type="submit">Print Numbers</button>
</form>

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

echo "Numbers from 1 to $number: <br>";


for ($i = 1; $i <= $number; $i++)
{
echo "$i <br>";
}
}
?>
</body>
</html>

SAKSHAM GUPTA [Roll No: 03714202023] pg. 8


Practical – I [WEB BASED PGM Lab] [BCA-104]

OUTPUT :

SAKSHAM GUPTA [Roll No: 03714202023] pg. 9


Practical – I [WEB BASED PGM Lab] [BCA-104]

ii) WHILE LOOP

<html>
<head>
<title>Print Numbers</title>
</head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"
method="post">
<label for="number">Enter a number:</label>
<input type="number" id="number" name="number" required>
<br><br>
<button type="submit">Print Numbers</button>
</form>

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

echo "Numbers from 1 to $number: <br>";


while ($i <= $number) {
echo "$i <br>";
$i++;
}
}
?>
</body>
</html>

SAKSHAM GUPTA [Roll No: 03714202023] pg. 10


Practical – I [WEB BASED PGM Lab] [BCA-104]

OUTPUT :

SAKSHAM GUPTA [Roll No: 03714202023] pg. 11


Practical – I [WEB BASED PGM Lab] [BCA-104]

iii) DO WHILE LOOP

<html>
<head>
<title>Print Numbers</title>
</head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"
method="post">
<label for="number">Enter a number:</label>
<input type="number" id="number" name="number" required>
<br><br>
<button type="submit">Print Numbers</button>
</form>

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

echo "Numbers from 1 to $number: <br>";


do {
echo "$i <br>";
$i++;
} while ($i <= $number);
}
?>

</body>
</html>

SAKSHAM GUPTA [Roll No: 03714202023] pg. 12


Practical – I [WEB BASED PGM Lab] [BCA-104]

OUTPUT :

SAKSHAM GUPTA [Roll No: 03714202023] pg. 13


Practical – I [WEB BASED PGM Lab] [BCA-104]

PROGRAM 5

/*Program 5: Write a program to perform all four types of sortting.

Name : SAKSHAM GUPTA


Roll NO : 03714202023

*/

SOLUTION

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sorting Algorithms</title>
</head>
<body bgcolor="#01B4FF">
<h1>Sorting Algorithms</h1>

<form method="post">
<label for="numbers">Enter numbers (comma-separated):</label>
<input type="text" id="numbers" name="numbers" required>
<input type="submit" value="Sort">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$input = $_POST['numbers'];
$numbers = explode(',', $input);

// Remove any leading/trailing whitespace and convert strings to integers


$numbers = array_map('trim', $numbers);
$numbers = array_map('intval', $numbers);

echo "<h2>Original Array:</h2>";

SAKSHAM GUPTA [Roll No: 03714202023] pg. 14


Practical – I [WEB BASED PGM Lab] [BCA-104]

printArray($numbers);

// Bubble Sort
$bubbleSorted = bubbleSort($numbers);
echo "<h2>Bubble Sort:</h2>";
printArray($bubbleSorted);

// Selection Sort
$selectionSorted = selectionSort($numbers);
echo "<h2>Selection Sort:</h2>";
printArray($selectionSorted);

// Insertion Sort
$insertionSorted = insertionSort($numbers);
echo "<h2>Insertion Sort:</h2>";
printArray($insertionSorted);

// Quick Sort
$quickSorted = quickSort($numbers);
echo "<h2>Quick Sort:</h2>";
printArray($quickSorted);
}

function bubbleSort($arr) {
$n = count($arr);
for ($i = 0; $i < $n - 1; $i++) {
for ($j = 0; $j < $n - $i - 1; $j++)
{ if ($arr[$j] > $arr[$j + 1]) {
$temp = $arr[$j];
$arr[$j] = $arr[$j + 1];
$arr[$j + 1] = $temp;
}
}
}
return $arr;
}

SAKSHAM GUPTA [Roll No: 03714202023] pg. 15


Practical – I [WEB BASED PGM Lab] [BCA-104]

function selectionSort($arr) {
$n = count($arr);
for ($i = 0; $i < $n - 1; $i++) {
$minIndex = $i;
for ($j = $i + 1; $j < $n; $j++) {
if ($arr[$j] < $arr[$minIndex]) {
$minIndex = $j;
}
}
$temp = $arr[$i];
$arr[$i] = $arr[$minIndex];
$arr[$minIndex] = $temp;
}
return $arr;
}

function insertionSort($arr) {
$n = count($arr);
for ($i = 1; $i < $n; $i++) {
$key = $arr[$i];
$j = $i - 1;
while ($j >= 0 && $arr[$j] > $key) {
$arr[$j + 1] = $arr[$j];
$j = $j - 1;
}
$arr[$j + 1] = $key;
}
return $arr;
}

function quickSort($arr) {
$n = count($arr);
if ($n <= 1) {
return $arr;
}
$pivot = $arr[0];
$left = $right = [];

SAKSHAM GUPTA [Roll No: 03714202023] pg. 16


Practical – I [WEB BASED PGM Lab] [BCA-104]

for ($i = 1; $i < $n; $i++)


{ if ($arr[$i] < $pivot) {
$left[] = $arr[$i];
} else {
$right[] = $arr[$i];
}
}
return array_merge(quickSort($left), [$pivot], quickSort($right));
}

function printArray($arr)
{ echo "<pre>";
print_r($arr);
echo "</pre>";
}
?>
</body>
</html>

SAKSHAM GUPTA [Roll No: 03714202023] pg. 17


Practical – I [WEB BASED PGM Lab] [BCA-104]

OUTPUT :

SAKSHAM GUPTA [Roll No: 03714202023] pg. 18


Practical – I [WEB BASED PGM Lab] [BCA-104]

PROGRAM 6
/* Program 6:- Write al Program to implement array, pad(),array_ slice(),array.
Splice(),list()
Name : SAKSHAM GUPTA
Roll No: 03714202023
*/
Solution:

<!DOCTYPE html>
<html>
<head>
<title>Array Functions</title>
<link rel="stylesheet" href="array_functions.css">
</head>
<body>
<hl>Array Functions</h1>
<div class="container">
<div class="output">
<h2>Array Pad</h2>
<?php
$original_array =[1, 2, 3];
$padded_array = array_pad($original_array, 5, 0);
echo "<p>Original Array: ".implode(", ", $original_array)."</p>";
echo "<p>Array Pad:". implode(", ", $padded_array)."</p>";
?>
</div>
<div class="output">
<h2>Array Slice</h2>
<?php
$sliced_array = array_slice($original_array, 1, 2);
echo "<p>Original Array: ".implode(", ", $original_array)."</p>";
echo "<p>Array Slice: ". implode(", ", $sliced_array)."</p>"; ?>
</div>
<div class="output">
<h2>Aray Splice</h2>
<?php
$spliced_array = $original_array;

SAKSHAM GUPTA [Roll No: 03714202023] pg. 19


Practical – I [WEB BASED PGM Lab] [BCA-104]

array_splice($spliced_array, 1,2,[4,5]);
echo "<p>Original Array: " .implode(",",$original_array). "</p>";
echo "<p>Array Splice: ".implode(", ", $spliced_array). "/p>"; ?>
</div>
<div class="output">
<h2>List</h2>
<?php
list($a, $b, $c)= $original_array;
echo "<p>Original Array: ".implode(", ", $original_array)."</p>";
echo "<p>List: a =$a,b= $b, c= $c</p>"; ?>
</div></div>
</body>
</html>\

Css

body {
font-family: Arial, sans-serif;
padding: 10px;
background-color: aquamarine;
}
hl {
text-align: center;
}
.container
{ display:
center;
justify-content: center;
margin-top: 10px;
}
.output {
border: 5px solid #1a1a1a;
padding: 10px;
margin: 10px 10px;
background-color:
azure;
}
SAKSHAM GUPTA [Roll No: 03714202023] pg. 20
Practical – I [WEB BASED PGM Lab] [BCA-104]

OUTPUT :

SAKSHAM GUPTA [Roll No: 03714202023] pg. 21


Practical – I [WEB BASED PGM Lab] [BCA-104]

PROGRAM 7
/* Program 7:- Write a program to implement functions.(use foreach wherever
applicable)
Name : SAKSHAM GUPTA
Roll No: 03714202023
*/
SOLUTION

<!DOCTYPE html>
<html>
<head>
<title>PHP Functions</title>
<link rel="stylesheet" href="foreach.css">
</head>
<body>
<hl>PHP Functions</h1>
<div class="container">
<div class="output">
<h2>Factorial Calculation</h2>
<?php
function factorial($n) {
if($n==0){
return 1;
}
else
{
return $n * factorial($n - 1);}
}
$number =5;
echo "Factorial of $number is".factorial($number);
?>
<div>
<div class="output">
<h2>Reverse of Array</h2>
<?php
function reverse_array($arr)
{ return array_reverse($arr);

SAKSHAM GUPTA [Roll No: 03714202023] pg. 22


Practical – I [WEB BASED PGM Lab] [BCA-104]

}
$original_array=[1, 2, 3,4,5];
$reversed_array= reverse_array($original_array);
foreach ($reversed_array as $element)
{
echo $element.",";
}
?>
</div>
</div>
</body>
</html>

CSS

body {
font-family: Arial, sans-serif;
padding: 10px;
background-color: aquamarine;
}

hl {
text-align: center;
}
.container
{ display:
center;
justify-content: center;
margin-top: 10px;
}
.output {
border: 5px solid #1a1a1a;
padding: 10px;
margin: 10px 10px;
background-color:
azure;
}
SAKSHAM GUPTA [Roll No: 03714202023] pg. 23
Practical – I [WEB BASED PGM Lab] [BCA-104]

OUTPUT :

SAKSHAM GUPTA [Roll No: 03714202023] pg. 24


Practical – I [WEB BASED PGM Lab] [BCA-104]

PROGRAM 8
/* Program 8:- Write a program that passes control to another page (include , require ,
exit and die functions )
Name : SAKSHAM GUPTA
Roll No : 03714202023
*/

SOLUTION

<!DOCTYPE html>
<html>
<head>
<title>Control Transfer</title>
<link rel="stylesheet" href="Pass_control.css">
</head>
<body>
<hl>Control Transfer</h1>
<div class="container">
<div class="output">
<h2>Include</h2>
<?php
echo "Before include statement<br><br>";
$included_text = "Included text";
echo $included_text;
echo "<br><br>After include statement";?>
</div>
<div class="output">
<h2>Require</h2>
<?php
echo "Before require statement<br><br>";
$required_text = "This is required text";
echo $required_text;
echo "<br><br>After require statement";?>
</div>
<div class="output">
<h2>Exit</h2>
<?php

SAKSHAM GUPTA [Roll No: 03714202023] pg. 25


Practical – I [WEB BASED PGM Lab] [BCA-104]

echo "Before exit statement<br><br>";


exit("Exiting program");
echo "<br><br>After exit statement";?>
</div>
<div class="output">
<h2>Die</h2>
<?php
echo "Before die statement<br>";
die("Exiting program with die");
echo "<br>After die statement";?>
</div>
</div>
</body>
</html>

CSS

body {
font-family: Arial, sans-serif;
padding: 30px;
background-color: yellow;
}
hl {
text-align: center;
}
.container{ dis
play: flex;
justify-content: center;
margin-top: 20px;
}
.output{
border: 2px solid #092531;
padding: 10px;
margin: 10px 10px;
background-color: cornflowerblue;
}

SAKSHAM GUPTA [Roll No: 03714202023] pg. 26


Practical – I [WEB BASED PGM Lab] [BCA-104]

OUTPUT :

SAKSHAM GUPTA [Roll No: 03714202023] pg. 27


Practical – I [WEB BASED PGM Lab] [BCA-104]

PROGRAM 9
/* Program 9 :- Write a program to show the usage of Cookie?
Name : SAKSHAM GUPTA
Roll No : 03714202023
*/
SOLUTION

<!DOCTYPE html>
<html>
<head>
<title> Cookie Example</title>
<link rel="stylesheet" href="Cookies.css">
</head>
<body>
<hl>Consent Form</h1>
<div class="container">
<div class="output">
<?php
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['phone'])) {
setcookie('username', $_POST['name'], time() + 3600); // Set cookie for 1 hour
setcookie('useremail', $_POST['email'], time() + 3600); // Set cookie for 1 hour
setcookie('userphone', S_POST['phone'], time() + 3600); // Set cookie for 1 hour
echo "Hello, " .$_POST['name']. "! Your information has been stored.";
}
elseif (isset($_COOKIE['username']) && isset($_COOKIE['useremail']) &&
isset($_COOKTE['userphone'])) {
echo "Hello, ". $_COOKIE['username']. "!Welcome back.";
echo "<br>Your email: ".$_COOKIE['useremail'];
echo "<br>Your phone number: ". $_COOKIE['userphone'];
}?>

</div> </div>
<div class="container">
<form method="post" action="">
<label for="name">Enter your name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="email">Enter your email: </label>

SAKSHAM GUPTA [Roll No: 03714202023] pg. 28


Practical – I [WEB BASED PGM Lab] [BCA-104]

<input type="email" id="email" name="email" required><br><br>


<label for="phone">Enter your phone number:</label>
<input type="text" id="phone" name="phone" required><br><br>
<input type="submit" value="Submit">
</form>
</div>
</body>
</html>

CSS

body {
font-family: Arial, sans-serif;
padding: 10px;
background-color: aquamarine;
}

hl {
text-align: center;
}
.container
{ display:
center;
justify-content: center;
margin-top: 10px;
}
.output {
border: 5px solid #1a1a1a;
padding: 10px;
margin: 10px 10px;
background-color:
azure;
}
SAKSHAM GUPTA [Roll No: 03714202023] pg. 29
Practical – I [WEB BASED PGM Lab] [BCA-104]

OUTPUT :

SAKSHAM GUPTA [Roll No: 03714202023] pg. 30


Practical – I [WEB BASED PGM Lab] [BCA-104]

PROGRAM 10
/* Program 10 :- Write a program to show the usage of session ?
Name : SAKSHAM GUPTA
Roll No 03714202023
*/
SOLUTION

<?php
session_start();
$valid_username = 'user';
$valid_password = 'password';
if (isset($_POST['login']))
$username=$_POST['username'];
$password=$_POST['password'];
if ($username === $valid_username && $password === $valid_password) {
$_SESSION['loggedin']=true;
$_SESSION['username'] = $username;
header('Location: protected.php');
exit;
} else {
$error = 'Invalid username or password.';
}
if (isset($_POST['logout'])) {
session_unset();
session_destroy();
}?>
<!DOCTYPE html>
<html>
<head>
<title>Session Example - User Authentication</title>
<link rel="stylesheet" href="session.css">
</head>
<body>
<hl>User Authentication</h1>
<div class="container">
<div class="output">
<?php

SAKSHAM GUPTA [Roll No: 03714202023] pg. 31


Practical – I [WEB BASED PGM Lab] [BCA-104]

if (isset($_SESSION['loggedin']) && $_SESSION['loggedin']===true) {


echo 'Hello,'. $_SESSIONI['username'].'!<br>';
echo '<form method="post" action="">
<input type="submit" name="logout"value="Logout">
</form>';
} else {
echo
'<label for="username"> Username: </label>
<input type="text" id="username" name="username"> <br>
<label for="password">Password: </label>
<input type="password" id="password" name="password"><br><br>
<input type="submit" name="login" value="Login">
</form>';
if (isset($error)) {
echo '<p>'. $error .'</p>';
}}?>
</div>
</div>
</body>
</html>
CSS

body {
font-family: Arial, sans-serif;
padding: 20px;
background-color: pink;}
h1 {text-align: center;}
.container{ d
isplay: flex;
justify-content: center;
margin-top: 20px;}
.output {
border: 1px solid
#ccc; padding: 10px;
margin: 0 10px;
}
form {text-align: center;}

SAKSHAM GUPTA [Roll No: 03714202023] pg. 32


Practical – I [WEB BASED PGM Lab] [BCA-104]

OUTPUT :

SAKSHAM GUPTA [Roll No: 03714202023] pg. 33


Practical – I [WEB BASED PGM Lab] [BCA-104]

PROGRAM 11
/* Program 11 :- Write a program to implement oops concept.
Name : SAKSHAM GUPTA
Roll No 03714202023
*/

Q11.HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple OOP Example</title>
<link rel="stylesheet" href="styl.css">
</head>
<body>
<h1>Simple OOP Example</h1>
<form action="process.php" method="post">
<label for="color">Enter Car Color:</label>
<input type="text" name="color" id="color" required><br>
<label for="model">Enter Car Model:</label>
<input type="text" name="model" id="model" required><br>
<label for="year">Enter Car Year:</label>
<input type="number" name="year" id="year" min="1900" max="2022" required><br>
<button type="submit">Submit</button>
</form>
</body>
</html>

PROCESS.PHP
<?php

require_once 'Car.php';

$color = $_POST['color'];
$model = $_POST['model'];

SAKSHAM GUPTA [Roll No: 03714202023] pg. 34


Practical – I [WEB BASED PGM Lab] [BCA-104]

$year = $_POST['year'];
$myCar = new Car($color, $model, $year);
echo "The car is a ". $myCar->color. " ". $myCar->model. " from ". $myCar->year. ".";
?>

CAR.PHP
<?php

class Car {
public $color;
public $model;
public $year;

function construct($color, $model, $year) {


$this->color = $color;
$this->model = $model;
$this->year = $year;
}
}

?>

SLYL.CSS
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}

form {
margin-top: 20px;
}

label {
display: block;

SAKSHAM GUPTA [Roll No: 03714202023] pg. 35


Practical – I [WEB BASED PGM Lab] [BCA-104]

margin-top: 10px;
}

input[type="text"], input[type="number"]
{ width: 100%;
padding: 5px;
margin-top: 5px;
}

button {
margin-top: 20px;
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}

OUTPUT:

SAKSHAM GUPTA [Roll No: 03714202023] pg. 36


Practical – I [WEB BASED PGM Lab] [BCA-104]

PROGRAM 12

/* Program 12 :- Do Form handling In PHP Design a personal Information form , then


Submit & Retrieve the Form Data Using $_GET(), $_POST() and $_REQUEST()
Variables. Design A Login Form and Validate that Form using PHP Programming.

Name : SAKSHAM GUPTA


Roll No 03714202023
*/

Q12.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Personal Information Form</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h2>Personal Information Form</h2>
<form action="process_form.php" method="post">

<label for="fname">First Name:</label>


<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last Name:</label>
<input type="text" id="lname" name="lname"><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br><br>
<label for="uname">Choose a Username for your Account :</label>
<input type="text" id="uname" name="uname"><br><br>
<label for="pword">Choose a Password:</label>
<input type="password" id="pword" name="pword"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

SAKSHAM GUPTA [Roll No: 03714202023] pg. 37


Practical – I [WEB BASED PGM Lab] [BCA-104]

Style.css
body {
font-family: 'Roboto', sans-serif;
background-color: #f09191;}

h2 {
text-align: center;
color: #333;}

form {
width: 300px;
background-color: #fff;
padding: 30px;
margin: 0 auto;
margin-top: 50px;
box-shadow: 0px 0px 10px rgba(0,0,0,0.1);
border-radius: 5px;}

label {
display: block;
margin-top: 10px;
color: #333;}

input[type="text"], input[type="password"], input[type="email"]


{ width: 100%;
padding: 5px;
margin-top: 2px;
border: 1px solid
#ccc; border-radius:
5px;}

input[type="submit"]
{ background-color:
#4CAF50; color: white;
padding: 14px 20px;
border: none;}

SAKSHAM GUPTA [Roll No: 03714202023] pg. 38


Practical – I [WEB BASED PGM Lab] [BCA-104]

Process_form.php
<?php
session_start();

$_SESSION['uname'] = $_POST['uname'];
$_SESSION['pword'] = $_POST['pword'];

header('Location: login_form.html');
exit;
?>

Login.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Login Form</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h2>Login Form</h2>
<form action="process_login.php" method="post">
<label for="uname">Username:</label>
<input type="text" id="uname" name="uname"><br><br>
<label for="pword">Password:</label>
<input type="password" id="pword" name="pword"><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>

SAKSHAM GUPTA [Roll No: 03714202023] pg. 39


Practical – I [WEB BASED PGM Lab] [BCA-104]

Process_login.php
<?php
session_start();

$username = $_SESSION['uname'];
$password = $_SESSION['pword'];

if ($_POST['uname'] == $username && $_POST['pword'] == $password)


{ echo "<p>Login successful!</p>";
echo "<br><br><form action='logout.php' method='post'>";
echo "<button type='submit'>Logout</button>";
echo "</form>";
}
else {
echo "<p>Invalid username or password.</p>";
}
?>

Logout.php
<?php
session_start();
session_destroy();
echo "You have been logged out.";
echo "<form action='q12.html' method='post'>";
echo "<br><br><button type='submit'>go to personal information form</button>";
echo "</form>";
exit;
?>

SAKSHAM GUPTA [Roll No: 03714202023] pg. 40


Practical – I [WEB BASED PGM Lab] [BCA-104]

Output:

SAKSHAM GUPTA [Roll No: 03714202023] pg. 41


Practical – I [WEB BASED PGM Lab] [BCA-104]

PROGRAM 13

/* Program 13 :- Write a program that use various PHP library functions,


and that manipulate files and directories.

Name : SAKSHAM GUPTA


Roll No 03714202023
*/

<!DOCTYPE html>
<html lang="en">
<head>
<title>a program that use various PHP library functions, and that manipulate files and
directories.</title>
</head>
<body>
<?php

if (file_exists('example.txt'))
{
$file = fopen('example.txt', 'r+');
$contents = file_get_contents('example.txt');
echo '<h4>before writing in file</h4><br>';

while (!feof($file))
{
$line = fgets($file);
echo '<br>Line: '. $line;
}

$txt='i am a student of bca';


fwrite($file, $txt);
echo '<br><br><h4>after writing in file</h4><br>';
rewind($file);

while (!feof($file))

SAKSHAM GUPTA [Roll No: 03714202023] pg. 42


Practical – I [WEB BASED PGM Lab] [BCA-104]

{
$line = fgets($file);
echo '<br>Line: '. $line;
}

fclose($file);
}

else
{
mkdir('example_dir');
if (is_dir('example_dir'))
{
echo 'The directory was created.';
}
}

?>
</body>
</html>

OUTPUT :

SAKSHAM GUPTA [Roll No: 03714202023] pg. 43


Practical – I [WEB BASED PGM Lab] [BCA-104]

PROGRAM 14

/* Program 14 :- Write a program to modify the content of an existing file.

Name : SAKSHAM GUPTA


Roll No 03714202023
*/

<?php
$file = 'q14.txt';
$currentContent = file_get_contents($file);

echo '<h4>file contents before modifying : </h4><br><br>';


echo $currentContent;

$newContent = $currentContent. "\n". "this is php coding. it is used to creat webpages and it
is used with html.\n";
file_put_contents($file, $newContent);

echo '<br><br><h4>file contents after modifying : </h4><br><br>';


echo $newContent;
?>

OUTPUT :

SAKSHAM GUPTA [Roll No: 03714202023] pg. 44


Practical – I [WEB BASED PGM Lab] [BCA-104]

PROGRAM 15

/* Program 15 :- Design a from which upload And Display Image in PHP.


Name : SAKSHAM GUPTA
Roll No 03714202023
*/

Index.php
<!DOCTYPE html>
<html>

<head>
<style>
.form-container {
width: 100%;
max-width: 500px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #f2f2f2;
text-align: center;
}

.form-container input[type="file"]
{ width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
box-sizing: border-box;
}

.form-container input[type="submit"] {
width: 100%;
padding: 10px;
border: none;

SAKSHAM GUPTA [Roll No: 03714202023] pg. 45


Practical – I [WEB BASED PGM Lab] [BCA-104]

border-radius: 5px;
background-color: #4CAF50;
color: #fff;
cursor: pointer;
margin-top: 10px;
}
</style>
</head>

<body>

<form action="upload.php" method="post" enctype="multipart/form-data">


<div class="form-container">
<b><label for="fileToUpload">Select image to upload:</label><br><br></b>
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</div>
</form>

<div >
<img src="uploads/<?php echo @htmlspecialchars(basename($_FILES["fileToUpload"]
["name"])); ?>" >
</div>

</body>

</html>

Upload.php
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

SAKSHAM GUPTA [Roll No: 03714202023] pg. 46


Practical – I [WEB BASED PGM Lab] [BCA-104]

if (isset($_POST["submit"]))
{
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if ($check !== false)
{
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
}
else
{
echo "File is not an image.";
$uploadOk = 0;
}
}

if (file_exists($target_file))
{
echo "Sorry, file already exists.";
$uploadOk = 0;
}

if ($_FILES["fileToUpload"]["size"] > 50000000)


{
echo "Sorry, your file is too large.";
$uploadOk = 0;
}

if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"


&& $imageFileType != "gif")
{
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}

if ($uploadOk == 0)
{
echo "Sorry, your file was not uploaded.";

SAKSHAM GUPTA [Roll No: 03714202023] pg. 47


Practical – I [WEB BASED PGM Lab] [BCA-104]

}
else
{
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
{
echo "The file " . htmlspecialchars(basename($_FILES["fileToUpload"]["name"])) . "
has been uploaded. <br><br>";
}
else
{
echo "Sorry, there was an error uploading your file. <br><br>";
}
}

OUTPUT:

SAKSHAM GUPTA [Roll No: 03714202023] pg. 48


Practical – I [WEB BASED PGM Lab] [BCA-104]

PROGRAM 16

/* Program 16 :- Use phpMyAdmin and perform the following : import, review data
and structure, run SQL statements, create users and privileges.

Name : SAKSHAM GUPTA


Roll No 03714202023
*/

XAMPP CONTROL PANEL -

SAKSHAM GUPTA [Roll No: 03714202023] pg. 49


Practical – I [WEB BASED PGM Lab] [BCA-104]

IMPORT :

SAKSHAM GUPTA [Roll No: 03714202023] pg. 50


Practical – I [WEB BASED PGM Lab] [BCA-104]

REVIEW DATA AND STRUCTURE :

run SQL statements-

SAKSHAM GUPTA [Roll No: 03714202023] pg. 51


Practical – I [WEB BASED PGM Lab] [BCA-104]

create users and privileges –

SAKSHAM GUPTA [Roll No: 03714202023] pg. 52


Practical – I [WEB BASED PGM Lab] [BCA-104]

PROGRAM 17

/* Program 17 : Write a program to create a mysql database.


Name : SAKSHAM GUPTA
Roll No 03714202023
*/

<?php
$servername = "localhost";
$username = "SAKSHAM";
$password = "123";

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

if ($conn->connect_error)
{
die("Connection failed: ". $conn->connect_error);
}
else
{
echo 'connection created successfully <br><br>';
}

$sql = "CREATE DATABASE student";


if ($conn->query($sql) === TRUE)
{
echo "Database created successfully";
}
else
{
echo "Error creating database: ". $conn->error;
}

$conn->close();
?>

OUTPUT:

SAKSHAM GUPTA [Roll No: 03714202023] pg. 53


Practical – I [WEB BASED PGM Lab] [BCA-104]

PROGRAM 18

/* Program 18 : Write a program to select all the records and display it in table. CO1,
CO2
Name : SAKSHAM GUPTA
Roll No 03714202023
*/

<?php
$servername = "localhost";
$username = "SAKSHAM";
$password = "123";
$dbname = "student";

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

if ($conn->connect_error)
{
die("Connection failed: ". $conn->connect_error);
}
else
{
echo "DATABASE Connected successfully<br><br>";
}

$sql = "SELECT * FROM student_data";


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

echo "<h4>Student Data</h4>";


echo "<table border='1'>
<tr>
<th>sno.</th>
<th>f_name</th>
<th>l_name</th>
<th>course</th>
</tr>";

while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>". $row["Sno."]. "</td>";
echo "<td>". $row["F_NAME"]. "</td>";
echo "<td>". $row["L_NAME"]. "</td>";
echo "<td>". $row["COURCE"]. "</td>";
echo "</tr>";

SAKSHAM GUPTA [Roll No: 03714202023] pg. 54


Practical – I [WEB BASED PGM Lab] [BCA-104]

echo "</table>";

$conn->close();
?>

OUTPUT:

SAKSHAM GUPTA [Roll No: 03714202023] pg. 55


Practical – I [WEB BASED PGM Lab] [BCA-104]

PROGRAM 19

/* Program 19 : Write a program to modify (delete/modify/add) a table.


Name : SAKSHAM GUPTA
Roll No 03714202023
*/

UPDATE.HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>sql alter table</title>
<style>

form {
width: 300px;
margin: 0 auto;
padding: 20px;
border: 1px solid
#ccc; border-radius:
5px;
}

label {
display: block;
margin-bottom: 5px;
}

input[type="text"],
input[type="number"],
select {
width: 100%;
padding: 5px;
margin-bottom: 10px;
border: 1px solid
#ccc; border-radius:
3px;
}
SAKSHAM GUPTA [Roll No: 03714202023] pg. 56
Practical – I [WEB BASED PGM Lab] [BCA-104]

input[type="submit"]
{ background-color:
#4CAF50; color: white;
padding: 10px 20px;
border: none;
border-radius: 3px;
cursor: pointer;
}

input[type="submit"]:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<form action="update.php" method="post">
<label for="action">Action:</label>
<select name="action">
<option value="add">Add</option>
<option value="modify">Modify</option>
<option value="delete">Delete</option>
</select>
<br>
<label for="id">sno.</label>
<input type="number" name="sno." id="sno.">
<br>
<label for="fname">First Name:</label>
<input type="text" name="fname" id="fname">
<br>
<label for="lname">Last Name:</label>
<input type="text" name="lname" id="lname">
<br>
<label for="course">Course:</label>
<input type="text" name="course" id="course">
<br>
<input type="submit" value="Submit">

SAKSHAM GUPTA [Roll No: 03714202023] pg. 57


Practical – I [WEB BASED PGM Lab] [BCA-104]

</form>
</body>
</html>

UPDATE.PHP
<?php
$servername = "localhost";
$username = "SAKSHAM";
$password = "123";
$dbname = "student";

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

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

switch ($_POST['action'])
{
case 'delete':

$Sno = $_POST['Sno.'];
$sql = "DELETE FROM student_data WHERE Sno. = $Sno";
if ($conn->query($sql) === TRUE)
{
echo "Record deleted successfully";
}
else
{
echo "Error deleting record: ". $conn->error;
}
break;

case 'modify':
$Sno = $_POST['Sno.'];

SAKSHAM GUPTA [Roll No: 03714202023] pg. 58


Practical – I [WEB BASED PGM Lab] [BCA-104]

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$course = $_POST['course'];
$sql = "UPDATE student_data
SET F_NAME = '$fname', L_NAME = '$lname', COURCE = '$course'
WHERE Sno = $Sno";
if ($conn->query($sql) === TRUE)
{
echo "Record updated successfully";
}
else
{
echo "Error updating record: ". $conn->error;
}
break;

case 'add':

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$course = $_POST['course'];
$sql = "INSERT INTO student_data (F_NAME, L_NAME, COURCE)
VALUES ('$fname', '$lname', '$course')";
if ($conn->query($sql) === TRUE)
{
echo "New record created successfully";
} else
{
echo "Error: ". $sql. "<br>". $conn->error;
}
break;
default:

echo "Invalid action";


}
$conn->close();
?>

SAKSHAM GUPTA [Roll No: 03714202023] pg. 59


Practical – I [WEB BASED PGM Lab] [BCA-104]

OUTPUT :

SAKSHAM GUPTA [Roll No: 03714202023] pg. 60


Practical – I [WEB BASED PGM Lab] [BCA-104]

PROGRAM 20

/* Program 20 : Create a dynamic website by incorporating the following functionalities


:Implement a basic registration and login system, with styling ,Make the database
connection, Make a connection to a MySQL database, and log in with valid credentials.
,Create Dynamic, interactive and database - Driven web application using php & mysql.

Name : SAKSHAM GUPTA


Roll No 03714202023
*/

REGISTER.HTML

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<h2>REGISTRATION FORM</h2>
<div class="container">
<h2>Register now!</h2>
<form action="process_form.php" method="post">
<label for="username">Username:</label><br>
<input type="text" id="username" name="username" required><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" required><br>
<label for="password">Password:</label><br>
<input type="password" id="password" name="password" required><br>
<button type="submit">Register</button>
</form>
</div>
</body>
</html>

STYLS.CSS
body
{
background-color: lightgray;
}

.container {

SAKSHAM GUPTA [Roll No: 03714202023] pg. 61


Practical – I [WEB BASED PGM Lab] [BCA-104]

width: 400px;
padding: 16px;
background-color: white;
margin: 0 auto;
margin-top: 50px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}

.container h2
{ text-align:
center;
margin-bottom: 20px;
}

.container label
{ display: block;
margin-bottom: 5px;
}

.container input[type="text"],.container input[type="email"],.container


input[type="password"] {
width: 95%;
padding: 10px;
margin-bottom: 10px;
}

.container button[type="submit"]
{ width: 100%;
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
}

h2 {
font-size: 24px;
color: #333;
margin-bottom: 20px;
text-align: center;
}

button[type="submit"] {
width: 50%;
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
display: block;

SAKSHAM GUPTA [Roll No: 03714202023] pg. 62


Practical – I [WEB BASED PGM Lab] [BCA-104]

margin: 0 auto;

p{
text-align: center;
}

PROCESS_FORM.PHP
<!DOCTYPE html>
<html lang="en">
<head>
<title>registration</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<?php
session_start();

$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];

echo "<p>Registration successful!</p>";

echo "<br><br><form action='login_form.html' method='post'>";


echo "<button type='submit'>login now!</button>";
echo "</form>";

?>

<?php
$servername = "localhost";
$username = "SAKSHAM";
$password = "123";
$dbname = "USER_DETAILS";

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

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

$stmt = $conn->prepare("INSERT INTO users (username, email, password) VALUES


(?,?,?)");
$stmt->bind_param("sss", $username, $email, $password);

SAKSHAM GUPTA [Roll No: 03714202023] pg. 63


Practical – I [WEB BASED PGM Lab] [BCA-104]

$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$stmt->execute();

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

LOGIN_FORM.HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Login Form</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
</head>
<body>
<h2>LOGIN PAGE</h2>
<div class="container">
<h2>Enter your details</h2>
<form action="process_login.php" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br><br>
<label for="pword">Password:</label>
<input type="password" id="pword" name="password"><br><br>
<button type="submit">Login</button>
</form>

</div>
</body>
</html>

PROCESS_LOGIN.PHP
<!DOCTYPE html>
<html lang="en">
<head>
<title>LOGGING IN</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>

SAKSHAM GUPTA [Roll No: 03714202023] pg. 64


Practical – I [WEB BASED PGM Lab] [BCA-104]

<?php
session_start();

// Connect to the database


$host = 'localhost';
$db = 'USER_DETAILS';
$user = 'SAKSHAM';
$pass = '123';

$conn = new mysqli($host, $user, $pass, $db);

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

// Check if the username and password match a record in the database


$result = $conn->query("SELECT * FROM users");

$validLogin = false;

while ($row = $result->fetch_assoc()) {


if ($_POST['username'] == $row['USERNAME'] && $_POST['password'] ==
@$row['password']) {
$validLogin = true;
break;
}
}

if ($validLogin) {
echo "<p>Login successful!</p>";
echo "<br><br><form action='logout.php' method='post'>";
echo "Lorem, ipsum dolor sit amet consectetur adipisicing elit. Obcaecati vero labore
cupiditate quibusdam et, nihil earum nobis, similique officiis incidunt veniam quaerat saepe
maiores laboriosam, voluptatibus harum itaque sit? Possimus. Lorem ipsum dolor sit amet
consectetur adipisicing elit. Quia modi alias amet enim blanditiis consectetur reprehenderit eos
repellendus corporis at. Architecto aspernatur voluptas explicabo aliquam dicta accusamus
eius, maiores quaerat! Lorem ipsum dolor sit amet consectetur adipisicing elit. Voluptatibus,
magni, perferendis labore sequi possimus repudiandae ea fugit eveniet sunt expedita aut quod
numquam accusamus nostrum illum. Qui ea quaerat eos.<br><br>";
echo "<button type='submit'>Logout</button>";
echo "</form>";
} else {
echo "<p>Invalid username or password.</p>";
echo "<br><br><form action='login_form.html' method='post'>";
echo "<button type='submit'>Try again!</button>";
echo "</form>";
}

SAKSHAM GUPTA [Roll No: 03714202023] pg. 65


Practical – I [WEB BASED PGM Lab] [BCA-104]

?>

</body>
</html>

LOGOUT.PHP
<!DOCTYPE html>
<html lang="en">
<head>
<title>LOGGED OUT</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>

<body>
<?php

session_start();
session_destroy();
echo "<p>You have been logged out.</p>";
echo "<form action='register.html' method='post'>";
echo "<br><br><button type='submit'>go to registration form</button>";
echo "</form>";
echo "<form action='login_form.HTML' method='post'>";
echo "<br><br><button type='submit'>go to login form</button>";
echo "</form>";
exit;
?>
</body>
</html>

SAKSHAM GUPTA [Roll No: 03714202023] pg. 66


Practical – I [WEB BASED PGM Lab] [BCA-104]

OUTPUT:

SAKSHAM GUPTA [Roll No: 03714202023] pg. 67


Practical – I [WEB BASED PGM Lab] [BCA-104]

SAKSHAM GUPTA [Roll No: 03714202023] pg. 68

You might also like