This is a discussion on Unidentified Index Problem on some servers within the PHP Language forums, part of the PHP Programming Forums category; I'm getting an error message "Unidentified Index" when reading posted variables from a form $x = $_POST['VariableName'] ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I'm getting an error message "Unidentified Index" when reading posted
variables from a form $x = $_POST['VariableName'] I know I can use 'isset()' to check them first, but I'm curious as to why this works on some servers and not others......... Most of the servers I run it on just ignore the problem and result in the variable being assigned empty. I've tries on 3 different installations: 1 Linux ( No Error message) 1 Windows XP ( No Error message ) 1 Windows 2000 ( Fails! ) Is it a php.ini setting? Regards, Neil |
|
|||
|
Neil Strong wrote:
> I'm getting an error message "Unidentified Index" when reading posted > variables from a form > > $x = $_POST['VariableName'] > > I know I can use 'isset()' to check them first, but I'm curious as to why > this works on some servers and not others......... Most of the servers I run > it on just ignore the problem and result in the variable being assigned > empty. > > I've tries on 3 different installations: > > 1 Linux ( No Error message) > 1 Windows XP ( No Error message ) > 1 Windows 2000 ( Fails! ) > > Is it a php.ini setting? from: http://php.net/error_reporting It seems that using E_NOTICE (included with E_ALL) is the only way to get warnings about undefined variables. For example, if you type $soemthing when you mean $something, you may not get any message about it unless you use E_NOTICE level reporting. The problem is, at that level of reporting you also get notices about array indexes that have not been set. This means lots of warnings when using $_GET['formvariable'] and such. You can check isset($_GET['formvariable']) first, but that gets annoying, especially when it is redundant to stricter input validation you need to do anyway. The only solution I have found is to use set_error_handler() to register a custom error reporting function to report everything except where the error string starts with "Undefined index:". Then I call error_reporting(E_ALL). This seems to be the best compromise. function error_handler($errno, $errstr, $errfile, $errline, $errctx) { if ($errno == E_NOTICE && substr($errstr, 0, 17) == "Undefined index: ") return; echo "\nerror_handler:\n\terrno=$errno\n\terrstr=$errst r\n"; echo "\terrfile=$errfile\n\terrline=$errline\n"; if ($errno & (E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR)) die(); } set_error_handler("error_handler"); error_reporting(E_ALL); checks the others comments too... Regards -- David |
|
|||
|
Turn down error reporting.
You probably have it set WAY to high. Also make sure you don't have something like this in your script if(error)die; Oh yeah also for post variables I just like to use extract($_POST); make sure you don't try $x = extract($_POST); Otherwise = $x winds up just being null or 0 or 1 I think, depending on you version. extract assigns all of them automagically If thats to sloppy and full of holes for ya try this if($_POST['VariableName']){ $x = $_POST['VariableName']; } "Neil Strong" <Neil_Strong@nodomain.com> wrote in message news:<bnit2q$3vv$1@visp.bt.co.uk>... > I'm getting an error message "Unidentified Index" when reading posted > variables from a form > > $x = $_POST['VariableName'] > > I know I can use 'isset()' to check them first, but I'm curious as to why > this works on some servers and not others......... Most of the servers I run > it on just ignore the problem and result in the variable being assigned > empty. > > I've tries on 3 different installations: > > 1 Linux ( No Error message) > 1 Windows XP ( No Error message ) > 1 Windows 2000 ( Fails! ) > > Is it a php.ini setting? > > Regards, > Neil |
|
|||
|
Steve <gr82meetu78@yahoo.com> wrote:
> Turn down error reporting. > You probably have it set WAY to high. Error reporting _can't_ be to high :) > if($_POST['VariableName']){ > $x = $_POST['VariableName']; > } This will generate a warning if the index doens't exist, since the code aboce tries to access the index. Correct example would be: if(array_key_exists('VariableName',$_POST)) { $x=$_POST['VariableName']; } -- Daniel Tryba |
|
|||
|
Hi Steve!
On 27 Oct 2003 20:07:44 -0800, gr82meetu78@yahoo.com (Steve) wrote: >Oops hehe, you're right, I stand corrected. >On the other hand, the site shouldn't die either way, and errors >should be reported to a log, not dumped to the screen for everyone to >see. >IMHO this is one of PHP greatest failings in it's default >configuration. >There is however a way to fix this in the php.ini file. Depends a bit, if you want the bugs now or the bugs later. I love error notices, because they tell me, when - I misplelled a variable, property etc. - Assumed wrongfully a variable is an array, a scalar etc. - I didn't check a case - I don't have an else part for an if. All this will lead to a bug sometime, because someone will use the script in a way I haven't though of. I have notices off of course on the production server, but develop a script, until it runs without notices. HTH, Jochen > >Also error reporting can be too high, a person doesn't nessecarily >need to know about every single "notice". And a script certainly >shouldn't die when encountering a notice, only an error. This is what >I'm reffering to when I mean having it set to high. >Sorry about the confusion. > >Daniel Tryba <news_comp.lang.php@canopus.nl> wrote in message news:<bnjsab$gdc$1@news.tue.nl>... >> Steve <gr82meetu78@yahoo.com> wrote: >> > Turn down error reporting. >> > You probably have it set WAY to high. >> >> Error reporting _can't_ be to high :) >> >> > if($_POST['VariableName']){ >> > $x = $_POST['VariableName']; >> > } >> >> This will generate a warning if the index doens't exist, since the code >> aboce tries to access the index. >> >> Correct example would be: >> >> if(array_key_exists('VariableName',$_POST)) >> { >> $x=$_POST['VariableName']; >> } -- Jochen Daum - CANS Ltd. PHP DB Edit Toolkit -- PHP scripts for building database editing interfaces. http://sourceforge.net/projects/phpdbedittk/ |
|
|||
|
Oops hehe, you're right, I stand corrected.
On the other hand, the site shouldn't die either way, and errors should be reported to a log, not dumped to the screen for everyone to see. IMHO this is one of PHP greatest failings in it's default configuration. There is however a way to fix this in the php.ini file. Also error reporting can be too high, a person doesn't nessecarily need to know about every single "notice". And a script certainly shouldn't die when encountering a notice, only an error. This is what I'm reffering to when I mean having it set to high. Sorry about the confusion. Daniel Tryba <news_comp.lang.php@canopus.nl> wrote in message news:<bnjsab$gdc$1@news.tue.nl>... > Steve <gr82meetu78@yahoo.com> wrote: > > Turn down error reporting. > > You probably have it set WAY to high. > > Error reporting _can't_ be to high :) > > > if($_POST['VariableName']){ > > $x = $_POST['VariableName']; > > } > > This will generate a warning if the index doens't exist, since the code > aboce tries to access the index. > > Correct example would be: > > if(array_key_exists('VariableName',$_POST)) > { > $x=$_POST['VariableName']; > } |