Slide 1

Some HTTP Examples
• A utility which shows all data from incoming requests is here:
http://www.cs.ucc.ie/j.bowen/cs3314/resources/showRequest.php
• It is defined as follows:
<?php
echo "<strong>SERVER variables:</strong><br>";
foreach ($_SERVER as $name => $value)
{ echo "$name = $value <br>"; }
echo "<strong>GET variables:</strong><br>";
foreach ($_GET as $name => $value)
{ echo "$name = $value <br>"; }
echo "<strong>POST variables:</strong><br>";
foreach ($_POST as $name => $value)
{ echo "$name = $value <br>"; }
echo "<strong>COOKIE variables:</strong><br>";
foreach ($_COOKIE as $name => $value)
{ echo "$name = $value <br>"; }
?>
• A program which delivers a web-page in the user’s
preferred language
http://www.cs.ucc.ie/j.bowen/cs3314/resources/multiLingual.php
• It is defined as follows:
<?php
function answerInEnglish()
{?> <h1>Hello</h1> <?php }
function answerInFrench()
{?> <h1>Bonjour</h1> <?php }
function giveDefaultAnswer()
{?> <h1>Default greeting</h1> <?php }
$language=$_SERVER['HTTP_ACCEPT_LANGUAGE'];
if ($language=='en-ie') { answerInEnglish(); }
else if ($language=='fr') { answerInFrench(); }
else { giveDefaultAnswer(); }
?>
• Consider this page:
http://www.cs.ucc.ie/j.bowen/cs3314/resources/poem.html
• It is defined as follows:
<?php
function answerInEnglish()
{?> <h1>Hello</h1> <?php }
function answerInFrench()
{?> <h1>Bonjour</h1> <?php }
function giveDefaultAnswer()
{?> <h1>Default greeting</h1> <?php }
$language=$_SERVER['HTTP_ACCEPT_LANGUAGE'];
if ($language=='en-ie') { answerInEnglish(); }
else if ($language=='fr') { answerInFrench(); }
else { giveDefaultAnswer(); }
?>
• Consider this web-page
http://www.cs.ucc.ie/j.bowen/cs3314/resources/poem.html
• It contains the following:
<poem>
<poet>Wordsworth</poet>
<verse>
<line>I wandered lonely as a cloud</line>
<line>That floats on high o'er vales and hills</line>
</verse>
</poem>
• When we try to view this page, we see this, because the
tags in the file are not recognized
• Now consider this file, which has a different name but
contains exactly the same text:
http://www.cs.ucc.ie/j.bowen/cs3314/resources/poem.xml
• It contains the following:
<poem>
<poet>Wordsworth</poet>
<verse>
<line>I wandered lonely as a cloud</line>
<line>That floats on high o'er vales and hills</line>
</verse>
</poem>
• When we try to view this page, we see this, because the
tags in the file are treated as XML tags
• See what happens when we use telnet to access the
poem.html file
• The response message contains this header line:
Content-Type: text/html
• See what happens when we use telnet to access the
poem.xml file
• The response message contains this header line:
Content-Type: text/xml
• The different behaviour shown by a browser when we
view the two files poem.xml and poem.html is caused by
these two different headers
• The header
Content-Type: text/html
tells the browser to treat the data in the body of the response
message as a HTML file
• The header
Content-Type: text/xml
tells the browser to treat the data in the body of the response
message as a XML file
• Consider this PHP program which outputs the XML for
the poem
http://www.cs.ucc.ie/j.bowen/cs3314/resources/poem1.php
• It contains the following:
<?php
echo “<poem>”;
echo “<poet>Wordsworth</poet>”;
echo “<verse>”;
echo “<line>I wandered lonely as a cloud</line>”;
echo “<line>That floats on high o'er vales and hills</line>”;
echo “</verse>”;
echo “</poem>”;
?>
• When we try to view this page, we see this, because the
tags in the file are not recognized
• Let’s use telnet to try to view the same page
• Notice that the response message contains the header
Content-Type: text/html
• This is why the browser did not recognize the tags
• Now consider this PHP program which outputs a header
before it outputs the XML text for the poem
http://www.cs.ucc.ie/j.bowen/cs3314/resources/poem2.php
• It contains the following:
<?php
header(“Content-Type: text/xml”);
echo “<poem>”;
echo “<poet>Wordsworth</poet>”;
echo “<verse>”;
echo “<line>I wandered lonely as a cloud</line>”;
echo “<line>That floats on high o'er vales and hills</line>”;
echo “</verse>”;
echo “</poem>”;
?>
• Let’s use telnet to ask for the page created by this new
PHP program
• Notice that the response message contains the header
Content-Type: text/xml
• This means that a browser should recognize that the tags
are XML tags
• When this page in a browser, the browser does indeed
recognize that the tags are XML tags