Page 318 - Beginning PHP 5.3
P. 318

Part III: Using PHP in Practice























                            Figure 10-2
                    H ow  I t  W orks
                  The script starts with the main decision - making logic. If the user details form was sent, it
                calls   storeInfo()  to save the details in cookies. If the  “ Forget about me! ”  link was clicked, it calls
                  forgetInfo()  to erase the cookies. If neither of those things occurred, the script calls
                  displayPage()  to display the output to the visitor:
                    if ( isset( $_POST[“sendInfo”] ) ) {
                      storeInfo();
                    } elseif ( isset( $_GET[“action”] ) and $_GET[“action”] == “forget” ) {
                      forgetInfo();
                    } else {
                      displayPage();

                    }
                 The  storeInfo()  function looks for the user info fields,  firstName  and  location , in the  $_POST
                array. For each field, if it is found then a corresponding cookie is sent to the browser to store the field
                value. Each cookie is given an expiry time of one year from today. Finally, the function sets a
                  Location:  header to cause the browser to reload the  remember_me.php  script. Note that this
                reloading will cause the browser to send the recently created cookies back to the script:

                    function storeInfo() {
                      if ( isset( $_POST[“firstName”] ) ) {
                        setcookie( “firstName”, $_POST[“firstName”], time() + 60 * 60 * 24 * 365,
                    “”, “”, false, true );
                      }
                      if ( isset( $_POST[“location”] ) ) {
                        setcookie( “location”, $_POST[“location”], time() + 60 * 60 * 24 *
                    365, “”, “”, false, true );
                      }

                      header( “Location: remember_me.php” );
                    }


              280





                                                                                                      9/21/09   9:05:12 AM
          c10.indd   280                                                                              9/21/09   9:05:12 AM
          c10.indd   280
   313   314   315   316   317   318   319   320   321   322   323