home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Web Database Applications with PHP \& MySQLWeb Database Applications with PHP \& MySQLSearch this book

11.3. Managing Redirection

The cart.5 script shown in Example 11-6 is a central point that manages redirection to other scripts in the winestore. All pages that have more than one button request this script using the action attribute of the <form> element. The script processes the requests, determines from the GET method attributes which script should be requested next, and then redirects the browser to that script.

For example, if the user clicks the Empty Cart button on any page, the following URL is requested:

http://localhost/example.cart.5.php?empty=Empty+Cart

The cart.5 script is then processed, the following if test is found to be true, and the script redirects to the cart.4 script:

// Did they want to empty the cart?
if (!empty($parameters["empty"]))
{
   // Redirect the browser to the empty page
   // using the HTTP response header "Location:"
   header("Location: example.cart.4.php");
   exit;
}

When redirecting to some scripts, the redirection also passes on the entire QUERY_STRING—the query string is stored in the PHP environment variable $QUERY_STRING—as a GET method parameter. In addition, a session variable, referer, is registered in selected cases so that in later processing the script can redirect to the original calling page.

As discussed in Chapter 10, there are several other possible approaches for managing requests for different scripts throughout an application. An alternative to the approach is to add each button to the HTML page as a separate <form> element with its own action attribute. Other approaches include using embedded links or images instead of buttons.

Example 11-6. cart.5 manages button clicks in the winestore

<?php
   // This script redirects the browser to another script,
   // depending on what parameters are provided. It is used 
   // for processing several submit buttons from an 
   // HTML <form>

   include 'include.inc';

   set_error_handler("errorHandler");

   session_start( );

   // Clean up the data, and save the results in
   // an array
   foreach($HTTP_GET_VARS as $varname => $value)
           $parameters[$varname] = clean($value, 10);

   // Did they want to view the cart?
   if (!empty($parameters["view"]))
   {
      // Redirect the browser to the cart page
      // using the HTTP response header "Location:"
      header("Location: example.cart.2.php");
      exit;
   }

   // Did they want to go home?
   if (!empty($parameters["home"]))
   {
      // Redirect the browser to the home page
      // using the HTTP response header "Location:"
      header("Location: example.cart.1.php");
      exit;
   }

   // Did they want to empty the cart?
   if (!empty($parameters["empty"]))
   {
      // Redirect the browser to the empty page
      // using the HTTP response header "Location:"
      header("Location: example.cart.4.php");
      exit;
   }

   // Did they want to update the quantities?
   if (!empty($parameters["update"]))
   {
      // Redirect the browser to the update page
      // using the HTTP response header "Location:"
      header("Location: example.cart.6.php?" .
             $QUERY_STRING");
      exit;
   }

   // Did they want to save the search?
   if (!empty($parameters["savesearch"]))
   {
      // Redirect the browser to the search save page
      // using the HTTP response header "Location:"
      header("Location: example.cart.8.php?" .
             $QUERY_STRING");
      exit;
   }

   // Did they want to login to the site?
   if (!empty($parameters["login"]))
   {
      // Save the referer page for later redirection
      if (session_is_registered("referer"))
         session_unregister("referer");
      session_register("referer");
      $referer = $HTTP_REFERER;

      // Redirect the browser to the login page
      // using the HTTP response header "Location:"
      header("Location: example.order.1.php?" .
             $QUERY_STRING");
      exit;
   }

   if (!empty($parameters["logout"]))
   {
      // Save the referer page for later redirection
      if (session_is_registered("referer"))
         session_unregister("referer");
      session_register("referer");
      $referer = $HTTP_REFERER;

      // Redirect the browser to the logout page
      // using the HTTP response header "Location:"
      header("Location: example.order.2.php?" .
             $QUERY_STRING");
      exit;
   }
   
   // Did they want to finalise the purchase?
   if (!empty($parameters["buy"]))
   {
      // Redirect the browser to the purchase page
      // using the HTTP response header "Location:"
      header("Location: example.order.3.php?" .
             $QUERY_STRING");
      exit;
   }

   // Did they want to edit customer details?
   if (!empty($parameters["account"]))
   {
      // Redirect the browser to the customer account 
      // page using the HTTP response header "Location:"
      header("Location: example.customer.2.php");
      exit;
   }

   // They got here without providing an option, so
   // there is a problem
   echo "You arrived here unexpectedly.";
?>


Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.











??????????????@Mail.ru