CSE134A DISCUSSION SECTION

November 16, 2001
 
 

ANNOUNCEMENTS

I have midterms for pick-up for those of you who have not done so yet. Also, at the end of the section, I'm available to discuss issues relating to Question 2 on your midterm.
 

A HIGH-LEVEL OVERVIEW OF PROJECT 4Architecture of Project 4 Software

XML, DTD, and XSL

XML is an extensible markup language for describing data. It does not contain formatting or any information about how the data should be presented. Here is a simple example:
<PEOPLE>
<PERSON>
<NAME>Mark Wilson</NAME>
<TEL>(619) 543 12345</TEL>
<AGE>23</AGE>
</PERSON>
<PERSON>
<NAME>Tracey Wilson</NAME>
<TEL>(858) 531 9090</TEL>
<AGE>38</AGE>
</PERSON>
</PEOPLE>
DTD is a data type definition. It defines an application specific special case of XML.
XSL is an extensible stylesheet language. It is a language for defining stylesheets.  A stylesheet specifies the presentation of XML information using two basic categories of techniques: How can we use XSL to display XML in a web browser?
 

How do XML and  XSL work together
 

Coming back to the simple example, say we would like to present out date in the following form table like form in HTML:

Name                   Tel
Mark Wilson        (619) 543 12345
Tracey Wilson      (858) 531 9090
We can create a stylesheet that specifies our desired layout:
  <?xml version="1.0" ?>
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
    <xsl:template match="/">
       <HTML>
          <BODY>
             <TABLE BORDER="2">
                <TR>
                   <TD>Name</TD>
                   <TD>Tel</TD>
                 </TR>
               <xsl:for-each select="PEOPLE/PERSON">
                  <TR>
                     <TD>
                         <xsl:value-of select="NAME" />
                      </TD>
                    <TD>
                         <xsl:value-of select="TEL" />
                      </TD>
                   </TR>
                </xsl:for-each>
             </TABLE>
          </BODY>
       </HTML>
    </xsl:template>
 </xsl:stylesheet>


For more information about XSL please visit http://www.nwalsh.com/docs/tutorials/xsl/ from where I have taken some of this material.
 
 

HOW TO STORE IMAGES AS BLOBS IN MySQL

A BLOB is a binary large object that can hold a variable amount of data. The four BLOB types TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB differ only in the maximum length of the values they can hold.

The following example, taken from http://www.phpbuilder.com/columns/florian19991014.php3, shows an example of  "How to store images directly in the SQL database ":

Create a database with a bin_data column that can hold BLOB:
 

CREATE TABLE binary_data (
id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY,
description CHAR(50),
bin_data LONGBLOB,
filename CHAR(50),
filesize CHAR(50),
filetype CHAR(50)
);


Read and store the binary data into the database:

Read binary data date from $bin_data which is either a file name or a URL

$data = addslashes(fread(fopen($bin_data, "r"), filesize($bin_data)))

$result=MYSQL_QUERY("INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) ".
                    "VALUES ('$bin_description','$data','$bin_data_name','$bin_data_size','$bin_data_type')");


Retrieve a BLOB from the database:

$query = "select bin_data,filetype from binary_data where id=$id";
$result = @MYSQL_QUERY($query);

$data = @MYSQL_RESULT($result,0,"bin_data");
 
 

HOW TO SEND EMAIL FROM A PHP SCRIPT

bool mail (string to, string subject, string message [, string additional_headers, string [additional_parameters]])

mail() automatically mails the message specified in message to the receiver specified in to. Multiple recipients can be specified by putting a comma between each address in to. Email with attachments and special types of content can be sent using this function. mail() returns TRUE if the mail is successfully sent, FALSE otherwise.

Example: mail("someone@aol.com", "Sample subject", "Sample message");

You can find more complex examples about mail on www.php.net, including sending HTML messages and attachments, but for this project you should use plain ASCII.
 
 

HOW TO PROVIDE SECURITY THROUGH HARD-TO-GUESS URLS

Login names and password are burdensome for the everyday user. An alternative mechanism to achieve security on the web is to construct hard-to-guess URLS like http://www.quicktopic.com/7/H/XdHpb7a7SYe. This mechanism is simple to use and implement and provides sufficient security for everyday use.

In Project 4 after the sender's message is validated the client needs to send an email notification to the receiver with the message included in ASCII.  This email message should also include a URL the receiver should visit to get a nicer layout of the message. This can be done in the following way:

  1. Associate a UNIQUE random key with every document that is stored in the database.
  2. Enable the client software to display a certain message specified by the unique key in a given layout format
Your client software can get the values for the unique key and the layout method using the GET method. The dynamic URL corresponding to a specific message can be sent to the receiver in an email.
 
 

HOW TO INVOKE A POST-BASED EXTERNAL WEB PAGE INSIDE PHP

Part of this project ask you to contact the UPS ZIP+4 Code Look-up site at http://www.usps.gov/ncsc/lookups/lookup_zip+4.html. This site uses the POST method for processing the values entered in the form. To invoke this web page we can simply open a socket to port 80 (HTTP) on the host (www.usps.gov), and send HTTP header and the values for each of the variables in the form. The results can be simply read back trough the same socket. The following is an example shows the structure of a standard HTTP header. Note the 2 new line characters after the last line of the HTTP header, they are essential!
<?php

function PostToHost($host, $path, $data_to_send) {
  $fp = fsockopen($host,80);
  printf("Open!\n");
  fputs($fp, "POST $path HTTP/1.1\n");
  fputs($fp, "Host: $host\n");
  fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
  fputs($fp, "Content-length: ".strlen($data_to_send)."\n");
  fputs($fp, "Connection: close\n\n");
  fputs($fp, "$data_to_send\n");
  printf("Sent!\n");
  while(!feof($fp)) {
      $res .= fgets($fp, 128);
  }
  printf("Done!\n");
  fclose($fp);

  return $res;
}

$data = "Firm=&Urbanization=&Delivery+Address=10968+Canyon+Hill+Lane&City=San+Diego&State=CA&Zip+Code=&Submit=Process";

printf("Go!\n");
$x = PostToHost(
              "www.usps.gov",
              "/cgi-bin/zip4/zip4inq2",
              $data
);
print "\n\n$x\n\n";
?>

The following is the output of this php script:
Go! Open! Sent! Done! HTTP/1.1 200 OK Server: Netscape-Enterprise/4.0 Date: Fri, 16 Nov 2001 19:43:45 GMT Content-type: text/html Connection: close   ZIP+4 Code Lookup

Look up another ZIP+4 Code | Questions and Comments

--------------------------------------------------------------------------------
The standardized address is:
10968 CANYON HILL LN
SAN DIEGO CA 92126-2054 Carrier Route : C043  County : SAN DIEGO
Delivery Point : 68  Check Digit : 5
 

--------------------------------------------------------------------------------

For other business lookup services: YellowPages.com

Version 5.0 Database 11/2001
Copyright © 1997 United States Postal Service. All rights reserved.
Developed by the USPS National Customer Support Center


 



Copyright (c) by Gyozo Gidofalvi, 2001.