Page 321 - Beginning PHP 5.3
P. 321

Chapter 10: Preserving State With Query Strings
                         to the browser as a cookie called  PHPSESSID  (by default). However, if the browser has sent a  PHPSESSID
                          cookie to the server because a session already exists,   session_start()  uses this existing session:
                             session_start();
                          There ’ s one gotcha though: because  session_start()  needs to send the  PHPSESSID  cookie in an HTTP
                         header when it creates a session, you must call it before you output anything to the browser, much like
                         you do with   setcookie() :
                             Hi there!
                               < ?php
                               // Generates a “Cannot send session cookie - headers already sent” warning
                               session_start();
                             ? >


                           Reading and Writing Session Data

                           Working with session data in PHP is also simple. You store all your session data as keys and values in
                         the   $_SESSION[]  superglobal array. So you might store the user ’ s first name using:


                             $_SESSION[“firstName”] = “John”;
                           You could then display the user ’ s first name  —  whether in the same page request or during a later
                         request  —  as follows:

                               echo( $_SESSION[“firstName”] );

                           You can store any type of data in sessions, including arrays and objects:


                               $userDetails = array( “firstName” =>  “John”, “lastName” =>   “Smith”, “age” =>
                             34 );
                               $_SESSION[“userDetails”] = $userDetails;

                           However, if storing objects make sure you include your class definitions (or class definition files) before
                         trying to retrieve the objects from the   $_SESSION  array, so that the PHP engine can correctly identify the
                         objects when they ’ re retrieved:
                               session_start();

                               class WebUser {
                                 public $firstName;
                                 public $lastName;
                               }
                               if ( isset( $_SESSION[“user”] ) ) {
                                 // Make sure the WebUser class is defined by this point
                                 print_r( $_SESSION[“user”] );
                               } else {
                                 echo “Creating user...”;
                                 $user = new WebUser;

                                 $user-> firstName = “John”;

                                 $user-> lastName = “Smith”;
                                 $_SESSION[“user”] = $user;

                               }


                                                                                                         283



          c10.indd   283                                                                              9/21/09   9:05:13 AM
          c10.indd   283
                                                                                                      9/21/09   9:05:13 AM
   316   317   318   319   320   321   322   323   324   325   326