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
:::::::::::::::::::::::::::::::::