Unit 4 - Internet and Web Technology - WWW - Rgpvnotes.in
Unit 4 - Internet and Web Technology - WWW - Rgpvnotes.in
Tech
Subject Name: Internet and Web Technology
Subject Code: CS-504
Semester: 5th
Downloaded from www.rgpvnotes.in
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.
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).
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,
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.
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)>
]>
!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"
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.
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
<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
<?php
echo trim($proc->transformToDoc($xml)->firstChild->wholeText);
?>
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>
<?php
echo "Hello World!";
?>
</body>
</html>
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.
Loops in PHP are used to execute the same block of code a specified number of times. PHP
supports following four loop types.
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.
We will discuss about continue and break keywords used to control the loops execution
Example:
<html>
<body>
<?php
$a = 0;
$b = 0;
$a += 10;
$b += 5; }
?>
</body>
</html>
<html>
<head></head>
<body>
<ul>
<?php for($i=1;$i<=5;$i++){ ?>
<?php } ?>
</ul>
</body>
</html>
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!";
}
?>
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.
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.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
?>
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>
</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
while(!feof($myfile)) {
echo fgetc($myfile);
}
fclose($myfile);
?>
$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.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
<?php
?>
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.
<?php
if(isset($_SESSION['page_count']))
{
$_SESSION['page_count'] += 1;
}
else
{
$_SESSION['page_count'] = 1;
}
echo 'You are visitor number ' . $_SESSION['page_count'];
?>
<?php
class vehicle{
/*** define public properties ***/