Assigned Th 7/3/2008, due Fr 7/11 at 9pm. 50 points.
http://www.cs.nyu.edu/courses/summer08/G22.3033-002/
Homeworks are due on Fridays at 9pm. This deadline will be strictly enforced.
Email your answers to Robert Soulé robert.soule@gmail.com. Please put your solutions to VBA programming problems in a powerpoint presentation. For all other questions (including programming problems in other languages), just send a simple text file, such as what you get when using Emacs, Vi, Notepad, or the "save as text only" feature in Word.
Please make sure that your code works with the compilers and tools installed at CIWW. Specifically, please test:
doowop1
(see http://www.cims.nyu.edu/systems/resources/computeservers/).
(6+6 = 12 points) Try the Google Suggest web application, which is available at http://www.google.com/webhp?complete=1&hl=eN. Now think about what you would need to do to write a similar web application yourself.
(6+6+6 = 18 points) Consider the following PHP script.
<html><head>
<title>Translation Database</title>
</head><body>
<!-- show form for user input -->
<form action="<?php echo $_SERVER[PHP_SELF]; ?>" method=get>
<table border=0>
<tr><td>English: <td><input type="text" name="eng"><br/>
<tr><td>Translated: <td><input type="text" name="tra"><br/>
</table>
<input type="radio" name="request" value="add">
Add</br>
<input type="radio" name="request" value="e2f">
Get (English to Translated)</br>
<input type="radio" name="request" value="f2e">
Get (Translated to English)</br>
<input type="radio" name="request" value="del">
Delete</br>
<input type="submit" value="submit">
</form>
<!-- open database, and create table if it does not yet exist -->
<?php
$db = sqlite_open("/home/hirzel/data/sqlite2", 0666, $err);
if ($err) { die($err); }
sqlite_query($db, "SELECT * FROM Translate", SQLITE_BOTH, $err);
if ($err) {
echo "Table 'Translate' does not yet exist, creating it ...<br/>";
$q = "CREATE TABLE Translate"
. "(English VARCHAR(50), Translated VARCHAR(50))";
sqlite_query($db, $q, SQLITE_BOTH, $err);
if ($err) { die($err); }
} else {
echo "Reopened table 'Translate'.<br/>";
}
?>
<!-- process input, if any -->
<?php
if ("add" == $_GET[request]) {
$q = "INSERT INTO Translate VALUES("
. "'" . addslashes($_GET[eng]) . "', "
. "'" . addslashes($_GET[tra]) . "')";
sqlite_query($db, $q);
if ($err) { die($err); }
echo "Add English '" . htmlentities($_GET[eng]) . "', " .
"Translated '" . htmlentities($_GET[tra]) . "' succeeded.";
} else if ("e2f" == $_GET['request']) {
$q = "SELECT Translated FROM Translate WHERE English = "
. "'" . addslashes($_GET[eng]) . "'";
$rows = sqlite_query($db, $q);
if ($err) { die($err); }
$row = sqlite_fetch_array($rows, SQLITE_BOTH);
if ($err) { die($err); }
echo "Get English '" . htmlentities($_GET[eng]) . "' returned "
. "Translated '" . htmlentities($row[Translated]) . "'.";
} else if ("f2e" == $_GET['request']) {
# put your code here
echo "Sorry, lookup from Translated to English not yet implemented.";
} else if ("del" == $_GET['request']) {
# put your code here
echo "Sorry, deletion not yet implemented.";
}
?>
</body>
data directory, for example,
like this:
cd ~
mkdir data
chmod 777 data
The file permissions must enable the web server to read and write your data directory. You will also need to change the line
$db = sqlite_open("/home/hirzel/data/sqlite2", 0666, $err);
to use your own directory. When you submit this homework, please indicate the URL at which the script is running.
(8 points) Write a PHP script that uses a session variable to count how often the user has visited it. For example, after the second visit, the page should look like this:
(6 + 6 = 12 points) Consider the AJAX application from the lecture slides from 7/3/2008, repeated here for your convenience.
a.html
<html><head>
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<script src="c.js"></script>
</head><body bgColor="#00ff00">
Time: <span id="time">(please click button)</span>
<form name="in">
<input type="button" value="Toggle Color" onclick="localChange();">
<input type="button" value="Request Time" onclick="sendRequest();">
</form>
</body></html>
b.php
<?php sleep(3); echo date('h:i:s') ?>
c.js
function localChange() {
var blue = document.bgColor == '#0000ff'
document.bgColor = blue ? '#00ff00' : '#0000ff';
}
function sendRequest() {
document.getElementById('time').innerHTML = '(please wait)';
var xhr = false;
function handleResponse() {
if (4 == xhr.readyState && 200 == xhr.status)
document.getElementById('time').innerHTML = xhr.responseText;
}
try { xhr = new XMLHttpRequest(); } catch(e1) {
try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e2) {
try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e3) {
alert('Could not create XMLHttpRequest object.'); } } }
xhr.open('GET', 'b.php?nonce=' + Math.random(), true);
xhr.onreadystatechange = handleResponse;
xhr.send(null);
}
Questions