Tracking Variables across Pages during a Session

   

Now that you know how to start a session and track some variables, how about putting this to some use? There are countless uses for sessions, but the real benefit of sessions is to reduce the amount of work for you and your Web server. Think of a typical site that has registered users. This site may have message boards, feature stories that users can respond to, and various other features that users can choose to enable or disable.

Typically, you'd store these preferences in some sort of database and load the values when the user logs into the site. You need a way to remember these values across each page that the user views. If users don't want to see a Poll feature on one page, then it's safe to assume they don't want to see it on other pages as well. You could query the database each time the user loads a page to see which features should be enabled, or you could store that information in a cookie, but there are drawbacks to each method. Querying the database each time a page loads can lead to some serious server resource problems for large sites. A cookie can only hold one value, so storing cookies for each feature option quickly turns into a coding nightmare: When you have more than one or two features, you'd need a cookie for each one!

This is where sessions can help. You can store multiple values that can be accessed across many pages. You keep the database queries to a minimum by loading the user preferences at the start of the session, and you only need to worry about storing one cookie on the user's browser, eliminating the worry of exceeding the finite number of cookies any one site can store on a browser. If you use PHP's session link rewriting feature, you don't even have to worry about that one cookie!

In this next example, you'll load some default variables into a session as if they were coming from a database query and then verify that the session variables work in Script 2-3, track-prefs2.php. The result will appear in Figure 2-2.

Script 2-2 track_prefs1.php
  1.  <?  2.  session_start();  3.  session_register("first");  4.  session_register("last");  5.  session_register("email");  6.  session_register("news_prefs");  7.  ?>  8.  <html>  9.  <head> 10.  <title>Welcome</title> 11.  <style type="text/css"> 12.    p, ul, h3 {font-family: verdana, helvetica, sans-serif;} 13.    .enabled {font-weight: bold; color: green;} 14.    .disabled {font-weight: bold; color: red;} 15.  </style> 16.  </head> 17.  <body> 18.  <? 19.  function load_user_data(){ 20.    global $first, $last, $email, $news_prefs; 21.    $first = "Faye"; 22.    $last = "Valentine"; 23.    $email = "faye@bebop.com"; 24.    $news_prefs = array( 25.      "Local" => 0, 26.      "Nation" => 1, 27.      "World" => 1, 28.      "Comics" => 0, 29.      ); 30.  } 31.  load_user_data(); 32.  ?> 33.  <h3>Welcome</h3> 34.  <p>Welcome back <b><?=$first?></b> 35.  <p>Your settings have been loaded. 36.  <p><a href=track_prefs2.php>View Your Settings</a>. 37.  </body> 38.  </html> 
Figure 2-2. track_prefs2.php

graphics/02fig02.jpg

Script 2-2. track_prefs1.php Line-by-Line Explanation

LINE

DESCRIPTION

2

Start the session or continue an existing session.

3 6

Register session variables for use throughout the session.

8 17

Print out the beginning of the HTML page.

19

Declare a new function, load_user_data().

20

Allow the function to use and modify variables in the global scope of the script. These variables correspond to the session variables that were registered earlier in the script.

21 23

Assign some strings to the session variables.

24

Assign an array to the session variable $news_prefs.

25 29

Assign items to the array.

31

Run the load_user_data() function

33 36

Notify the users that their settings have been loaded and provide a link to view the settings.

37 38

Close out the HTML for the page.

Script 2-3 track_prefs2.php
  1.  <?  2.    session_start();  3.  ?>  4.  <html>  5.  <head>  6.  <title>View Settings</title>  7.  <style type="text/css">  8.    p, ul, h3 {font-family: verdana, helvetica, sans-serif;}  9.    .enabled {font-weight: bold; color: green;} 10.    .disabled {font-weight: bold; color: red;} 11.  </style> 12.  </head> 13.  <body> 14.  <h3>View Your Settings</h3> 15.  <p>Hello <b><?=$first?> <?=$last?></b>, 16.  <p>Email: <?=$email?> 17.  <p>Your settings:<ul> 18.  <? 19.  while(list($key,$value) = each($news_prefs)) { 20.    if($value) { 21.      $value = "Enabled"; 22.    } else { 23.      $value = "Disabled"; 24.    } 25.    print("<li class=$value>$key: $value</li>"); 26.  } 27.  ?> 28.  </ul> 29.  </body> 30.  </html> 

Script 2-3. track_prefs2.php Line-by-Line Explanation

LINE

DESCRIPTION

2

Start the session or continue an existing session.

4 13

Print out the beginning of the HTML page.

14 17

Greet the users and show some of their current settings.

19 26

Execute a while loop that goes though the user's settings in the $news_prefs session variable. Print the key of the array item to the screen. If the value of the current array item is enabled, then print "Enabled" on the screen; otherwise, print "Disabled" on the screen. In either case, the value determines which CSS class should be used to display the setting.

27 30

End the HTML page.


   
Top


Advanced PHP for Web Professionals
Advanced PHP for Web Professionals
ISBN: 0130085391
EAN: 2147483647
Year: 2005
Pages: 92

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net