Nothing Special   »   [go: up one dir, main page]

WBP 5 Sem Unit 01 & 02

Download as pdf or txt
Download as pdf or txt
You are on page 1of 167

Syllabus

Recommended Books
UNIT-01 :

PART –A : Introduction to
Web Application
Topic -1

Introductions to Web Applications


Web Application
 A Web application (Web app) is an application
program that is stored on a remote server and
delivered over the Internet through a browser
interface.
 For a web app to operate, it needs a Web server,
application server, and a database. Web servers
manage the requests that come from a client,
while the application server completes the
requested task. A database can be used to store
any needed information.
s
Web programming refers to the writing, markup and coding
involved in Web development, which includes Web content,
Web client and server scripting and network security
Web Application
Architecture
Client Server Model
Server Applications (Software)
 Management and maintenance of
Data including
 User login data
 Application data
 Data processing
 Centralized
 Access via Login
Client Applications (Software)
 Provides user interface
 Stores some settings
 Can do some data processing
 Little to no application data
storage
 Same view of data no matter where
you login
Client-Server Advantages
 Centralized Data Storage
 No data redundancy (no duplication of data)
 Reduces data dependencies
 If data is stored on each user’s system and each
system is different than data depends on how the
user system is designed
 Data can not be shared easily if such dependencies
exist
First Web Applications
 1993 – Rob McCool proposed a framework called
 CGI (Common Gateway Interface)
 Data passed from a web browser to the server
 GET - passed via URL variables
 POST - passed via HTML forms
 Web server daemon (httpd) could then make
remote system calls
 Example
 Web server could run a C++ program and write the
output to public HTML folder
 Web server would send response back with location of
the output.
Intro to HTML
Scripting : Client Side vs Server Side
Scripting Languages
❑ A scripting language is a programming language that
supports the writing of scripts
“A script is a program that automates the execution of tasks
which could alternatively be executed one-by-one by a
human operator”

❑ Where to use scripts?

Software applications(Php,Asp,Jsp,Python,Perl,etc)
Web pages (Javascript,VB script,php etc)
FEATURES
❑ Ease of use: scripting languages are intended to be
very
fast to pick up

❑ Interpreted from source code: to give the fastest


turnaround between the scripting phase and the
execution phase
EXECUTION
❑ Scripts are written and executed on-the-fly
Without compiling
Without linking

❑ Differences with the software environment code


Scripts are created and/or modified by the person who
executes them
Software code is compiled and distributed in machine code
form; the user does not access to it and modify it

❑ A script is used in order to extend application functionalities,


or to execute simple tasks by using pre-existing components
ADVANTAGES OF SCRIPTING
❑ Easy to learn and use
❑ Minimum programming knowledge or
experience required
❑ Allow complex tasks to be performed in
relatively few steps
❑ Allow simple creation and editing in a
variety of text editors
❑ Allow the addition of dynamic and
interactive activities to web pages
❑ Editing and running code is fast.
SCRIPTING

SCRIPTING

CLIENT-SIDE SEVER-SIDE
SERVER SIDE SCRIPTING
❑ Server side scripting is used to create dynamic
pages based a number of conditions when the
users browser makes a request to the server

❑ The Web Server executes the server side scripting


that produces the page to be sent to the browser.

❑ Server executes server-side scripts to send out a


page but it does not execute client-side scripts.
SERVER SIDE SCRIPTING
❑ Server side scripting is used to connect to the
databases that reside on the web server.

❑ Server side scripting can access the file system


residing at the web server.

❑ Response from a server-side script is slower as


compared to a client-side script because the scripts
are processed on the remote computer.
Example
SERVER SIDE SCRIPTING-
advantages
1. It does not require the user to download plugins like Java or Flash
(client-side scripting).

2. Your scripts are hidden from view. Users only see the HTML output,
even when they view the source.
Disadvantages
The scripts can be used by attackers to gain access
to the server. Because the scripts respond to URL
input, changing the URL to something that exploits a
security hole can give the user server access,
sometimes even as root.
CLIENT-SIDE SCRIPTING
❑ Client side scripting is used when the users
browser already has all the code and the page is
altered on the basis of the users input.

❑ The Web Browser executes the client side


scripting that resides at the user’s computer.
CLIENT-SIDE SCRIPTING
❑ The browser receives the page sent by the
server and executes the client-side scripts.

❑ Client side scripting cannot be used to connect


to the databases on the web server.

❑ Client side scripting can’t access the file


system that resides at the web server.
CLIENT-SIDE SCRIPTING
❑ The files and settings that are local at the user’s
computer can be accessed using Client side
scripting.

❑ Response from a client-side script is faster as


compared to a server-side script because the
scripts are processed on the local computer.

❑ Examples of Client side scripting languages :


Javascript, VB script, etc.
Example
Advantages of Client Side Script

Allow for more interactivity by immediately responding to


users' actions
• Execute quickly because they don't require a trip to the server
•May improve the usability of Web sites for users whose
browsers support scripts
Can give developers more control over the look and behavior

of their Web widgets (note that this can be an advantage or


disadvantage)
Can be substituted with alternatives (for example, HTML) if

users' browsers do not support scripts


Disadvantages of Client Side Script
1.Not all browsers support scripts, therefore, users might experience errors if no
alternatives have been provided.

2. Different browsers and browser versions support scripts differently, thus more
quality assurance testing is required.

3.More development time and effort might be required (if the scripts are not already
available through other resources).

4. Developers have more control over the look and behavior of their Web widgets;
however, usability problems can arise if a Web widget looks like a standard control
but behaves differently or vice-versa
Web Server
WEB SERVERS

Web Server Definition


▪ A Web server is a program that generates and
transmits responses to client requests for Web
resources.
▪ Handling a client request consists of several
key steps:
➢ Parsing the request message
➢ Checking that the request is authorized
➢ Associating the URL in the request with a file name
➢ Constructing the response message
➢ Transmitting the response message to the requesting client
WEB SERVERS

Web Server Definition


▪ The server can generate the response message
in a variety of ways:
➢ The server simply retrieves the file associated with
the URL and returns the contents to the client.
➢ The server may invoke a script that communicates
with other servers or a back-end database to
construct the response message.
Web Server - Example
The URL

Where you place your web site

40
WEB SERVERS

Web Site versus Web Server


▪ Web site and Web server are different:
➢ A Web site consists of a collection of Web pages
associated with a particular hostname.
➢ A Web server is a program to satisfy client
requests for Web resources.
WORKING OF WEB SERVERWEB SERVERS
Steps in Handling a Client Request:-

▪ A Web server proceeds through the following


steps in handling an HTTP request:
➢ Read and parse the HTTP request message
for example GET the resource /foo.htm
➢ Translate the URL to a file name
for example the resource be located in the base directory
such as /www, where the URL
http://www.bar.com/foo/index.html corresponds to
the file of www/foo/index.html
➢ Determine whether the request is authorized
➢ Generate and transmit the response that includes
header to show the status information
A Web server communicating with
several HTTP clients
WEB SERVERS

Access Control
▪ A Web server may limit which users can access
certain resources. Access control requires a
combination of authentication and
authorization.
➢ Authentication identifies the user who originated the
request.
➢ Authorization determines which users have access
to a particular resource.
WEB SERVERS

AUTHENTICATION
▪ Most client-server systems authenticate a user by asking for
a name and password.

▪ Web server must perform authentication for every request for


a resource that has access restrictions.

▪ The server returns an HTTP response that indicates that the


request requires authorization.

▪ The response also identifies what kind of authentication is


required.
WEB SERVERS

AUTHORIZATION
▪ To control access to Web resources, the server must
employ an authorization policy.
▪ A policy typically expressed in terms of an access control
list that enumerates the users who are granted or denied
access to the resources.
▪ In addition to checking the user name, the server may
allow or deny access to the resource based on other
information associated with the HTTP request, such as
the host name or IP address of the requesting client.
▪ Authenticating HTTP requests can impose a heavy load
on the Web server.
Type of web servers:-

1.Local Web Server


2.Remote web Server
Local Web Servers

 As the name suggests a local server is a


privately owned machine most commonly used
by developers to store and test the web pages
that have server side scripting codes. In other
words when a developer installs web server
software on his current machine and uses it for
developing web applications, the machine will
be called a local web server. A local server can
be your laptop or Personal Computer giving you
the complete environment (software & hardware)
for developing a web application.
Remote Web Servers

 Opposite to the local server, remote server refers to a


computer that is remotely located having a web server
software, database and other resources to handle remote
requests sent by the users of a website. A remote server
may host single or multiple websites. It has to be powered
to process the scripting language codes in the web
pages. Usually the remote servers are high performance
computers having large memory and storage space to
handle multiple page requests of the users. The remote
web server responds to the user requests by transferring
the pages requested over the internet connectivity.
Difference between Local Server and a
Remote Server
UNIT 2:

Introduction to PHP
PHP Introduction

PHP is a recursive acronym for “PHP: Hypertext


Preprocessor” -- It is a widely-used open source
general-purpose scripting language that is
especially suited for web development and can
be embedded into HTML.
PHP Introduction

> PHP is a server-side scripting language


> PHP scripts are executed on the server
> PHP supports many databases (MySQL,
Oracle, Sybase, Generic ODBC, etc.)
> PHP is open source software
> PHP is free to download and use
PHP Introduction

> PHP runs on different platforms (Windows,


Linux, Unix, etc.)
> PHP is compatible with almost all servers used
today (Apache, IIS, etc.)
> PHP is FREE to download from the official PHP
resource: www.php.net
> PHP is easy to learn and runs efficiently on the
server side
Instead of lots of commands to output HTML (as
seen in C or Perl), PHP pages contain HTML with
embedded code that does "something" (like in the
next slide, it outputs "Hi, I'm a PHP script!").

The PHP code is enclosed in special start and end


processing instructions <?php and ?> that allow
you to jump into and out of "PHP mode."
PHP Introduction
PHP Introduction

PHP code is executed on the server, generating


HTML which is then sent to the client. The client
would receive the results of running that script, but
would not know what the underlying code was.

A visual, if you please...


PHP Introduction
PHP Getting Started

On windows, you can download and install


WAMP. With one installation and you get an
Apache webserver, database server and php.
http://www.wampserver.com

On mac, you can download and install MAMP.


http://www.mamp.info/en/index.html
PHP Hello World

Above is the PHP source code.


PHP Hello World

It renders as HTML that looks like this:


PHP Hello World

This program is extremely simple and you really


did not need to use PHP to create a page like this.
All it does is display: Hello World using the PHP
echo() statement.

Think of this as a normal HTML file which


happens to have a set of special tags available to
you that do a lot of interesting things.
PHP Comments

In PHP, we use // to
make a single-line
comment or /* and */ to
make a large comment
block.
DATA TYPES IN PHP
Data type Description
int, integer Whole numbers (i.e., numbers without a decimal point).
float, double Real numbers (i.e., numbers containing a decimal point).
string Text enclosed in either single ('') or double ("") quotes.
bool, Boolean True or false.
array Group of elements of the same type.
object Group of associated data and methods.
Resource An external data source.
NULL No value.

Fig. PHP data types.


Data Types (1)
o PHP has eight different data types – 5 basic and 2
composite types and 1 resource type
o The 5 basic data types are:
- integers : whole numbers in the range -2,147,483,648 to
+2,147,483,647 on 32-bit architecture. Can be octal (prefixed by
0), decimal and hexadecimal (prefixed by 0x)
- floating-point : real or doubles (decimal), can be written using
base 10 notation (e.g. 3900 == 3.9e3)
- strings : sequence of characters contained by single (‘..’) or
double (“..”) quotes. Within double quotes variables are replaced
by their values (interpolated). Escape sequences are used to
include special characters within double quoted strings (e.g. \” to
include a double quote and \\ to include a backslash)
- booleans : the boolean data type evaluates to either true or false
(e.g. $a == $b will evaluate to true if both variables contain the
same value, false otherwise
- null : a special data type that stands for a lack of value. Often
used to initialize or reset variables.
o
Data Types (2)
arrays : are a composite data type that collect values into a list. The values
of the array elements can be text, a number or even another array. Arrays
can be from 1 to any number of dimensions.
- arrays are referenced using square [ .. ] brackets.
<?php

$cities[0] = “London”;

$cities[1] = “Bath”;

$cities[2] = “Bristol”;

print (“I live in $cities[2].<br/>\n”);

?>

- an array can be indexed using a string value for the index (called
associative array or hash) and are useful in situations where there is a need
to collect different types of data into one array. (e.g. $userInfo [“name”] =
“Kevin”; )

- array elements containing other arrays are used to build multi-dimensional


arrays.
<?php

$modules = array(

“ISD”=>array(

“Joe”,

“Tim”,
Data Types (3)
o objects : are another composite data type fully supported by php.
- php supports standard object-oriented (OO) methodologies and
techniques such as
inheritance - the ability to derive new classes from existing ones and
inherit or override their attributes and methods.
encapsulation - the ability to hide data from users of the class (the
public, protected and private keywords.)
special methods - for instance, code that is automatically run when
a object is created (constructor) or destroyed (destructor).
polymorphism – overloading a function so that a function call will
behave differently when passed variables of different type.
- a good introducton to OO programming using php can be found at

o resources : are a data type that hold handles to external resources


such as open files or database connections.
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 69
2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4 <!-- Fig. 26.3: data.php -->
5 <!-- Demonstration of PHP data types -->
6
7 <html xmlns = "http://www.w3.org/1999/xhtml">
8 <head>
9 <title>PHP data types</title>
10 </head>
11
12 <body>
13
14 <?php Assign a string to variable
$testString
15
16 // declare a string, double and integer
17 $testString = "3.5 seconds";
Assign a double to variable
18 $testDouble = 79.2;
$testDouble
Assign an integer to variable
19 $testInteger = 12; $testInteger
20 ?>
21

EXAMPLE OF ASSIGNING DATA TYPES IN PHP


PHP Variables
> Variables are used for storing values, like text
strings, numbers or arrays.
> When a variable is declared, it can be used over
and over again in your script.
> All variables in PHP start with a $ sign symbol.
> The correct way of declaring a variable in PHP:
PHP Variables

> In PHP, a variable does not need to be declared


before adding a value to it.
> In the example above, you see that you do not
have to tell PHP which data type the variable is.
> PHP automatically converts the variable to the
correct data type, depending on its value.
PHP Variables

> A variable name must start with a letter or an


underscore "_" -- not a number
> A variable name can only contain alpha-numeric
characters, underscores (a-z, A-Z, 0-9, and _ )
> A variable name should not contain spaces. If a
variable name is more than one word, it should be
separated with an underscore ($my_string) or with
capitalization ($myString)
Superglobal Variables
Example of Super Global Variable
Constants In Php
 Sometimes, you don’t want a data item to be a variable.
For example, the value of pi shouldn’t change.
 The thing to do here is to create a constant, whose value
can’t be modified.
 You create a constant in PHP with the define function:
The define() function defines a constant.
Constants are much like variables, except for the following differences:
• A constant's value cannot be changed after it is set
• Constant names do not need a leading dollar sign ($)
• Constants can be accessed regardless of scope
• Constant values can only be strings and numbers
Syntax
define(name,value,case_insensitive)
Parameter Description
name Required. Specifies the name of the constant
value Required. Specifies the value of the constant
case_insensitive Optional. Specifies whether the
constant name should be case-insensitive. If set to TRUE,
the constant will be case-insensitive. Default is FALSE
(case-sensitive)
PHP Concatenation
> The concatenation operator (.) is used to put
two string values together.
> To concatenate two string variables together,
use the concatenation operator:
PHP Concatenation

The output of the code on the last slide will be:

If we look at the code you see that we used the


concatenation operator two times. This is because
we had to insert a third string (a space character),
to separate the two strings.
PHP Operators
Operators are used to operate on values. There
are four classifications of operators:

> Arithmetic
> Assignment
> Comparison
> Logical
PHP Operators
Airthmetic Operators with Examples
1. Addition :

2. Substraction :

3. Multiplication

4. Division
5. Modulus:

6. Increment :

a) Pre-Increment :

b) Post-increment:

c) Pre-decrement:

d) Post-decrement
Assignment Operators
The PHP assignment operators are used with numeric values to write a value to a
variable.

The basic assignment operator in PHP is "=". It means that the left operand gets set to the
value of the assignment expression on the right.
Assignment Operators with Examples

1. x=y

2. x += y

3. x -= y

4. x *= y
1. x /= y

2. x %= y
PHP Comparison Operators
Comparison Operators with Examples
1. ==

2. 2. === Identical

3. != or <> Not equal


4. !== Not identical

5. > Greater than

6. < Less than


7. >= Greater than or equal to

8. <= Less than or equal to

9. <=> Spaceship
PHP Logical Operators
Logical Operators with Examples
1. and

2. 2. or

1. 3. xor
4. &&

5. ||

6. !
PHP Conditional Statements
> Very often when you write code, you want to
perform different actions for different decisions.
> You can use conditional statements in your
code to do this.
> In PHP we have the following conditional
statements...
PHP Conditional Statements
> if statement - use this statement to execute
some code only if a specified condition is true
> if...else statement - use this statement to
execute some code if a condition is true and
another code if the condition is false
> if...elseif....else statement - use this statement
to select one of several blocks of code to be
executed
> switch statement - use this statement to select
one of many blocks of code to be executed
IF Statement
The following example will output "Have a nice
weekend!" if the current day is Friday:
Nested if Statement
if....else
Use the if....else statement to execute some code
if a condition is true and another code if a
condition is false.
PHP Conditional Statements

If more than one line


should be executed if a
condition is true/false,
the lines should be
enclosed within curly
braces { }
PHP - The if...elseif...else
Statement
PHP - The switch Statement
Use the switch statement to select one of many
blocks of code to be executed.
PHP Conditional Statements
For switches, first we have a single expression n
(most often a variable), that is evaluated once.

The value of the expression is then compared with


the values for each case in the structure. If there
is a match, the block of code associated with that
case is executed.

Use break to prevent the code from running into


the next case automatically. The default statement
is used if no match is found.
Example of Switch Statement
Loops in Php
Often when you write code, you want the same block of
code to run over and over again a certain number of
times. So, instead of adding several almost equal code-
lines in a script, we can use loops.

Loops are used to execute the same block of code again


and again, as long as a certain condition is true
Loops in Php
PHP Loops – Do ... While
PHP Loops – Do ... While
PHP Loops - For
PHP Loops - For
Parameters:
> init: Mostly used to set a counter (but can be
any code to be executed once at the beginning
of the loop)
> condition: Evaluated for each loop iteration. If
it evaluates to TRUE, the loop continues. If it
evaluates to FALSE, the loop ends.
> increment: Mostly used to increment a counter
(but can be any code to be executed at the end
of the loop)
PHP Loops - For
The example below defines a loop that starts with
i=1. The loop will continue to run as long as i is
less than, or equal to 5. i will increase by 1 each
time the loop runs:
PHP Loops - For
Arrays in Php
Arrays In PHP
 Most of our PHP examples to this point have involved
scalar variables (we did see a couple of example in the
first section of notes that made use of one of PHP’s
global associative arrays).
 Scalar variables can only hold a single value at a time.
For example, a variable $color could hold only a
single value such as red, at any point in time. The
variable could not be used to hold more than one color.
 Arrays are special types of variables that enable you to
store as many values as you want.
Note: Although you can technically make an array as large as you’d like, some built-in
array handling functions in PHP have an upper limit of 100,000 values. If you are storing
more data that this in your arrays and you need to use one of these functions, you will
either need to write your own function or split the data into multiple arrays.
Arrays In PHP
 Arrays are indexed, which means that each entry in the
array, called an element, is made up of a key and a
value.
 The key is the index position, beginning with 0 and
increasing incrementally by 1 with each new element in
the array.
 The value is whatever value you associate with that
position – a string, an integer, or whatever you want.
 In PHP you can think of an array as a filing cabinet and
each key/value pair as a file folder. The key is the label
written on the tab of the folder, and the value is what is
inside. What’s inside each folder can vary from folder to
folder.
Creating Arrays In PHP
 You can create an array using either the array()
function or the array operator [].
 The array() function is usually used when you want
to create a new array and populate it with more than one
element, all at the same time.
 The array operator is more often used when you want to
create a new array with just one element at the outset or
when you want to add to an existing array element.
 The examples on the following couple of pages illustrate
creating an array in PHP using these two techniques.
This version uses the
array() function to
create the array.
This version uses the array
operator [ ] to create the array.
Note that no index values are
specified, PHP will auto number for
you
This version also uses the array
operator [ ] to create the array.
Note that index values are specified
in this case.
Creating Arrays In PHP
 As shown in the example on page 44, PHP can
automatically index the array for you when you use the [
] operator to create the array.
 This is useful in that it eliminates the possibility that you
might misnumber the elements. The example on the
next page illustrates what happens if you misnumber the
elements in an array.
Misnumbering starts
here with no element 4
defined and then 6 too
is missed.
Multidimensional arrays in PHP
Multi-dimensional arrays are such type of arrays
which stores an another array at each index
instead of single element. In other words, define
multi-dimensional arrays as array of arrays. As
the name suggests, every element in this array
can be an array and they can also hold other sub-
arrays within. Arrays or sub-arrays in
multidimensional arrays can be accessed using
multiple dimensions.

Dimensions: Dimensions
of multidimensional array indicates
the number of indices needed to select an element. For
a two dimensional array two indices to select an
element.
Creating Associative Arrays In PHP
 The arrays we’ve seen so far have been numerically
indexed, meaning that they use an integer index position
as the key.

 Associative arrays utilize actual named keys. In PHP,


the named keys of an associative array are character
strings rather than numerical values. The string value is
used to look up or provide a cross-reference to the data
value.
Iterating through an Array
 A common iterative statement used with both sequential
and associative arrays is the foreach statement.
 The general syntax of the foreach statement is:
foreach ( arrayname as variable ) {
. . . Statements to repeat
}
 The first variable inside the parentheses is the variable
name representing the array and the second variable is
automatically set to the next array item at each iteration of
the loop. An example using a sequential array is shown
on the next page and one with an associative array on the
following page.
Sorting Arrays In PHP
Array Functions
Examples of Some Array Functions
PHP Regular Expressions
What is a Regular Expression?

 A regular expression is a sequence of


characters that forms a search pattern. When
you search for data in a text, you can use this
search pattern to describe what you are
searching for.
 A regular expression can be a single character,
or a more complicated pattern.
 Regular expressions can be used to perform all
types of text search and text replace operations.
The preg_match() function will tell you whether a
string contains matches of a pattern.

<?php
$str = "Visit Jims Rohini Sector 05";
$pattern = "/jims/i";
echo preg_match($pattern, $str);
?>
Using preg_match_all()

The preg_match_all() function will tell you how


many matches were found for a pattern in a
string.

<?php
$str = "We always pretend that we are listening
the lecture,but we are lost in day dreaming";
$pattern = "/we/i";
echo preg_match_all($pattern, $str);
?>
Using preg_replace()

The preg_replace() function will replace all of the


matches of the pattern in a string with another
string.
<?php
$str = "Visit Cafeteria for Time Pass!";
$pattern = "/Cafeteria/i";
echo preg_replace($pattern, "Corridors", $str);
?>
Quantifiers

You might also like