Page 178 - Open Soource Technologies 304.indd
P. 178

Unit 10: Cookies



                              27: <option value=”Hal 2000”>Hal 2000</option>                      Notes

                               28: <option value=”Tardis”>Tardis</option>
                               29: <option value=”ORAC”>ORAC</option>

                               30: <option value=”Transporter bracelet”>Transporter
                                  bracelet</option>

                               31: </select>
                               32:
                               33: <P><input type=”submit” value=”choose”></p>

                               34: </form>
                               35:
                               36: <p><a href=”listing10.5.php”>go to content page</a></p>

                               37: </body>
                               38: </html>
            We start or resume a session  by calling  session_start() on line 2. This should give us
            access to any previously set session variables. We begin an HTML form on line 23 and, on line
            25, create a SELECT element named form_products[], which contains OPTION elements
            for a number of products. Remember that HTML form elements that allow multiple selections
            should have square brackets appended to the value of their NAME arguments. This makes the
            user’s choices available in an array.

            Within the block of PHP code beginning on line 10, we test for the presence of the  $_
            POST[form_products] array (line 11). If the variable is present, we can assume that the form
            has been submitted and information has already been stored in the $_SESSION superglobal.
            We then test for an array called $_SESSION[products] on line 12. If the array exists, it was
            populated on a previous visit to this script, so we merge it with the $_POST[form_products]
            array, extract the unique elements, and assign the result back to the $products array (lines
            13–15). We then add the $products array to the $_SESSION superglobal on line 16.

            Line 36 contains a link to another script, which we will use to demonstrate our access to the
            products the user has chosen. We create this new script in example.

                  Example: 5:  Accessing Session Variables

                              1:  <?php
                              2:  session_start();
                              3:  ?>
                              4:  <html>

                              5: <head>
                              6:  <title>Listing 10.5 Accessing session variables</title>
                              7:  </head>
                              8:  <body>

                              9:  <h1> Content Page</h1>


                                             LOVELY PROFESSIONAL UNIVERSITY                                   173
   173   174   175   176   177   178   179   180   181   182   183