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

Open Source Technologies



                   Notes                            10: <?php
                                                    11: if (isset($_SESSION[products])) {

                                                    12: echo “<strong>Your cart:</strong><ol>”;
                                                    13: foreach (unserialize($_SESSION[products]) as $p) {
                                                    14:   echo “<li>$p”;
                                                    15: }
                                                    16:  echo “</ol>”;

                                                    17: }
                                                    18: ?>
                                                    19:  <p><a href=”listing10.4.php”>return to product
                                                        choice page</a></p>

                                                    20: </body>
                                                    21: </html>
                                 Once again, we use session_start() to resume the session on line 2. We test for the presence
                                 of the $_SESSION[products] variable on line 11. If it exists, we unserialize it and loop through
                                 it on lines 13–15, printing each of the user’s chosen items to the browser. An example is shown
                                 in Figure 10.3.

                                                  Figure 10.3: Accessing an Array of Session Variables



































                                 For  a  real  shopping cart  program,  of  course,  you would  keep product  details  in  a  database
                                 and test user input, rather than blindly store and present it, but example 4 and 5 demonstrate
                                 the ease with which you can use session functions to access array variables set in other pages.






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