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
  #1 (permalink)  
Old August 29th, 2003, 03:53 AM
Registered User
 
Join Date: Jul 2003
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Set Cookie Chapter 8 ?

Page 283 ... It doesn't work. Please check the problem, Wrox Staff.

  #2 (permalink)  
Old August 29th, 2003, 03:55 AM
Registered User
 
Join Date: Jul 2003
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It seems that cookies have not been set. There is nothing when I refresh the page..still the same.

  #3 (permalink)  
Old August 29th, 2003, 10:09 AM
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

  #4 (permalink)  
Old August 30th, 2003, 02:06 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

One immediate problem that I can see is that you need to have all of your array strings quoted. The reason for this is an unquoted array string will be first assumed a defined constant and in the following PHP will try to find a constant named 'type' if one is not found PHP will assume this an unquoted string and issue a warning under error reporting E_ALL. Needless to say this isn't a very good idea.

See also:
http://us3.php.net/manual/en/language.types.string.php

if ($font[type]) echo "face=$font[type] ";
   if ($font[size]) echo "size=$font[size] ";

And with writing an equivilent expression for:
setcookie ("font[type]", $type_sel, time()+3600);

This should now be available in the following variable:
$_COOKIE["font"]["type"];

Again, don't use the deprecated GLOBAL arrays (i.e. HTTP_*_VARS), use the new superglobal array equivalents. That is unless you're stuck with using PHP earlier than 4.1. In which case I would strongly suggest designing a work around yourself to support the new method, (i.e... $_COOKIE = $HTTP_COOKIE_VARS; $_POST = $HTTP_POST_VARS;).

See also:
http://us3.php.net/manual/en/languag...predefined.php

: )
Rich


:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
  #5 (permalink)  
Old January 22nd, 2004, 10:06 AM
Registered User
 
Join Date: Jan 2004
Location: Amersfoort, , Netherlands.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I get the following error (register_globals = on.

Your cookies say:
Notice: Use of undefined constant type - assumed 'type' in D:\Downloads\PHP4\Examples\BegPHP\ch08\cookie_test .php on line 27

This is the line of code:
     if ($font[type]) echo "FACE=$font[type] ";
  #6 (permalink)  
Old January 22nd, 2004, 05:28 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

This is happening because you aren't quoting your array indice..

Quote:
quote:
if ($font[type]) echo "FACE=$font[type] ";
if ($font['type']) echo "FACE=$font[type] ";

The conditional expression will accept the code, in PHP if you do not quote an associative array indice the program looks for a defined constant by that name, if none are found the program issues a notice level error and assumes it to be the string literal.

http://www.php.net/manual/en/language.types.array.php

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
  #7 (permalink)  
Old January 23rd, 2004, 09:03 AM
Registered User
 
Join Date: Jan 2004
Location: Amersfoort, , Netherlands.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Rich,

Your solution doesn't seem to work either. I found a workaround.
If I replace all occurrences of font[type] and font[size] with cookie_type and cookie_size resp., it works fine. I don't see the point of using an array here.

Regards,
Jaap Zwaan
  #8 (permalink)  
Old January 23rd, 2004, 03:20 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

Quote:
quote:Your solution doesn't seem to work either. I found a workaround.
If I replace all occurrences of font[type] and font[size] with cookie_type and cookie_size resp., it works fine. I don't see the point of using an array here.
OK, but I didn't provide a solution. I pointed out errors in your syntax.. if your program wasn't functioning you should post code and commentate as to what you expect from it.

: )
Rich


:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
  #9 (permalink)  
Old January 26th, 2004, 04:44 AM
Registered User
 
Join Date: Jan 2004
Location: Amersfoort, , Netherlands.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Rich,

Thanks for your support. Sorry that I didn't post the complete code, because it is available here at wrox. I simply tried to use the example as downloaded from this file, but it didn't work. I hoped someone else had the same problem and knew how to fix this.

I don't know, but with most of the code provided with this book, i get loads of errors and warnings. Is the newer version of PHP more strict?

Thanks again,

Jaap Zwaan
  #10 (permalink)  
Old January 26th, 2004, 05:34 AM
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

Hey Jaap,

No worries!
I should have pointed out that most of us, that is those of us that answer questions frequently, don't have the book. I just sold my copy a week or two ago... I've already learned the language and didn't much see the need to hang onto it.

The short answer is yes, it is more strict.

The Wrox book came out before certain adjustments in the language to tighten up security were made... the two having the most adverse effects being:

register_globals = off

AND

error_reporting = E_ALL

The Wrox book written before all this happened did not structure examples to accomodate either one.

When I first began learning the language I thought the adjustments were silly, but now having a little experience under my belt I can see that their benefits outweigh their nuisance greatly.

To me those being, both allow the creation of more easily understood code with regards to readability and logic flow. And the discovery of errors with E_ALL along the lines of non-initialized variables is in itself invaluable to bug tracking... this is particularly useful if you mispell a variable or array indice.. the program, most times, will catch that and point it out to you in the form of a NOTICE level error. In your case the NOTICE level error came in the form of an undefined constant.

Some extra reading as to why the adjustments were made:
http://www.php.net/manual/en/security.index.php

...which contains a wealth of information on PHP security in general.

The PHP manual is always helpful.. doesn't do much hand holding.. but you'll find it very useful as you learn the language.

http://www.php.net/manual

Cheers!
: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::




Similar Threads
Thread Thread Starter Forum Replies Last Post
Set-Cookie: PHPSESSID=deleted ajit Beginning PHP 0 October 23rd, 2007 08:29 AM
set the cookie file name crmpicco Javascript How-To 4 February 17th, 2006 05:04 AM
Problem with set cookie chapter 8 stephen_c_ BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 2 December 15th, 2004 02:35 PM
Set Cookie rowlandk Beginning PHP 5 April 6th, 2004 01:18 PM
getting set cookie value from a script abbylee26 Javascript 1 July 17th, 2003 03:01 AM





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