Some HTTP Examples
A utility which shows all data from incoming requests is here: It is defined as follows: <?php echo " SERVER variables: "; foreach ($_SERVER as $name => $value) { echo "$name = $value "; } echo " GET variables: "; foreach ($_GET as $name => $value) { echo "$name = $value "; } echo " POST variables: "; foreach ($_POST as $name => $value) { echo "$name = $value "; } echo " COOKIE variables: "; foreach ($_COOKIE as $name => $value) { echo "$name = $value "; } ?>
A program which delivers a web-page in the user’s preferred language It is defined as follows: <?php function answerInEnglish() {?> Hello <?php } function answerInFrench() {?> Bonjour <?php } function giveDefaultAnswer() {?> Default greeting <?php } $language=$_SERVER['HTTP_ACCEPT_LANGUAGE']; if ($language=='en-ie') { answerInEnglish(); } else if ($language=='fr') { answerInFrench(); } else { giveDefaultAnswer(); } ?>
Consider this page: It is defined as follows: <?php function answerInEnglish() {?> Hello <?php } function answerInFrench() {?> Bonjour <?php } function giveDefaultAnswer() {?> Default greeting <?php } $language=$_SERVER['HTTP_ACCEPT_LANGUAGE']; if ($language=='en-ie') { answerInEnglish(); } else if ($language=='fr') { answerInFrench(); } else { giveDefaultAnswer(); } ?>
Consider this web-page It contains the following: Wordsworth I wandered lonely as a cloud That floats on high o'er vales and hills
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: It contains the following: Wordsworth I wandered lonely as a cloud That floats on high o'er vales and hills
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 It contains the following: <?php echo “ ”; echo “ Wordsworth ”; echo “ ”; echo “ I wandered lonely as a cloud ”; echo “ That floats on high o'er vales and hills ”; echo “ ”; ?>
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 It contains the following: <?php header(“Content-Type: text/xml”); echo “ ”; echo “ Wordsworth ”; echo “ ”; echo “ I wandered lonely as a cloud ”; echo “ That floats on high o'er vales and hills ”; echo “ ”; ?>
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