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

Open Source Technologies



                   Notes         10.4.2 Working with Session Variables

                                 If you are using a pre-4.1.x version of PHP, the $_SESSION superglobal is not present, and
                                 session functionality is much different.

                                       Example:   2: Add two  variables into the  $_SESSION superglobal:  product1 and
                                               product2 (lines 10 and 11).

                                                Storing Variables in a Session
                                                1:  <?php

                                                2:  session_start();
                                                3:  ?>
                                                4:  <html>
                                                5:  <head>
                                                6:  <title>Listing 10.2 Storing variables in a session</ title>
                                                7:  </head>
                                                8:  <body>
                                                9:  <?php
                                               10:  $_SESSION[product1] = “Sonic Screwdriver”;

                                               11:  $_SESSION[product2] = “HAL 2000”;
                                               12:  echo “The products have been registered.”;
                                               13:  ?>
                                               14:  </body>
                                               15:  </html>
                                 Creates a separate PHP script that accesses the variables stored in the $_SESSION superglobal
                                 in example2.
                                       Example:    3:  Accessing Stored Session Variables

                                                    1: <?php
                                                    2: session_start();

                                                    3: ?>
                                                    4: <html>

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

                                                    9: <?php
                                                    10: echo “Your chosen products are:”;
                                                    11:  echo “<ul><li>$_SESSION[product1]  <li>$_SESSION
                                                        [product2]\n</ul>\n”;



        170                               LOVELY PROFESSIONAL UNIVERSITY
   170   171   172   173   174   175   176   177   178   179   180