Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5
This is the forum to discuss the Wrox book Beginning PHP4 by Wankyu Choi, Allan Kent, Chris Lea, Ganesh Prasad, Chris Ullman; ISBN: 9780764543647
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 25th, 2003, 04:32 PM
Authorized User
 
Join Date: Jul 2003
Location: , , Russia.
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 8 and register_globals off

I've had fun with the workarounds on the book examples in Chapter 8 (that work fine with register_globals on), adjusting them to work with register_globals off.

In the Using Cookies... example on page 283, I either couldn't get the cookies to set, or I just couldn't access them as array elements.
By assigning the cookie as a regular variable (say, "font_type"), I could read the cookie by accessing the (persisting) $HTTP_COOKIE_VARS[font_type] variable. This variable wouldn't let me access an array element (ie. $HTTP_COOKIE_VARS[font[type]]).

I'm new to PHP, so maybe I'm missing something, but it looks to me that the register_globals variable is quite infused in the supporting code? I've used session_register() and had strange results and had to rely on the super global variables
(ie $_SESSION[type_sel]) to gain persistence.

Has anyone else seen the same thing? I tried to stay as close to the original example code as possible. My stuff's working, but it's not what I thought it would look like.

The GLOBALS array in the page count example, with the format as:
$GLOBALS["view${whichpage}count"], doesn't work with register_globals off.

I noticed that the super global variable:
$_SESSION['view${whichpage}count'] looks like it should work, but it doesn't. For example:

for ($i = 1;$i <= 4;$i ++) {
   $_SESSION['whichpage[$i]'] = 0;
   // this really only sets $whichpage and not it's elements...
   $_SESSION['view${i}count'] = 0; // same here.

For what it's worth, this is my workaround on the page_count example,
with register_globals off:

<?
session_start();
if ($_SESSION['status'] != "underway") {
   $_SESSION['view1count'] = 0;
   $_SESSION['view2count'] = 0;
   $_SESSION['view3count'] = 0;
   $_SESSION['view4count'] = 0;
   $status = "underway";
   $_SESSION['status'] = $status;
}
?>

<?
// page_count.php
// this doesn't work exactly like the original. the results are the same though.

echo "<html><head><title>Web Page Hit Counter</title></head><body></body>";

$whichpage = $_GET['whichpage'];
if ($whichpage) {
   echo "<b>You are currently on page $whichpage.</b><br><br>\n";
   switch ($whichpage) {
      case $whichpage = 1:
      $_SESSION['view1count']++;
      break;
      case $whichpage = 2:
      $_SESSION['view2count']++;
      break;
      case $whichpage = 3:
      $_SESSION['view3count']++;
      break;
      case $whichpage = 4:
      $_SESSION['view4count']++;
      default:
      break;
   }
}

for ($i = 1; $i<=4; $i++) {
   if ($whichpage == $i) {
      echo "<b><a href=\"$PHP_SELF?".SID."&whichpage=$i\">Page $i</a></b>";
   }
   else {
      echo "<a href=\"$PHP_SELF?".SID."&whichpage=$i\">Page $i</a>";
   }
   switch ($whichpage) {
      case $whichpage = 1:
      $page_count = $_SESSION['view1count'];
      $_SESSION['page1count'] = "$page_count times.";
      break;
      case $whichpage = 2:
      $page_count = $_SESSION['view2count'];
      $_SESSION['page2count'] = "$page_count times.";
      break;
      case $whichpage = 3:
      $page_count = $_SESSION['view3count'];
      $_SESSION['page3count'] = "$page_count times.";
      break;
      case $whichpage = 4:
      $page_count = $_SESSION['view4count'];
      $_SESSION['page4count'] = "$page_count times.";
      break;
      default:
      break;
   }
   $page_string[1] = $_SESSION['page1count'];
   $page_string[2] = $_SESSION['page2count'];
   $page_string[3] = $_SESSION['page3count'];
   $page_string[4] = $_SESSION['page4count'];
   echo " which you have chosen ";
   echo " $page_string[$i]<br>\n";
}
echo "\n\n<br><br>\n\n";
echo "</body></html>";

?>
__________________
I keep forgetting that my memory is slipping.
 
Old August 30th, 2003, 01:33 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Hi Oregon,

In the Using Cookies... example on page 283, I either couldn't get the cookies to set, or I just couldn't access them as array elements.
By assigning the cookie as a regular variable (say, "font_type"), I could read the cookie by accessing the (persisting) $HTTP_COOKIE_VARS[font_type] variable. This variable wouldn't let me access an array element (ie. $HTTP_COOKIE_VARS[font[type]]).

As far as this goes you should set up sub-elements in the proper array syntax.

$HTTP_COOKIE_VARS[font[type]]
should be
$_COOKIE["font"]["type"];
Because what you're really trying to do is create a multi-dimensional array.
This would correspond to
$font["type"] with the register_globals directive turned on.

Try to avoid the deprecated globals, i.e. HTTP_*_VARS
Stick with the one word capitalized superglobal array names like $_POST, $_GET, $_COOKIE... etc.

All code can be transformed to work with the register_globals directive being turned off! So don't get discouraged if what appears to be a sound theory doesn't immediately work out.

$PHP_SELF should also be changed to $_SERVER["PHP_SELF"];

I realize also that Beginning PHP4 has used the single '<?' opening statement throughout many of the books examples but this should be avoided too. Always use '<?php' which is 1.) XML compliant and 2.) Gauranteed to be portable, as the '<?' opening delimiter is not always turned on in php.ini.

Code:
<?php
session_start();
if ($_SESSION['status'] != "underway") {
   $_SESSION['view1count'] = 0;
   $_SESSION['view2count'] = 0;
   $_SESSION['view3count'] = 0;
   $_SESSION['view4count'] = 0;
   $_SESSION['status'] = "underway";
}
?>

<?php
// page_count.php
// this doesn't work exactly like the original. the results are the same though.

echo "<html><head><title>Web Page Hit Counter</title></head><body></body>";

Another of the many blunders of the PHP4 book is its inability to show explicit examples, using if($_GET["whichpage"]) is a bad idea... you should be more explicit.

Use the empty() function which checks for null value and the isset() function which checks for a variable's existence.
Ok let's walk through your code and have a look at some of your mistakes.


if (isset($_GET["whichpage"]) && !empty($_GET['whichpage'])) {

    echo "<b>You are currently on page {$_GET['whichpage'}.</b><br><br>\n";

    switch ($_GET['whichpage') {

        # Since the switch control structure is already looking at the value
        # of $_GET["whichpage"], including the comparision operator in the 
        # case statement is not neccessary.  
        # Actually in your code you use the assignment operator which 
        # would not provide the desired result, you're just overwriting the value of $whichpage
        # I also like to stick with the superglobal array throughout a script
        # In the long run it makes things much easier to understand

        case 1:
            $_SESSION['view1count']++;
            break;

        case 2:
            $_SESSION['view2count']++;
            break;

        case 3:
            $_SESSION['view3count']++;
            break;

        case 4:
            $_SESSION['view4count']++;
            break;

        default:

    }

}

for ($i = 1; $i<=4; $i++) {

    /* Sometimes in a windows enviornment the $_SERVER["PHP_SELF"] variable does not have a value.  This workaround will fix that */

    if (!isset($_SERVER["PHP_SELF"]) || empty($_SERVER["PHP_SELF"])) {

        $_SERVER["PHP_SELF"] = $_SERVER["SCRIPT_NAME"];

    }


    if ($_GET["whichpage"] == $i) {

        echo "<b><a href=\"{$_SERVER["PHP_SELF"]}?".SID."&whichpage=$i\">Page $i</a></b>";

    } else {

        echo "<a href=\"{$_SERVER["PHP_SELF"]}?".SID."&whichpage=$i\">Page $i</a>";

    }

       switch ($_GET["whichpage"]) {

         case 1:

            # don't write more code than you have to
            $_SESSION['page1count'] = $_SESSION['view1count']." times.";
                  break;

        case 2:

                  $_SESSION['page2count'] = $_SESSION['view2count']." times.";
                  break;

        case 3:

            $_SESSION['page3count'] = $_SESSION['view3count']" times.";
            break;

        case 4:

            $_SESSION['page4count'] = $_SESSION['view4count']." times.";
            break;

        default:
            #No need to break; the default statement as there is not another statement to fall through to

       }

    $page_string[1] = $_SESSION['page1count'];
    $page_string[2] = $_SESSION['page2count'];
    $page_string[3] = $_SESSION['page3count'];
    $page_string[4] = $_SESSION['page4count'];

    echo " which you have chosen ";
    # Get used to using curly syntax in everything!  
    echo " {$page_string[$i]}<br>\n";
}
echo "\n\n<br><br>\n\n";
echo "</body></html>";

I have no idea if this completely fixes your code, I just fixed all the obvious errors that I saw. Much of what you wrote could be much more easily accomplished with a loop.

Your code could be written in this simplified equivilent (minus the errors!):

Code:
<?php

session_start();

    if ($_SESSION["status"] != "underway") {

        $_SESSION["view1count"] = 0;
        $_SESSION["view2count"] = 0;
        $_SESSION["view3count"] = 0;
        $_SESSION["view4count"] = 0;
        $_SESSION["status"]     = "underway";

    }

    if (isset($_GET["whichpage"]) && !empty($_GET["whichpage"])) {

        echo "<b>You are currently on page {$_GET["whichpage"}.</b><br><br>\n";   

        if (!isset($_SERVER["PHP_SELF"]) || empty($_SERVER["PHP_SELF"])) {

            $_SERVER["PHP_SELF"] = $_SERVER["SCRIPT_NAME"];

        }

        for ($i = 1; $i <= 4; $i++;) {                    

            if ($_GET["whichpage"] == $i) {

                $_SESSION["view{$i}count"]++;

                $_SESSION["page{$i}count"] = $_SESSION["view{$i}count"]." times.";
                echo "<b><a href=\"{$_SERVER["PHP_SELF"]}?".SID."&whichpage=$i\">Page $i</a></b> | ";


            } else {

                echo "<a href=\"{$_SERVER["PHP_SELF"]}?".SID."&whichpage=$i\">Page $i</a>";

            }

            $page_string[$i] = $_SESSION['page{$i}count'];

            echo " which you have chosen ";
            echo " {$page_string[$i]}<br>\n";

        }        

    }

?>
Try that out and let me know how it goes.

: )
Rich



:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old July 24th, 2005, 01:16 PM
Registered User
 
Join Date: Jul 2005
Location: New York, NY, USA.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm not sure anyone is still reading the forum BUT this code will not let you ever get started:

session_start();

if ($_SESSION["status"] != "underway") {

$_SESSION["view1count"] = 0;
$_SESSION["view2count"] = 0;
$_SESSION["view3count"] = 0;
$_SESSION["view4count"] = 0;
$_SESSION["status"] = "underway";

}

if (isset($_GET["whichpage"]) && !empty($_GET["whichpage"])) {

When you start whichpage is empty so you never start the program.
I changed it to:

    if (isset($_GET["whichpage"]) || ($_SESSION["status"] == "underway")) {

and everything is nifty.
Complete code:

<?php

session_start();
header("Cache-control: private"); // IE 6 Fix.

    if ($_SESSION["status"] != "underway") {
        $_SESSION["view1count"] = 0;
        $_SESSION["view2count"] = 0;
        $_SESSION["view3count"] = 0;
        $_SESSION["view4count"] = 0;
        $_SESSION["status"] = "underway";

    }

    if (isset($_GET["whichpage"]) || ($_SESSION["status"] == "underway")) {

        echo "<b>You are currently on page {$_GET["whichpage"]} . </b><br><br>\n";

        if (!isset($_SERVER["PHP_SELF"]) || empty($_SERVER["PHP_SELF"])) {

            $_SERVER["PHP_SELF"] = $_SERVER["SCRIPT_NAME"];

        }


        for ($i = 1; $i <= 4; $i++) {
            if ($_GET["whichpage"] == $i) {

                $_SESSION["view{$i}count"]++;

                $_SESSION["page{$i}count"] = $_SESSION["view{$i}count"]." times.";
                echo "<b><a href=\"{$_SERVER["PHP_SELF"]}?".SID."&whichpage=$i\">Page $i</a></b> | ";


            } else {

                echo "<a href=\"{$_SERVER["PHP_SELF"]}?".SID."&whichpage=$i\">Page $i</a>";

            }

            $page_string[$i] = $_SESSION["page{$i}count"];

            echo " which you have chosen ";
            echo " $page_string[$i] <br>\n";

        }

    }



?>






 
Old July 26th, 2005, 03:26 PM
Registered User
 
Join Date: Jul 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This example, like every other one I've tried, is riddled with problems. It uses a deprecated way of setting session variables, and the second if statement (which is supposed to put the current page in bold) is just wrong.

I bought the book last week, and wish I could get my money back.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 2 page 48 'register_globals' question wndy26 BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 5 July 1st, 2004 11:07 PM
PHP - register_globals off/on Tachyon Beginning PHP 2 May 18th, 2004 07:38 PM
Does the code work with register_globals off? GraphicArmy BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 3 April 6th, 2004 10:10 PM
register_globals lunadellest Beginning PHP 1 September 20th, 2003 11:33 AM
register_globals=Off Cambo BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 4 August 15th, 2003 03:37 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.