Hardware Reference
In-Depth Information
The PHP script uses the
$_REQUEST array variable
to get the results of the HTTP request.
Even though the Processing sketch
is making a GET request, this script
doesn't careā€”it will read GET or POST.
Log It
<?php
/*
Data logger
Context: PHP
*/
// name of the file on the server
// where you want to save stuff:
$dataFile = 'datalog.txt';
First, it checks to see whether the $_
REQUEST array has a value set. If so, it
looks for a variable within it called data .
// see if the client uploaded a file:
if (isset($_REQUEST)) {
$newData = $_REQUEST['data'];
if (file_exists($dataFile)) {
// Open the existing file to get existing content
$currentData = file_get_contents($dataFile);
// Add what you got from the client:
$currentData .= $newData;
// Write everything back to the existing file
file_put_contents($dataFile, $currentData);
}
}
?>
Next, it checks to see whether the
datalog file exists. If so, it opens it and
puts its contents in a variable called
$currentData . Then it appends the new
data from the client to the end of that
variable, and overwrites the file with
the result.
The HTML following the script echoes
back to the client what it sent. Putting
it in HTML format is handy for checking
it in a browser.
<html>
<body>
Here's what you sent:
<?php echo $newData; ?>
</body>
</html>
Figure 10-23
What the logger script returns in a browser.
 
Search WWH ::




Custom Search