0% found this document useful (0 votes)
20 views20 pages

ECS751N Lab Manual

Lab manual

Uploaded by

SyedAliHussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views20 pages

ECS751N Lab Manual

Lab manual

Uploaded by

SyedAliHussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

IFTM UNIVERSITY MORADABAD

LAB MANUAL

Advance Web Technology Lab


ECS 751N

B.Tech (CSE) VII Semester

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF COMPUTER SCIENCE & APPLICATIONS
IFTM UNIVERSITY, MORADABAD

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Course Name: Advance Web Technology Lab Course Code: ECS751N


Course Instructor: Mr. Harpreet Singh Chawla
Lab Instructor: Mr. Aman
Session (2024-2025)
List of Programs
Sr No. Experiment Name Date Sign
1. Write a PHP program to demonstrate the use of Decision making control
structures using
a. If statement
b. If-else statement
c. Switch statement
2. Write a PHP program to demonstrate the use of Looping structures using- a)
While statement b) Do-while statement c) For statement d) Foreach statement
3. Write a PHP program for creating and manipulating- a) Indexed array b)
Associative array c) Multidimensional array
4. A. Write a PHP program to- • Calculate length ofstring. • Count the number of
words in string without using string functions
B. Write a simple PHP program to demonstrate use of various built-in string
functions
5. Write a simple PHP program to demonstrate use of simple function and
parameterized function.
6. Write a simple PHP program to create PDF document buy using graphics
concepts
7. Write a PHP program to a) Inherit members of super class in subclass. b)
Create constructor to initialize object of class by using object oriented concepts.
8. Develop web page with data validation

9. Write a Program in php to Demonstrate session management


Experiment - 1

Objective: Write a PHP program to demonstrate the use of Decision making


control structures using a. If statement b. If-else statement c. Switch statement.

CONCEPT: Controls statements are used to control are used to control the flow of execution of
program based on certain conditions. These are used to cause the flow of execution to advance and branch
based on changes to the state of program.
1. if statement
2. if-else statement
3. If..elseif..else statement
1. if: if statement is simple decision making statement. It is used to decide whether a certain
statement or block of statements will be executed or not. i.e. if a certain condition is true then the block
will be executed otherwise not.
Syntax:
if(condition)
{
//Statement to execute if the condition is true.
}

2. if-else statement: The if statement alone tells us that if a condition is true it will execute a block of
statements and if the condition false, else block will be executed.
if( condition)
{
//Statement to execute if the condition is true
}
else
{
//Statement to execute if the condition is false
}
3. switch statement
The switch statement is used to perform different actions based on different conditions. Use the switch
statement to select one of many blocks of code to be executed.
switch(n)
{
case label1:
//code to be executed if n=label1; break;
case label2:
//code to be executed if n=label2; break;
case label3:
//code to be executed if n=label3; break;
...
default:
//code to be executed if n is different from all labels;

Source Code:
If Statement:
<html>
<head>
<title> Basic If Statements </title>
</head>
<body>
<h1>Day Report</h1>
<?php
$day = 'sunday';
if ($day == 'sunday') {
echo '<p>today is sunday.go to sleep.</p>';
}?>
</body>
</html>
If Else
<html>
<head>
<title> If Else</title>
</head>
<body>
<h1>Day Report</h1>
<?php
$day = 'monday';
if ($day == 'sunday') {
echo '<p>Today is sunday. go to sleep.</p>';
} else {
echo '<p>today is Monday the student are ready to go school.</p>';
}?>
</body>
</html>
Switch Case:
<html>
<body>
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}?>
</body></html>
Experiment - 2

Objective: Write a PHP program to demonstrate the use of Looping structures


using- a) While statement b) Do-while statement c) For statement d) Foreach
statement
CONCEPT: Loops allow you to execute a block of code multiple times without rewrite the code. This is
useful when working with repetitive tasks, such as:
 Iterating through arrays or data structures
 Performing an action a specific number of times
 Waiting for a condition to be met before proceeding
PHP for loop is used when you know exactly how many times you want to iterate through a block of
code. It consists of three expressions:
 Initialization: Sets the initial value of the loop variable.
 Condition: Checks if the loop should continue.
 Increment/Decrement: Changes the loop variable after each iteration.
While loop
while (condition is true)
{
//code to be executed;
}

Do..While loop
do
{
//code to be executed;
} while (condition is true);

For loop
for (init counter; test counter; increment counter)
{
//code to be executed for each iteration;
}

Source Code:
Do While Loop:
<html>
<body>
<?php
$a = 1;
do {
echo "The number is: $a <br>";
$a++;
} while ($a <= 10);
?>
</body>
</html>
While Loop
<html>
<body>
<?php
$s = 1;
while($s <= 7)
{
echo "The number is: $s <br>";
$s++;
}
?>
</body>
</html>
For Loop:
<html>
<body>
<?php
$d = 0;
$e = 0;
for( $i = 0; $i<5; $i++ )
{
$d += 10;
$e += 5;
}
echo ("At the end of the loop d = $d and e = $e" );
?>
</body>
</html>
Experiment - 3

Objective: Write a PHP program for creating and manipulating- a) Indexed array
b) Associative array c) Multidimensional array
CONCEPT:
There are basically three types of arrays in PHP:
 Indexed or Numeric Arrays: An array with a numeric index where values are stored linearly.
 Associative Arrays: An array with a string index where instead of linear storage, each value can
be assigned a specific key.
 Multidimensional Arrays: An array which contains single or multiple array within it and can be
accessed via multiple indices.
Indexed or Numeric Arrays:
These type of arrays can be used to store any type of elements, but an index is always a number.
By default, the index starts at zero.
$cars = array(“Volvo”,”BMW”,”Toyota”); OR
The index can be assigned manually:

$cars[0] = “Volvo”;

$cars[1] = “BMW”;

$cars[2] = “Toyota”;
Associative Arrays:
Associative arrays are arrays that use named keys that you assign to them. There are two ways
to create an associative array:
$total = array(“fyif”=> “58”, “syif” => “61”, “tyif”=> “46”);
OR
$total[‘fyif’]= “58”;

$total[‘syif’]= “61”;

$total[‘tyif’]= “46”;
Multidimensional Arrays:
A two-dimensional array is an array of arrays (a three-dimensional array is an array of arrays of arrays).
Example:
$student = array (
array("xyz",3401,86.45),

array("abc",3402,87.42),

array("pqr",3403,85.43),

);

Source Code:
Indexed or Numeric Arrays:
<html>
<body>
<?php
$cars= array("Volvo", "BMW", "Toyota");
$arrlength=count($cars);

for($x= 0;$x<$arrlength;$x++)
{
echo $cars[$x]; echo "<br>";
}
?>
</body>
</html>
Associative Arrays:
<html>
<body>
<?php
$total = array("fyif"=>"58", "syif"=>"61", "tyif"=>"46"); foreach($total as $x => $x_value)
{
echo "Key=" . $x . ", Value=" . $x_value; echo "<br>";
}
?>
</body>
</html>
Multidimensional Arrays:
<html>
<body>
<?php
$student = array (
array("XYZ",3401,86.45),
array("PQR",3402,87.42),
array("ABC",3403,85.43),
);
for ($row = 0; $row < 3; $row++) {
echo "<p><b>Row number $row</b></p>"; echo "<ul>";
for ($col = 0; $col < 3; $col++) {
echo "<li>".$student[$row][$col]."</li>";
}
echo "</ul>";
}
?>
</body>
</html>
Experiment - 4

Objective: A. Write a PHP program to- • Calculate length of string. • Count the
number of words in string without using string functions
B. Write a simple PHP program to demonstrate use of various built-in string
functions
CONCEPT:
Sr.
PHP function Use Example
No.
The PHP strlen() function returns the length of a
1 strlen() echo strlen("Hello world!");
string.
The PHP str_word_count() function counts the echo str_word_count("Hello
2 str_word_count()
number of words in a string. world!");

3 strrev() The PHP strrev() function reverses a string. echo strrev("Hello world!");

The PHP strpos() function searches for a specific


text within a string. If a match is found, the echo strpos("Hello
4 strpos() function returns the character position of the world!", "world"); //
first match. If no match is found, it will return outputs 6
FALSE.

The PHP str_replace() function replaces some echo str_replace("world", "bvit",


5 str_replace() characters with some other characters in a "Hello world!"); // outputs Hello
string. bvit!

Convert the first character of each word to echo ucwords("Welcome to php


6 ucwords()
uppercase world");// Welcome To Php World
echo strtoupper("information
strtoupper() Convert a string a uppercase letters technology");// INFORMATION
7
TECHNOLOGY
echo strtolower("INFORMATION
8 strtolower() Convert a string to lowercase letters TECHNOLOGY")// information
technology
Repeating a string with a specific number of echo
9 str_repeat()
times str_repeat("*",10);//**********

Compare two strings (case-sensitive). If this


function return 0, the two strings are equals. If echo strcmp("Hello world!",
10 strcmp()
this function returns any negative or positive "Hello world"); //0
nubers, the two strings
Source Code:
<html>
<body>
<?php
$str = "hello world";
// using strlen method
echo "Length of string is: ". strlen($str);
echo "<br>";
echo "Reverse: ". strrev($str);
echo "<br>";
echo "Position of 'World' in string: ". strpos($str, 'World');
echo "<br>";
echo ucwords($str);
echo "<br>";
echo strtoupper($str);
echo "<br>";
echo strtolower($str);
echo "<br>";
echo implode(" ", $arr) . "<br>";
echo "<br>";
echo implode("-", $arr) . "<br>";
echo "<br>";
$capitalizedName = ucfirst($str);
echo $capitalizedName;
echo "<br>";
$echo(addcslashes("Hello World!","W"));
echo "<br>";
?>
<\html>
<\body>
Experiment - 5

Objective: Write a simple PHP program to demonstrate use of simple function


and parameterized function.
CONCEPT:

Functions and types.


 PHP functions are similar to other programming languages. A function is a piece
of code which takes one more input in the form of parameter and does some
processing and returns a value.
 They are built-in functions but PHP gives you option to create your own functions
as well. A function will be executed by a call to the function. You may call a
function from anywhere within a page.
Syntax:

function function_name()
{
//body of function.
}
Function Types
1. Simple function
2. Function with parameter
3. Anonymous function.
Source Code:
Noraml Function:
<html>
<body>
<?php
function myMessage() {
echo "Hello world!";
}
myMessage();
?>
</body>
</html>
Function with Return Type:
<html>
<body>
<?php
function sum($x, $y) {
$z = $x + $y;
return $z;
}
echo "5 + 10 = " . sum(5,10) . "<br>";
echo "7 + 13 = " . sum(7,13) . "<br>";
echo "2 + 4 = " . sum(2,4);
?>
</body>
</html>
</html>
Function with default argument value
<html>
<body>
<?php
function setHeight($minheight = 50) {
echo "The height is : $minheight <br>";
}
setHeight(350);
setHeight();
setHeight(135);
setHeight(80);
?>
</body>
</html>
Function with variable number of arguments
<html>
<body>
<?php
function sumMyNumbers(...$x) {
$n = 0;
$len = count($x);
for($i = 0; $i < $len; $i++) {
$n += $x[$i];
}
return $n;
}
$a = sumMyNumbers(5, 2, 6, 2, 7, 7);
echo $a;
?>
</body>
</html>
Anonymous Function
<html>
<body>
<?php
//Defining Anonymous function
$a=function() {
echo "Anonymous function";
};
//Calling Anonymous function
$a();
?>
</body>
</html>
Experiment - 6

Objective: Write a simple PHP program to create PDF document buy using
graphics concepts
CONCEPT:
 PHP uses a standard code to display the pdf file in web browser. The process of displaying pdf
involves location of the PDF file on the server and it uses various types of headers to define
content composition in form of type, Disposition, Transfer-Encoding etc. PHP passes the PDF
files to read it on the browser. Browser either shows it or download it from local host server then
display pdf.
 Note: PHP is not actually reading the PDF file. It does not recognize File as pdf. It only passes
the PDF file to the browser to be read there. If copy the pdf file inside htdocs folder of XAMPP
then it does not need to specify the file path.
 FPDF is a PHP class which allows to generate PDF files with pure PHP, that is to say without
using the PDFlib library. F from FPDF stands for Free: you may use it for any kind of usage and
modify it to suit your needs.
 FPDF has other advantages: high level functions. Here is a list of its main features:
1) Choice of measure unit, page format and margins
2) Page header and footer management
3) Automatic page break
4) Automatic line break and text justification
5) Image support (JPEG, PNG and GIF)
6) Colors
7) Links
8) TrueType, Type1 and encoding support
9) Page compression

Source Code:
<?php

require('fpdf/fpdf.php');

class PDF extends FPDF {

// Page header
function Header() {

// Add logo to page


$this->Image('gfg1.png',10,8,33);

// Set font family to Arial bold


$this->SetFont('Arial','B',20);

// Move to the right


$this->Cell(80);

// Header
$this->Cell(50,10,'Heading',1,0,'C');

// Line break
$this->Ln(20);
}

// Page footer
function Footer() {

// Position at 1.5 cm from bottom


$this->SetY(-15);

// Arial italic 8
$this->SetFont('Arial','I',8);

// Page number
$this->Cell(0,10,'Page ' .
$this->PageNo() . '/{nb}',0,0,'C');
}
}

// Instantiation of FPDF class


$pdf = new PDF();

// Define alias for number of pages


$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',14);

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


$pdf->Cell(0, 10, 'line number '
. $i, 0, 1);
$pdf->Output();

?>
Experiment - 7

Objective: Write a PHP program to a) Inherit members of super class in subclass.


b) Create constructor to initialize object of class by using object oriented
concepts.
CONCEPT:
Inheritance:
 Inheritance is mechanism of extending an existing class by inheriting class.
 When we inherit one class from another we say that inherited class is a subclass
and the class who has inherits is called parent class.
 In order to declare that one class inherits the code from another class, we use the
extends keyword.
Syntax:
class Parent
{
// The parent’s class code.
}
class child extends Parent

{
// The class can use the parent’s class code
}

Constructor:

 Constructors are special member functions for initialize variables on the newly created object
instances from a class.
 When creating a new object, it’s useful to setup certain aspects of the object at the same time.
For example, you might want to set some properties to initial values fetch some information
from a database to populate the object, or register the object in some way.
 Constructor are of two types Default and parameterized constructor.

Source Code:
<html>
<body>
<?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}.";
}
}

// Strawberry is inherited from Fruit


class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry? ";
}
}
$strawberry = new Strawberry("Strawberry", "red");
$strawberry->message();
$strawberry->intro();
?>
<\html>
<\body>
Constructor
<html>
<body>
<?php
class emp
{
private $fname;
private $lname;
public function __construct($fname,$lname)
{
$this->fname=$fname;
$this->lname=$lname;
}
public function showName()
{
echo “My name is”.$this->fname.” ”.$this->lname;
}
}
$sid=new emp(“Ramesh”,”Patil”);
$sid->showName();
?>
<\html>
<\body>
Experiment - 8

Objective: Develop web page with data validation.


CONCEPT:
 Form validation is required to prevent web form abuse by malicious users. Improper
validation of form data is one of the main causes of security vulnerabilities. It exposes
your website to attacks such as header injections, cross-site scripting, and SQL
injections.
 header injection attacks can be used to send email spam from your web server
 cross-site scripting may allow an attacker to post any data to your site
 SQL injection may corrupt your database backend

PHP provides some inbuilt function, using these functions that input data can be validated.

 empty() function will ensure that text field is not blank it is with some data, function
accepts a variable as an argument and returns. It TRUE when the text field have filled
with some data otherwise it return FALSE.
 is_numeric() function will ensure that data entered in a text field is a numeric
value, the function accepts a variable as an argument and returns TRUE when the
text field is submitted with numeric value otherwise it return FALSE.
 preg_match() function is specifically used to performed validation for entering text in
the text field, function accepts a “regular expression” argument and a variable as an
argument which has to be in specific pattern. Typically it is for validating email, IP
address and Pin code information in a form.

Source Code:
<html>
<head>
</head>
<body>

<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$website = test_input($_POST["website"]);
$comment = test_input($_POST["comment"]);
$gender = test_input($_POST["gender"]);
}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

<h2>PHP Form Validation Example</h2>


<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<br><br>
E-mail: <input type="text" name="email">
<br><br>
Website: <input type="text" name="website">
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="other">Other
<br><br>
<input type="submit" name="submit" value="Submit">
</form>

<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>

</body>
</html>
Experiment - 9

Objective: Write a Program in php to Demonstrate session management.


CONCEPT: Demonstrate session management
This practical is expected to develop the following skills.
 Write a program to start a session
 Develop a program get session variables
 Write a program to destroy a session
A session is a way to store information (in variables) to be used across multiple pages. Unlike a cookie,
the information is not stored on the users computer.
What is a PHP Session?
When you work with an application, you open it, do some changes, and then you close it. This is
much like a Session. The computer knows who you are. It knows when you start the application and
when you end. But on the internet there is one problem: the web server does not know who you are or
what you do, because the HTTP address doesn't maintain state. Session variables solve this problem by
storing user information to be used across multiple pages (e.g. username, favorite color, etc). By default,
session variables last until the user closes the browser. So, Session variables hold information about one
single user, and are available to all pages in one application.

Start a PHP Session


A session is started with the session_start () function.
Session variables are set with the PHP global variable: $_SESSION.
Now, let's create a new page called "demo_session1.php". In this page, we start a new PHP ession and
set some session variables:
Source Code:
Starting a Session:
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>

</body>
</html>
Get PHP Session Variable Values
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Echo session variables that were set on previous page
echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
?>

</body>
</html>
Modify a PHP Session Variable
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// to change a session variable, just overwrite it
$_SESSION["favcolor"] = "yellow";
print_r($_SESSION);
?>

</body>
</html>
Destroy a PHP Session
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// remove all session variables
session_unset();

// destroy the session


session_destroy();
?>

</body>
</html>

You might also like