Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 12th, 2004, 10:45 AM
Registered User
 
Join Date: Jul 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Undefined Variable

I am using the file "file_upload.php" from the Beginning PHP4 book.
When the page is loaded the form appears, but I get the following message

Notice: Undefined variable: action in \file_upload.php on line 44
line 44 is -
if($action == "upload") upload_file();
else upload_form();

There is a hidden field on the form called "action"
<FORM METHOD="POST" ENCTYPE="MULTIPART/FORM-DATA"
   ACTION="<? echo $PHP_SELF ?>">
   <INPUT TYPE="hidden" NAME="action" VALUE="upload">
   Select file to Upload !
   <INPUT TYPE="FILE" NAME="userfile">
   <INPUT TYPE="SUBMIT" NAME="SUBMIT" VALUE="upload">
</FORM>

If I select a file and submit, the file uploads correctly.

Any suggestions for getting rid of the error message would be appreciated

Thanks

Frank

Reply With Quote
  #2 (permalink)  
Old July 12th, 2004, 11:58 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

At a guess, to get rid of the error message, first find your variable...

if($_POST['action'] == "upload") upload_file();

Take it easy,
Dan
Reply With Quote
  #3 (permalink)  
Old July 12th, 2004, 12:45 PM
Registered User
 
Join Date: Jul 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dan,

I made the change as follows
if($_POST['action'] == 'upload') upload_file();

Now I get the message
Notice: Undefined index: action in file_upload.php on line 44

As in the previous case, the file uploads properly.

It looks like that the "if" statement is being processed before the variable 'action' has been declared. Is that possible in PHP?

Would it help to post the page - it's not very long

Thanks

Frank

Reply With Quote
  #4 (permalink)  
Old July 13th, 2004, 06:13 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The "Notice: Undefined index" message usually means that the PHP engine has searched for an index to an array (in this case the $_POST array) and not found it. Are you sure you've got the quotes around 'action'? If not, that would explain this behaviour. If you refer to any array member via its associative index value (e.g. $_POST[action]) without quoting the index name, PHP will search for a constant so-named, to index the required element of the array. Since it won't find such a constant, it punts out the notice message. it then quickly swapping in a quoted string version of what you supplied, as a last resort, to see if that works. So, effeectively you get both the error AND the desired behaviour. That would explain why the code works. I might also hazard a guess that you have register_gflobals set to "on", and error reporting set to output no-critical notice messages. That, in turn, explains why the code worked before. The PHP engine looked for avariable called $action, didn't find it as a variable declared explicitly within the scope of the page, punted out the notice, and had a quick search through the gloabl arrays (like $HTTP_POST_VARS, $HTTP_GET_VAR, etc.) found a suitably-named array memebr and tried that, instead, as a last resort. That's my guess as to what's happening, at least.

Take it easy,
Dan
Reply With Quote
  #5 (permalink)  
Old July 13th, 2004, 03:18 PM
Registered User
 
Join Date: Jul 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dan,

The PHP code I'm running was taken directly from Beginning PHP4 - File & Directory Handling - Page 369 - file_upload.php

It would appear to be a bug. The code is checking for a variable before the form that creates the variable is "submitted"

Thanks for responding

Frank

Reply With Quote
  #6 (permalink)  
Old July 14th, 2004, 07:10 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, "Beginning PHP 4" is fairly old, now. I remember talking to Chris Ullman when he was working on it, and that must have been four years ago - at least - IIRC. The way PHP handles global arrays has been modified since then. You have to refer to variables explicitly, and global array variables are no longer extracted into page-scope automatically. I don't know whether you bought your copy of this book recently, but if Wiley are still selling this book unmodified*, then I'd suggest they deserve to be contacted about this. A book this old needs a revised second edition. I suppose you could:

extract($_POST)
and extract($_GET)

...At the top of each page, to get the same behavour as the book is expecting. Haven't tried it, but it's a suggestion.

Dan
*(forgive my ignorance: although I used to work for Wrox/glasshaus, the only Wrox book I have any regular use for, myself, these days, is Mike Kay's XSLT Prog Ref)
Reply With Quote
  #7 (permalink)  
Old July 14th, 2004, 09:06 AM
Registered User
 
Join Date: Jul 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dan,

I was getting the error when running the code on my local Win2000/IIS5 PC.
I uploaded the file to my hosted site that's running on a unix box, and it ran with no errors.
So I guess I'll just forget it.

Thanks for your help

Frank

P.S.
I bought the book new at a Chapters book store at the end of May this year.

Reply With Quote
  #8 (permalink)  
Old July 14th, 2004, 11:22 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The live server presumably has Notice Reporting switched off (check what phpinfo() says about thhis on the Windows machine). In a live environment, you don't particularly want error messages spitting out the actual paths to things like the PHP default include directory, and the like, in the browser - especially when they're not actually errors - since it gives away too much information about the server environment to potential attackers. (There was a live server running at Wrox, once, that used to fall over every few weeks with a message to the effect of: "'Wrox Empire' Secret Death Weapon is Out of Memory, Please Rebooot, Microsoft Active Directory Error: ..." and then the full and actual path to the Database Server... Which probably rates alongside the Querystring-based authentication system that was used on the admin interface of the main Wrox website, for lameness. :)

Such messages are useful in a test environment, though.
Dan
Reply With Quote
  #9 (permalink)  
Old July 14th, 2004, 08:12 PM
Registered User
 
Join Date: Jul 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dan,

I ran phpinfo.php on my test win2000 pc but I couldn't find a Notice Reporting setting

Your answer makes a lot of sense though. I tried a couple of other similiar scripts and they all get an error message on my pc but not on the ISP box.

It's been an interesting learning experience - I really had to try and figure out what the code was doing, rather than just using it<g>

Thanks for your help

Frank

Reply With Quote
  #10 (permalink)  
Old July 15th, 2004, 06:24 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, FWIW, the actual setting is called "error_reporting" (http://uk.php.net/error-reporting") and there are several levels of alert it can generate. You can use the ini_set() function to actually rewrite settings like these, at page level, (http://uk.php.net/manual/en/function.ini-set.php), but use this with great caution in a live environment. Your hosts will not thank you for allowing critical subsystem data to be output in a browser :). (That said, the number of times I've browsed the web and found various people's phpinfo() ouput blithely squirting crucial data about their software environment out into the ether in a nice, concise, readable form is frightening. ;)

Dan
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Undefined variable adel_88 BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 2 July 9th, 2007 03:21 PM
Undefined Variable aspire Beginning PHP 4 July 12th, 2006 07:00 AM
Undefined variable najib Beginning PHP 4 July 1st, 2005 08:12 AM
undefined variable !! cody44 Beginning PHP 3 January 20th, 2005 05:47 PM
Undefined Variable Some Help! knight Beginning PHP 7 February 19th, 2004 07:07 AM



All times are GMT -4. The time now is 07:56 PM.


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