View Single Post
 
Old August 29th, 2003, 10:09 AM
Oregon Oregon is offline
Authorized User
 
Join Date: Jul 2003
Location: , , Russia.
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The book example works fine. Farther down this reply is a workaround for the example with register_globals turned off.

Here's the code with register_globals On:
************************************************** *******************
<?
// cookie_test.php
// assumes register_globals is on

// test for post variables and set the cookie data accordingly

if ($type_sel) setcookie ("font[type]", $type_sel, time()+3600);
if ($size_sel) setcookie ("font[size]", $size_sel, time()+3600);

// define options available

$type = array("arial", "helvetica", "sans-serif", "courier");
$size = array( "1","2","3","4","5","6","7");

echo "<html><head><title>Cookie Test</title></head><body><div align = 'center'>";

// send out a couple listboxes to get the user's preference

echo "<form method=post>";
   echo "What font type would you like to use? ";
   echo "<select name = 'type_sel'>";
      echo "<option selected value =''>default</option>";
      foreach ($type as $var) echo "<option>$var</option>";
   echo "</select><br><br>";
   echo "<br>What font size would you like to use? ";
   echo "<select name = 'size_sel'>";
      echo "<option selected value =''>default</option>";
      foreach ($size as $var) echo "<option>$var</option>";
   echo "</select><br><br>";
   echo "<input type=submit>";
echo "</form>";

// echo some information and format it using the choices selected,
// but bring in the two choices first

echo "<b>Your cookies say:</b><br>";

echo "";

   echo "\$font[type] = $font[type]<br>";
   echo "\$font[size] = $font[size]<br>";
echo " <br>";

echo "<b>Your form variables say:</b><br>";
echo "";

   echo "\$type_sel = $type_sel<br>";
   echo "\$size_sel = $size_sel<br>";
echo "";

echo "</div></body></html>";

?>

Here's the ('one workaround') code with register_globals Off:
************************************************** *******************

<?
$type_sel = $_POST[type_sel];
$size_sel = $_POST[size_sel];
$_SESSION[type_sel] = $type_sel; // super global array assignment
$_SESSION[size_sel] = $size_sel;

if ($_SESSION[type_sel]) setcookie ("font_type", $type_sel, time()+3600);

if ($_SESSION[size_sel]) setcookie ("font_size", $size_sel, time()+3600);

?>

<?
/* cookie_test.php
   With register globals turned off, either the cookies won't accept an array, or accessing
   an array from a cookie bombs (ie using $HTTP_COOKIE_VARS[font[size]]
   if session_start is used or we use the session register function, we get this error:
-----------------------------------------------------------------------------------------
CGI Error
The specified CGI application misbehaved by not returning a complete set of HTTP headers.
The headers it did return are: (nothing was displayed...)
-----------------------------------------------------------------------------------------
*/

// define options available

$type = array("arial", "helvetica", "sans-serif", "courier");
$size = array( "1","2","3","4","5","6","7");

echo "<html><head><title>Cookie Test</title></head><body><div align = 'center'>";

// send out a couple listboxes to get the user's preference

echo "<form action=$PHP_SELF? method=post>";
   echo "What font type would you like to use? ";
   echo "<select name = 'type_sel'>";
      echo "<option selected value =''>default</option>";
      foreach ($type as $var) echo "<option>$var</option>";
   echo "</select><br>";
   echo "<br>What font size would you like to use? ";
   echo "<select name = 'size_sel'>";
      echo "<option selected value =''>default</option>";
      foreach ($size as $var) echo "<option>$var</option>";
   echo "</select><br>";
   echo "<input type=submit>";
echo "</form>";

// echo some information and format it using the choices selected

echo "<b>Your cookies say:</b><br>";
$font_type = $HTTP_COOKIE_VARS[font_type];// without this asignment, cookie doesn't come in.
$font_size = $HTTP_COOKIE_VARS[font_size];// ditto...

echo "";
echo "\$font_type = $font_type<br>";
echo "\$font_size = $font_size<br>";
echo " <br>";

echo "<b>Your form variables say:</b><br>";
echo "";

   echo "\$type_sel = $type_sel<br>";
   echo "\$size_sel = $size_sel<br>";

echo "";
echo "</div></body></html>";

?>

Good Luck,
Oregon