Forum Moderators: coopster

Message Too Old, No Replies

PHP / Apache Sensativity

How do I prevent messages like "Undefined index: "?

         

Willis

1:16 am on Apr 14, 2004 (gmt 0)

10+ Year Member


So ok as I test things out among my computer I can get a very nicely working script, if I uploaded it to a server it would do the job easily, but when I view it on my own computer I could get messages like this:

Notice: Undefined index: username in *LOCATION* on line 79

Notice: Undefined index: id in *LOCATION* on line 80

Notice: Undefined index: username in *LOCATION* on line 83

Notice: Undefined index: username in *LOCATION* on line 124

Notice: Undefined index: id in *LOCATION* on line 164

ok oviously I edited out the location for privay issues but thats besides the point. Now much if not all these errors come up because they operate off of sub categories like $id=$_REQUEST['id']; and if( $_REQUEST['id'])

either way its not disrupting my work but it seems very anoying to have such things show; I dont know if this would be an issue of PHP being too sensative, or if APACHE would be too sensative .. but if someone knows how to ignore irrelevent messages like this, yeh help plz.

ergophobe

2:04 am on Apr 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This is a function of PHP error reporting. It is a good thing the way you have it set up - lax on the live server (hide notices and warnings) and strict on the test server (show notices and warnings).

The best strategy is to correct your code so you don't get notices.

The notice in question refers to an array index. You are perhaps doing something like

if ($var['idx'])

If you have not assigned a value to $var['idx'], then the index is undefined. You have two choices:

1. assign default values to variables

$var['idx'] = "";
if ($var['idx'}

2. test variables

if (isset($var['idx'] && $var['idx'])

You get no error because if the first part is false, it stops.

If you don't like that solution and want to suppress these notices, you must change the "error handling and logging" settings in your php.ini file.

You could have PHP simply not report notices:
error_reporting = E_ALL & ~E_NOTICE

WhosAWhata

2:12 am on Apr 14, 2004 (gmt 0)

10+ Year Member



error_reporting(0);

ergophobe

8:49 pm on Apr 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yeah error_reporting(0) would work, but I still think it's the "wrong" approach on his test server

Keep in mind that he said it works on his live server, which presumably has error reporting off, logged or set not to show notices.

Willis

2:57 pm on Apr 15, 2004 (gmt 0)

10+ Year Member



well I wouldnt want to turn it OFF as a whole, that would just lead to more problems then i care to consider.

If be it possible to turn off the SPECIFIC messages of Undefined Index and Undefined Variable . . that would be nice. I will start going back and weaking what I can so they dont become notices as suggested . . but I'd rather not have to waste time doing that.

coopster

3:29 pm on Apr 15, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



ergophobe instructed how to turn off
NOTICE
errors in message #2 (see the very last line).

jatar_k

3:30 pm on Apr 15, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



check the end of ergophobe's post
You could have PHP simply not report notices:
error_reporting = E_ALL & ~E_NOTICE

or read here
error_reporting [ca.php.net]

ergophobe

3:30 pm on Apr 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




but I'd rather not have to waste time doing that.

That's my point. You are not wasting time. You have undefined indexes and undefined variables, which you should get rid of.

See message #6 by coopster in this thread
[webmasterworld.com...]

If you get in the habit of running with error reporting set to E_ALL, it won't take you more time, it will take you less time?

Why? Because the notices and warnings flag potential trouble spots in your code. It's sort of like setting "Option Explicit" in Visual Basic. Yes, it can cause problems for your code, but it avoids more problems than it causes.

For me, my development server will always be set to E_ALL, and no production server ever will be unless it sends errors to somewhere other than the screen.

jatar_k

3:49 pm on Apr 15, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



sorry Willis, I hope you don't feel like we jumped on you, seems the three of us were posting at the same time.

Willis

4:43 pm on Apr 15, 2004 (gmt 0)

10+ Year Member



ok . . . ok . . i get the picture lol. I missed that last bit of his post. Thanks.

ergophobe

5:05 pm on Apr 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yep, didn't want to do any jumping!

I was just trying to tell you why it was happening, give you two options to fix it (change the code or change the error reporting) and to try t convince you to choose #1.

Ultimately your choice. I know coopster and I (and many others here) choose #1, don't know about Jatar_K.

Another thread on the topic (same people shooting their mouths.. er fingers off, though):

[webmasterworld.com...]

jatar_k

5:18 pm on Apr 15, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I never change error_reporting

we go with all and send errors on live sites to logfiles.

There is no such thing as too much information when code misbehaves.

coopster

6:34 pm on Apr 15, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Good point, but what about contract jobs?

ergophobe

6:41 pm on Apr 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



we go with all

That's sort of what I meant by #1 (go with all and fix the code so you don't get errors). So I'll put you down for #1 too.

It has occurred to me that your solution is the best (always report all, but log them instead of sending them to the screen), but I haven't done it (for some reason I thought error reporting settings were PHP_INI_SYSTEM, not PHP_INI_ALL and could therefore only be changed where you had access to php.ini, which is not usually my case).

ergophobe

6:44 pm on Apr 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Coopster,

That's what I was thinking and alluding to in the last post. However, since you can set the

- error log file path
- max length of the log in bytes
- and you can set it from .htaccess or even at runtime [us2.php.net]

it doesn't seem like there is any real inconvenience. Then if someone tells you there's a problem with the script, you can ftp the error log. Seems like a good idea.

coopster

6:50 pm on Apr 15, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sold :)

jatar_k

9:20 pm on Apr 15, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



contract jobs never seem to be the same so it depends on what and where and with whom you are doing something. :)