Unidentified Index Problem on some servers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neil Strong

    Unidentified Index Problem on some servers

    I'm getting an error message "Unidentifi ed 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


  • david

    #2
    Re: Unidentified Index Problem on some servers

    Neil Strong wrote:
    [color=blue]
    > I'm getting an error message "Unidentifi ed 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?[/color]

    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_handl er() 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_handle r:\n\terrno=$er rno\n\terrstr=$ errstr\n";
    echo "\terrfile=$err file\n\terrline =$errline\n";
    if ($errno & (E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR))
    die();
    }
    set_error_handl er("error_handl er");
    error_reporting (E_ALL);

    checks the others comments too...

    Regards

    --
    David

    Comment

    • Steve

      #3
      Re: Unidentified Index Problem on some servers

      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@no domain.com> wrote in message news:<bnit2q$3v v$1@visp.bt.co. uk>...[color=blue]
      > I'm getting an error message "Unidentifi ed 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[/color]

      Comment

      • Daniel Tryba

        #4
        Re: Unidentified Index Problem on some servers

        Steve <gr82meetu78@ya hoo.com> wrote:[color=blue]
        > Turn down error reporting.
        > You probably have it set WAY to high.[/color]

        Error reporting _can't_ be to high :)
        [color=blue]
        > if($_POST['VariableName']){
        > $x = $_POST['VariableName'];
        > }[/color]

        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_ex ists('VariableN ame',$_POST))
        {
        $x=$_POST['VariableName'];
        }

        --

        Daniel Tryba

        Comment

        • Steve

          #5
          Re: Unidentified Index Problem on some servers

          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$gd c$1@news.tue.nl >...[color=blue]
          > Steve <gr82meetu78@ya hoo.com> wrote:[color=green]
          > > Turn down error reporting.
          > > You probably have it set WAY to high.[/color]
          >
          > Error reporting _can't_ be to high :)
          >[color=green]
          > > if($_POST['VariableName']){
          > > $x = $_POST['VariableName'];
          > > }[/color]
          >
          > 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_ex ists('VariableN ame',$_POST))
          > {
          > $x=$_POST['VariableName'];
          > }[/color]

          Comment

          • Jochen Daum

            #6
            Re: Unidentified Index Problem on some servers

            Hi Steve!

            On 27 Oct 2003 20:07:44 -0800, gr82meetu78@yah oo.com (Steve) wrote:
            [color=blue]
            >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
            >configuratio n.
            >There is however a way to fix this in the php.ini file.[/color]

            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

            [color=blue]
            >
            >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$gd c$1@news.tue.nl >...[color=green]
            >> Steve <gr82meetu78@ya hoo.com> wrote:[color=darkred]
            >> > Turn down error reporting.
            >> > You probably have it set WAY to high.[/color]
            >>
            >> Error reporting _can't_ be to high :)
            >>[color=darkred]
            >> > if($_POST['VariableName']){
            >> > $x = $_POST['VariableName'];
            >> > }[/color]
            >>
            >> 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_ex ists('VariableN ame',$_POST))
            >> {
            >> $x=$_POST['VariableName'];
            >> }[/color][/color]

            --
            Jochen Daum - CANS Ltd.
            PHP DB Edit Toolkit -- PHP scripts for building
            database editing interfaces.
            Download PHP DB Edit Toolkit for free. PHP DB Edit Toolkit is a set of PHP classes makes the generation of database edit interfaces easier and faster. The main class builds tabular and form views based on a data dictionary and takes over handling of insert/update/delete and user input.

            Comment

            Working...