By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,719 Members | 1,445 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,719 IT Pros & Developers. It's quick & easy.

Unidentified Index Problem on some servers

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

Jul 17 '05 #2

P: n/a
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" <Ne*********@nodomain.com> wrote in message news:<bn**********@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

Jul 17 '05 #3

P: n/a
Steve <gr*********@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

Jul 17 '05 #4

P: n/a
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 <ne****************@canopus.nl> wrote in message news:<bn**********@news.tue.nl>...
Steve <gr*********@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'];
}

Jul 17 '05 #5

P: n/a
Hi Steve!

On 27 Oct 2003 20:07:44 -0800, gr*********@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 <ne****************@canopus.nl> wrote in message news:<bn**********@news.tue.nl>...
Steve <gr*********@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/
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.