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

Unit 4 - Internet and Web Technology - WWW - Rgpvnotes.in

This document discusses XML (Extensible Markup Language) and its uses. XML is a markup language that is platform and software independent, allowing data to be stored, transported, and shared across different systems. It simplifies data sharing, transport, and availability. The document then discusses XML key components, using XML with applications, transforming XML using XSL and XSLT, and introduces PHP with examples of decisions and looping structures.

Uploaded by

Web Login
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)
103 views

Unit 4 - Internet and Web Technology - WWW - Rgpvnotes.in

This document discusses XML (Extensible Markup Language) and its uses. XML is a markup language that is platform and software independent, allowing data to be stored, transported, and shared across different systems. It simplifies data sharing, transport, and availability. The document then discusses XML key components, using XML with applications, transforming XML using XSL and XSLT, and introduces PHP with examples of decisions and looping structures.

Uploaded by

Web Login
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/ 14

Program : B.

Tech
Subject Name: Internet and Web Technology
Subject Code: CS-504
Semester: 5th
Downloaded from www.rgpvnotes.in

Unit-IV Subject Notes

Subject Code-CS 504 Subject Name- Internet & Web Technology

Introduction to XML:

XML is a mark-up language which is used for storing and transporting data. XML doesn’t
depend on the platform and the software (programming language). You can write a
program in any language on any platform (Operating System) to send, receive or store data
using XML.

4.1 Uses of XML:

This is just to show you, how a XML looks. You need not to give too much emphasis on this
right now because we will learn this in detail later. For now, you can see it as a document
that can be used for sending sender’s name (the value inside <from> tag), receiver’s name
(the value inside <to> tag) and the message (the value inside <msg> tag).

<?xml version="1.0" encoding="UTF-8"?>


<message>
<to>MyReader</to>
<from>Chaitanya</from>
<msg>Welcome to beginnersbook.com</msg>
</message>

The top line <?xml version="1.0" encoding="UTF-8"?> is called XML prolog.

4.2 Simple XML

 It simplifies data sharing


 It simplifies data transport
 It simplifies platform changes
 It simplifies data availability

Many computer systems contain data in incompatible formats. Exchanging data between
incompatible systems (or upgraded systems) is a time-consuming task for web developers.
Large amounts of data must be converted, and incompatible data is often lost.

XML stores data in plain text format. This provides a software- and hardware-independent
way of storing, transporting, and sharing data.

XML also makes it easier to expand or upgrade to new operating systems, new applications,
or new browsers, without losing data.

With XML, data can be available to all kinds of "reading machines" like people, computers,
voice machines, news feeds,

Page no: 1 Get real-time updates from RGPV


Downloaded from www.rgpvnotes.in

4.3 XML Key Components

1. XML Base
Override the default URI of a document or any part of a document starting at a given
element;
2. Stylesheets in XML
Associate an XSLT transformation with an XML document, for example so that a Web
browser will format it;
3. XLink
A vocabulary for hypertext in XML;
4. xml:id
Identify an XML attribute or element as containing a name that can be used as a
unique identifier within a document;
5. XInclude
Include all or part of other text or XML documents, or duplicate part of the current
XML document;
6. XPointer
This is a framework for different ways to point into XML documents, and is used by
XLink;
7. XForms
A more powerful cousin to HTML forms;
8. XML Events, XHTML Modularization
Specifications primarily relating to the use of XML in Web browsers or other DOM-
based systems;
9. XML Fragments
Listed here only for completeness; this specification is not in widespread use.

4.4 DTD and Schemas

The purpose of a DTD (Document Type Definition) is to define the structure of an XML
document. It defines the structure with a list of legal elements:

<!DOCTYPE note
[
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>

The DTD above is interpreted like this:

 !DOCTYPE note defines that the root element of the document is note
 !ELEMENT note defines that the note element must contain the elements: "to, from,
heading, body"
 !ELEMENT to defines the to element to be of type "#PCDATA"

Page no: 2 Get real-time updates from RGPV


Downloaded from www.rgpvnotes.in

 !ELEMENT from defines the from element to be of type "#PCDATA"


 !ELEMENT heading defines the heading element to be of type "#PCDATA"
 !ELEMENT body defines the body element to be of type "#PCDATA"

4.5 Using XML with Application

The Extensible Markup Language (XML) is a subset of SGML designed to enable generic
SGML to be served, received, and processed on the Web easily and efficiently. Because its
format is not fixed, XML enables you to design your own customized markup languages with
which to create various types of documents. In the IDE, XML documents are represented by
XML nodes. The IDE provides tools to assist you in creating, editing, checking, and validating
the various XML document types it supports.

The IDE supports several types of XML documents, including:

 Cascading Style Sheet. A CSS provides a simple mechanism for formatting and
adding style to HTML and XML documents.
 Document Type Definition. A DTD describes the grammar that can be used in an
XML file and indicates the valid arrangement of the file's tags. It can exist as a
prologue to an XML file (internal DTD) or as a separate file (external DTD).
 Parsed Entity Objects. Parsed entity nodes represent parsed entity (.ent) files. If
several of your XML documents refer to a single piece of information, you can store
that information in a parsed entity. Any changes you make to the parsed entity are
then automatically reflected in all of the documents that reference it.
 XML Catalogs. XML catalogs provide mapping information that maps an external
entity in an XML document to the actual location of the document being referenced.
You can use a catalog to redirect mappings for an external entity, such as a DTD or
XML file, to a different location.
 XSL Style sheet. XSL style sheets define transformation rules for your XML
documents. You can perform an XML transformation to transform the data in an
XML document into the output of your choice, such as formatted HTML or a text file

4.6 Transforming XML using XSL and XSLT PHP

XML TRANSFORMED. XSLT (Extensible Style sheet Language Transformations) is a language


for transforming XML documents into other XML documents, or other formats such as HTML
for web pages, plain text or into XSL Formatting Objects, which may subsequently be
converted to other formats, such as PDF, PostScript and PNG.

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
Or
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
4.6.1 XSLT PHP
Using the following code one can use XML inside PHP Scriplet

Page no: 3 Get real-time updates from RGPV


Downloaded from www.rgpvnotes.in

<?php

// Load the XML source


$xml = new DOMDocument;
$xml->load('collection.xml');

$xsl = new DOMDocument;


$xsl->load('collection.xsl');

// Configure the transformer


$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules

echo trim($proc->transformToDoc($xml)->firstChild->wholeText);

?>

4.7 Introduction and Basic Syntax of PHP

Basic Syntax: PHP or Hypertext Pre-processor is a widely used open-source general purpose
scripting language and can be embedded with HTML. PHP files are saved with “.php”
extension.
<?php
// PHP code goes here
?>
Example
<!DOCTYPE html>
<html>
<body>

<h1>My first PHP page</h1>

<?php
echo "Hello World!";
?>

</body>
</html>

4.8 Decision and Looping with Examples

Loops in PHP are used to execute the same block of code a specified number of times. ...
while − loops through a block of code if and as long as a specified condition is true.
do...while − loops through a block of code once, and then repeats the loopas long as a
special condition is true.

Page no: 4 Get real-time updates from RGPV


Downloaded from www.rgpvnotes.in

Loops in PHP are used to execute the same block of code a specified number of times. PHP
supports following four loop types.

 for − loops through a block of code a specified number of times.

 while − loops through a block of code if and as long as a specified condition is true.

 do...while − loops through a block of code once, and then repeats the loop as long as
a special condition is true.

 foreach − loops through a block of code for each element in an array.

We will discuss about continue and break keywords used to control the loops execution

Example:

<html>

<body>

<?php

$a = 0;

$b = 0;

For ( $i = 0; $i<5; $i++ ) {

$a += 10;

$b += 5; }

echo ("At the end of the loop a = $a and b = $b" );

?>

</body>

</html>

4.9 PHP and HTML


In an HTML page, PHP code is enclosed within special PHP tags. When a visitor opens the
page, the server processes the PHP code and then sends the output (not the PHP code itself)
to the visitor's browser. Actually it is quite simple to integrate HTML and PHP. A PHP script
can be treated as an HTML page, with bits of PHP inserted here and there. Anything in a PHP
script that is not contained within <?php ?> tags is ignored by the PHP compiler and passed
directly to the web browser.

Page no: 5 Get real-time updates from RGPV


Downloaded from www.rgpvnotes.in

<html>
<head></head>
<body>
<ul>
<?php for($i=1;$i<=5;$i++){ ?>

<li>MenuItem <?php echo $i; ?> </li>

<?php } ?>
</ul>
</body>
</html>

4.10 Arrays, Functions, Browser Control and Detection

4.10.1 Arrays

An array is a special variable, which can hold more than one value at a time. If you have a list
of items (a list of car names, for example), storing the cars in single variables could look like
this:

$cars1 = "Volvo";
$cars2 = "BMW";
$cars3 = "Toyota";

However, what if you want to loop through the cars and find a specific one? And what if you
had not 3 cars, but 300?

<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars [0]. ", " . $cars[1] . ”and " . $cars[2] . ".";
?>

4.10.2 Functions

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. You already have seen many functions like fopen() and fread() .

<?php
function writeMsg() {
echo "Hello world!";
}

writeMsg(); // call the function

Page no: 6 Get real-time updates from RGPV


Downloaded from www.rgpvnotes.in

?>

4.10.3 Browser Control and Detection

PHP get_browser() Function: The get_browser() function in PHP is an inbuilt function which
is used to tell the user about the browser's capabilities. The get_browser() function looks up
the user's browscap.ini file and returns the capabilities of the user's browser.

Syntax: get_browser(user_agent, return_array)

Parameters Used:
The get_browser() function in PHP accepts two parameters.
1. user_agent : It is an optional parameter which specifies the name of an HTTP user
agent. Default is the value of $HTTP_USER_AGENT.
2. return_array : It is an optional parameter which returns an array instead of an object
if it is set to True.

Exceptions:
1. The user_agent parameter can be bypassed with a NULL value.
2. The cookies value simply means that the browser itself is capable of accepting
cookies and does not mean the user has enabled the browser to accept cookies or
not.
3. In order for this function to work the browscap configuration setting in php.ini must
point to the correct location of the browscap.ini file on your system.

<?php
echo $_SERVER['HTTP_USER_AGENT'];
//using get_browser() to display capabilities of the user browser
$mybrowser = get_browser();
print_r($mybrowser);
?>

4.11 String and Form Processing

4.11.1String

The term PHP is an acronym for PHP: Hypertext Preprocessor. PHP is a server-side scripting
language designed specifically for web development. PHP can be easily embedded in HTML
files and HTML codes can also be written in a PHP file. The thing that differentiates PHP with
client-side language like HTML is, PHP codes are executed on the server whereas HTML
codes are directly rendered on the browser.

<?php
echo str_word_count("Hello world!"); // outputs 2
?>

Page no: 7 Get real-time updates from RGPV


Downloaded from www.rgpvnotes.in

4.11.2 Form Processing

When the user fills out the form above and clicks the submit button, the form data is sent
for processing to a PHP file named "welcome.php". The form data is sent with the HTTP
POST method.

<html>
<body>

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


Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>

4.12 Files

File handling is an important part of any web application. You often need to open and
process a file for different tasks. PHP has several functions for creating, reading, uploading,
and editing files.

<?php
echo readfile("webdictionary.txt");
?>

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>

<?php
$myfile = fopen("webdictionary.txt", "r");
// some code to be executed....
fclose($myfile);
?>

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file

Page no: 8 Get real-time updates from RGPV


Downloaded from www.rgpvnotes.in

while(!feof($myfile)) {
echo fgetc($myfile);
}
fclose($myfile);
?>

4.13 Advance Features

Following are some of the advanced features those can be used:

 $target_dir = "uploads/" - specifies the directory where the file is going to be placed
 $target_file specifies the path of the file to be uploaded
 $uploadOk=1 is not used yet (will be used later)
 $imageFileType holds the file extension of the file (in lower case)
 Next, check if the image file is an actual image or a fake image

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
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;
}
}
?>

4.14 Cookies and Sessions

4.14.1 Cookies
 Http is a stateless protocol; cookies allow us to track the state of the application
using small files stored on the user’s computer.
The path were the cookies are stored depends on the browser.
Internet Explorer usually stores them in Temporal Internet Files folder.
 Personalizing the user experience – this is achieved by allowing users to select their
preferences.
The page requested that follow are personalized based on the set preferences in the
cookies.
 Tracking the pages visited by a user

Page no: 9 Get real-time updates from RGPV


Downloaded from www.rgpvnotes.in

<?php

setcookie(cookie_name, cookie_value, [expiry_time], [cookie_path], [domain],


[secure], [httponly]);

?>

4.14.2 Session
 A session is a global variable stored on the server.
 Each session is assigned a unique id which is used to retrieve stored values.
 Whenever a session is created, a cookie containing the unique session id is stored on
the user’s computer and returned with every request to the server. If the client
browser does not support cookies, the unique php session id is displayed in the URL
 Sessions have the capacity to store relatively large data compared to cookies.
 The session values are automatically deleted when the browser is closed. If you want
to store the values permanently, then you should store them in the database.
 Just like the $_COOKIE array variable, session variables are stored in the $_SESSION
array variable. Just like cookies, the session must be started before any HTML tags.
 You want to store important information such as the user id more securely on the
server where malicious users cannot temper with them.
 You want to pass values from one page to another.
 You want the alternative to cookies on browsers that do not support cookies.
 You want to store global variables in an efficient and more secure way compared to
passing them in the URL
 You are developing an application such as a shopping cart that has to temporary
store information with a capacity larger than 4KB.

Why and when to use Sessions?


 You want to store important information such as the user id more securely on the
server where malicious users cannot temper with them.
 You want to pass values from one page to another.
 You want the alternative to cookies on browsers that do not support cookies.

<?php

session_start(); //start the PHP_session function

if(isset($_SESSION['page_count']))
{
$_SESSION['page_count'] += 1;
}
else
{

Page no: 10 Get real-time updates from RGPV


Downloaded from www.rgpvnotes.in

$_SESSION['page_count'] = 1;
}
echo 'You are visitor number ' . $_SESSION['page_count'];

?>

4.15 Object Oriented Programming with PHP

Object-oriented programming is a style of coding that allows developers to group similar


tasks into classes. This helps keep code following the tenet "don't repeat yourself" (DRY)
and easy-to-maintain. "Object-oriented programming is a style of coding that allows
developers to group similar tasks into classes.

<?php
class vehicle{
/*** define public properties ***/

/*** the color of the vehicle ***/


public $color;

/*** the number of doors ***/


public $num_doors;

/*** the price of the vehicle ***/


public $price;

/*** the shape of the vehicle ***/


public $shape;

/*** the brand of vehicle ***/


public $brand;

/*** the constructor ***/


public function __construct(){
echo 'About this Vehicle.<br />';
}

/*** define some public methods ***/

/*** a method to show the vehicle price ***/


public function showPrice(){
echo 'This vehicle costs '.$this->price.'.<br />';
}

/*** a method to show the number of doors ***/

Page no: 11 Get real-time updates from RGPV


Downloaded from www.rgpvnotes.in

public function numDoors(){


echo 'This vehicle has '.$this->num_doors.' doors.<br />';
}

/*** method to drive the vehicle ***/


public function drive(){
echo 'VRRROOOOOOM!!!';
}

} /*** end of class ***/


?>

Page no: 12 Get real-time updates from RGPV


We hope you find these notes useful.
You can get previous year question papers at
https://qp.rgpvnotes.in .

If you have any queries or you want to submit your


study notes please write us at
[email protected]

You might also like