13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Solution Overview<br />

825<br />

if(!eregi('^[a-z]+$', $mode)) {<br />

// mode must be alphabetic<br />

$mode = 'Books';<br />

}<br />

$page=intval($page); // pages <strong>and</strong> browseNodes must be integers<br />

$browseNode = intval($browseNode);<br />

// it may cause some confusion, but we are stripping characters out from<br />

// $search it seems only fair to modify it now so it will be displayed<br />

// in the heading<br />

This is nothing new.The safeString() function is in the utilityfunctions.php file.<br />

It simply removes any nonalphanumeric characters from the input string via a regular<br />

expression replacement. Because we have covered this topic before, we did not include it<br />

here in the text.<br />

The main reason that you need to validate input in this application is that you use the<br />

customer’s input to create filenames in the cache.You could run into serious problems if<br />

you allow customers to include .. or / in their input.<br />

Next, you set up the customer’s shopping cart, if she does not already have one:<br />

if(!isset($_SESSION['cart'])) {<br />

session_register('cart');<br />

$_SESSION['cart'] = array();<br />

You still have a few tasks to perform before you can display the information in the top<br />

information bar on the page (see Figure 33.1 for a reminder of what this looks like). A<br />

glimpse of the shopping cart is shown in the top bar of every page. It is therefore important<br />

that the cart variable is up to date before this information is displayed:<br />

// tasks that need to be done before the top bar is shown<br />

if($action == 'addtocart') {<br />

addToCart($_SESSION['cart'], $ASIN, $mode);<br />

}<br />

if($action == 'deletefromcart') {<br />

deleteFromCart($_SESSION['cart'], $ASIN);<br />

}<br />

if($action == 'emptycart') {<br />

$_SESSION['cart'] = array();<br />

Here, you add or delete items from the cart as necessary before displaying the cart.We<br />

come back to these functions when we discuss the shopping cart <strong>and</strong> checking out. If<br />

you want to look at them now, they are in the file cartfunctions.php.We are leaving<br />

them aside for a minute because you need to underst<strong>and</strong> the interface to Amazon first.<br />

Next, you include the file topbar.php. This file simply contains HTML <strong>and</strong><br />

a style sheet <strong>and</strong> a single function call to the ShowSmallCart() function (from<br />

cartfunctions.php). It displays the small shopping cart summary in the top-right<br />

corner of the figures.We come back to this when we discuss the cart functions.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!