In the last column we introduced the use of embedded databases within PHP via the Database Abstract (DBA) Layer. Now, we will re-implement Address Book 1 (AB1) using this superior technology.
Accepting New Data
In AB1, ab1.php built a string, $data, consisting of the uploaded data. Each item of data was on its own line with a leading identifier tag, incorporated so that the parser knew how to read the file. For example:
FGavin
LSherry
tells the parser that the first line is a first name and the second line is a second name. While we could simply use the same $data string, it does not make much sense to separate each attribute of data by a new line. Instead, we will take a lesson from the early Common Gateway Interface (CGI) engineers and use the name=value pairing. This also means that we can escape our delimiter using standard PHP URL functions. For example:
F=Gavin&L=Sherry
The script begins with the declaration of a variable $DB, where the name of the database is stored. This is followed by a function, mkdb(), which compensates for a shortcoming in PHP's DBA layer implementation. It checks if the database already exists, and if it does not, it creates it and returns the identifier, $id. If the database already exists, mkdb() returns false.
Following this, the code tests if it has been called to add data to the address database (see the December 2001 issue for a more in-depth explanation of this). The script then executes the mkdb() function with the argument $DB, our database. It tests the return value and, if it is false, opens $DB for writing without creating it.
The data string $data is then built in the form of name=value pairs. Since this mimics the CGI query protocol used by PHP and most other Web application development systems, the script can make use of a standard URL encoding function, rawurlencode(), to make sure the data is not corrupted. For example, if one of the uploaded values contained an ampersand (&), this would affect our $data string in the following way:
F=Gavin & Co&L=Sherry
The parser, if it follows the name=value rules, will think that the value of F is 'Gavin'. Moreover, the next name, 'Co' will have no value. By calling rawurlencode(), & will be converted to the value '%26'. Our parser will decode these values later.
Finally, the data is inserted into the database, using $ln, the last name, as a key. If dba_insert() returns true, "Data successfully updated" is sent to the user; otherwise, an error "Could not store data" is raised. Note that the database is closed in both instances to preserve data integrity.
Search Address Book 2
Searching in AB2 is much simpler than AB1, because the parser does not need to parse the whole file, just the result. See the implementation below:
/* search for the last name stored in $query */
if(isset($submit) && (strcmp($submit,"Search") == 0)) {
if(!file_exists($DB)) { /*
database has not been created yet */
exit("No entries to search");
}
if(!($id = dba_open($DB,"r","db3"))) {
exit("Could not open $db\n");
}
if(($str = dba_fetch($query,$id))) {
/* found the query */
parse_str($str);
?>
NAME: <? echo rawurldecode($F); ?> <? echo rawurldecode($L);
?>
<?
/* etc */
} else {
echo "No entry with that last name";
} dba_close($id);
}
Like the data upload script, this second section of AB2 tests to see if it has been called by comparing $submit to "Search". To start with, the script checks to see if $DB exists - since if it does not, it hasn't been created with mkdb() in the first section of AB2.
After this, $DB is opened and dba_fetch() is called in order to retrieve the entry pointed to by $query, where $query is a last name. If dba_fetch() returns true, the key has been located. The resulting entry is put in $str which is parsed by parse_str(). This function extracts the names out of the name=value pairs and inlines them as variables with the corresponding value. That is, given the string:
F=Gavin&L=Sherry
The corresponding data can be accessed via $F and $L.
The address book data is the output to the user. Note that the data is processed with rawurldecode(), to unescape any work done by rawurlencode() earlier in the script.
Discover how SOA can create smarter outcomes for your business.
Attend and learn:
- How SOA is helping leading companies to become more agile
- Where you should be applying SOA processes in your company
- The top SOA implementation mistakes to avoid
Click here for more information.
- +
Computerworld Live Podcast #97: The Future of Enterprise Networking 25/07/2008 09:45:36
This week CW Live chats with Mark Thompson, global sales and marketing manager for HP ProCurve, on the future of the enterprise networking. Mark discusses the trends we can expect to see in the near future and how the right infrastructure can ensure your enterprise network is secure. - +
Computerworld Live Podcast #96: Security at the Edge 11/06/2008 09:22:22
CW Live speaks with Amol Mitra, HP ProCurve Director of Marketing for Asia Pacific and Japan. Today's topic: how enterprises are starting to shift away from simply controlling security via server logins, firewalls and moving to more adaptive security frameworks. - +
Data Management Edition #10: Multi-Petascale Systems 02/05/2008 09:12:33
This week we look at sustainability and the development of multicore technologies to build multi-petascale systems. - +
IT Security Edition #11: How to poison the Storm botnet 01/05/2008 08:51:55
This week CW Live presents a case study on how to poison the notorious Storm botnet . Plus we take a look at Cisco's plans for Ironport. - +
IT Security Edition #10: Cyber-battles fought and won 24/04/2008 11:09:47
Vendors bow to end user pressure to improve product security, and we take a look at the latest concepts shaping the cyber-battlefield of the future.
Vignette Announces 2008 Excellence Awards 2008-11-21 10:50:00+11
PGP and Ponemon Institute Unveil Inaugural Australian Data Breach Study 2008 2008-11-20 17:34:00+11
Symantec Cloud Services Transform Data Centre Operations Through Proactive Management 2008-11-20 12:06:00+11
Verizon Business Offers Tips to Building a Successful Unified Communications and Collaboration Plan 2008-11-20 12:04:00+11
AARNet Brings 4K Digital Cinema to Australia: First 4K HD Video Signal delivered into Australia by AARNet 2008-11-20 12:02:00+11
Dude! You Say I Need an Application-Layer Firewall?!
Proxy firewall technologies have proven time and again to be more secure than “stateful” firewalls. They will also prove to be more secure than “deep inspection” firewalls. High-performance proxy firewalls are available today which are easily capable of handling gigabit-level traffic. Discover more by reading on.









