$request problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe Blow

    $request problem

    Can anybody see a problem with this? Am I being stupid?

    if (!isset($_REQUE ST["input"]) || "" == $_REQUEST["input"])
    {
    throw new Exception("need query");
    }
    else
    {
    echo "<p>Search words: $_REQUEST[input]";
    }
    /*
    * split input into array of query words
    */
    $queries = array_map(mysql _real_escape_st ring, explode(" ",
    $_REQUEST["input"]));


    This works on one server, running PHP5, but not another. Both apache.
    I've checked php.ini but cannot see why this seemingly simple bit of
    code should fail in one instance.

    In the one where it fails to work $queries is set to "" where I would
    expect it to be set to an array of the values of $_REQUEST["input"],
    split at each space.

    No errors are reported even with error_reporting (E_ALL);

    $_REQUEST is definitely set as the echo line prints out the input as
    expected.

  • Jerry Stuckle

    #2
    Re: $request problem

    Joe Blow wrote:[color=blue]
    > Can anybody see a problem with this? Am I being stupid?
    >
    > if (!isset($_REQUE ST["input"]) || "" == $_REQUEST["input"])
    > {
    > throw new Exception("need query");
    > }
    > else
    > {
    > echo "<p>Search words: $_REQUEST[input]";
    > }
    > /*
    > * split input into array of query words
    > */
    > $queries = array_map(mysql _real_escape_st ring, explode(" ",
    > $_REQUEST["input"]));
    >
    >
    > This works on one server, running PHP5, but not another. Both apache.
    > I've checked php.ini but cannot see why this seemingly simple bit of
    > code should fail in one instance.
    >
    > In the one where it fails to work $queries is set to "" where I would
    > expect it to be set to an array of the values of $_REQUEST["input"],
    > split at each space.
    >
    > No errors are reported even with error_reporting (E_ALL);
    >
    > $_REQUEST is definitely set as the echo line prints out the input as
    > expected.
    >[/color]

    Maybe your problem is:

    echo "<p>Search words: $_REQUEST['input']";
    ^ ^

    P.S. I prefer to use single quotes around constants like this.


    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Norman Peelman

      #3
      Re: $request problem

      "Jerry Stuckle" <jstucklex@attg lobal.net> wrote in message
      news:15edndHF8f D3jpDZ4p2dnA@co mcast.com...[color=blue]
      > Joe Blow wrote:[color=green]
      > > Can anybody see a problem with this? Am I being stupid?
      > >
      > > if (!isset($_REQUE ST["input"]) || "" == $_REQUEST["input"])
      > > {
      > > throw new Exception("need query");
      > > }
      > > else
      > > {
      > > echo "<p>Search words: $_REQUEST[input]";
      > > }
      > > /*
      > > * split input into array of query words
      > > */
      > > $queries = array_map(mysql _real_escape_st ring, explode(" ",
      > > $_REQUEST["input"]));
      > >
      > >
      > > This works on one server, running PHP5, but not another. Both apache.
      > > I've checked php.ini but cannot see why this seemingly simple bit of
      > > code should fail in one instance.
      > >
      > > In the one where it fails to work $queries is set to "" where I would
      > > expect it to be set to an array of the values of $_REQUEST["input"],
      > > split at each space.
      > >
      > > No errors are reported even with error_reporting (E_ALL);
      > >
      > > $_REQUEST is definitely set as the echo line prints out the input as
      > > expected.
      > >[/color]
      >
      > Maybe your problem is:
      >
      > echo "<p>Search words: $_REQUEST['input']";
      > ^ ^
      >
      > P.S. I prefer to use single quotes around constants like this.
      >[/color]

      No,

      echo "<p>Search words: $_REQUEST[input]";

      is (one of) the correct ways. Single quotes aren't needed/don't work when
      the variable is enclosed in double quotes. Unless you enclose the variable
      in curly braces:

      echo "<p>Search words: {$_REQUEST['input']}";

      will work properly too...

      Norm


      Comment

      • Richard Levasseur

        #4
        Re: $request problem

        Not quotes a string index generates an E_NOTICE or something, i forget
        which exactly, normally error reporting isn't set to show it. PHP
        tries to resolve it as a define()'d constant, then treats it as a
        quoted string. Not quoting the string index is generally bad form.

        Single quotes provide a small performance gain as php doesn't try to
        interpret the string.

        For the question at hand:
        I'm fairly sure that the callback has to be quoted, when using array
        map, that may be the problem. Why are you individually escaping each
        'word' of the input? Wouldn't it be easier to just apply it to the
        entire $_REQUEST['input'] string?

        $queries = array_map( 'mysql_real_esc ape_string', explode(" ",
        $_REQUEST["input"]));

        Comment

        • Jerry Stuckle

          #5
          Re: $request problem

          Norman Peelman wrote:[color=blue]
          > "Jerry Stuckle" <jstucklex@attg lobal.net> wrote in message
          > news:15edndHF8f D3jpDZ4p2dnA@co mcast.com...
          >[color=green]
          >>Joe Blow wrote:
          >>[color=darkred]
          >>>Can anybody see a problem with this? Am I being stupid?
          >>>
          >>>if (!isset($_REQUE ST["input"]) || "" == $_REQUEST["input"])
          >>> {
          >>>throw new Exception("need query");
          >>> }
          >>> else
          >>> {
          >>> echo "<p>Search words: $_REQUEST[input]";
          >>> }
          >>>/*
          >>>* split input into array of query words
          >>>*/
          >>> $queries = array_map(mysql _real_escape_st ring, explode(" ",
          >>>$_REQUEST["input"]));
          >>>
          >>>
          >>>This works on one server, running PHP5, but not another. Both apache.
          >>>I've checked php.ini but cannot see why this seemingly simple bit of
          >>>code should fail in one instance.
          >>>
          >>>In the one where it fails to work $queries is set to "" where I would
          >>>expect it to be set to an array of the values of $_REQUEST["input"],
          >>>split at each space.
          >>>
          >>>No errors are reported even with error_reporting (E_ALL);
          >>>
          >>>$_REQUEST is definitely set as the echo line prints out the input as
          >>>expected.
          >>>[/color]
          >>
          >>Maybe your problem is:
          >>
          >> echo "<p>Search words: $_REQUEST['input']";
          >> ^ ^
          >>
          >>P.S. I prefer to use single quotes around constants like this.
          >>[/color]
          >
          >
          > No,
          >
          > echo "<p>Search words: $_REQUEST[input]";
          >
          > is (one of) the correct ways. Single quotes aren't needed/don't work when
          > the variable is enclosed in double quotes. Unless you enclose the variable
          > in curly braces:
          >
          > echo "<p>Search words: {$_REQUEST['input']}";
          >
          > will work properly too...
          >
          > Norm
          >
          >[/color]

          Yes, if you turn on all errors you will get an E_NOTICE because the
          string is not quoted.


          --
          =============== ===
          Remove the "x" from my email address
          Jerry Stuckle
          JDS Computer Training Corp.
          jstucklex@attgl obal.net
          =============== ===

          Comment

          • ColdShine

            #6
            Re: $request problem

            Jerry Stuckle in news:WvadnQQMO-tzxI_ZRVn-gw@comcast.com wrote:
            [color=blue]
            > Norman Peelman wrote:
            >[color=green]
            >> echo "<p>Search words: $_REQUEST[input]";
            >>
            >> is (one of) the correct ways. Single quotes aren't needed/don't work when
            >> the variable is enclosed in double quotes. Unless you enclose the
            >> variable in curly braces:
            >>
            >> echo "<p>Search words: {$_REQUEST['input']}";
            >>
            >> will work properly too...[/color]
            >
            > Yes, if you turn on all errors you will get an E_NOTICE because the
            > string is not quoted.[/color]

            No E_NOTICE at all.



            There's no clear example stating this, but:

            echo "$array[key]";

            Is EXACTLY the same as:

            echo "{$array['key']}";

            With no notices being generated (I know since I always debug using E_ALL).
            It's probably faster too, since I guess { "activates" a more complex parser.

            Of course, that's the same as:

            echo $array['key'];

            --
            ColdShine

            "Experience is a hard teacher: she gives the test first, the lesson
            afterwards." - Vernon Sanders law


            Comment

            • Anonymous

              #7
              Re: $request problem

              ColdShine wrote:
              [color=blue]
              > There's no clear example stating this, but:
              >
              > echo "$array[key]";
              >
              > Is EXACTLY the same as:
              >
              > echo "{$array['key']}";[/color]

              No, Jerry is right, they are not the same.

              $array[key] is an array with a constant called key as index,
              $array['key'] is an array with the string 'key' as index. You will see
              the difference as soon as you assign a value to the constant key. :-)

              When using $array[key] PHP will try to find the constant key, but
              intelligently will assume you actually meant 'key' when it figures out
              that the constant is undefined. Here is the error from the log:

              [Sat Mar 11 19:29:40 2006] [error] [client 127.0.0.1] PHP Notice: Use
              of undefined constant key - assumed 'key' in C:\\webroot\\te st.php on
              line 4

              The PHP manual says on this topic:


              Note: Enabling E_NOTICE during development has some benefits. For
              debugging purposes: NOTICE messages will warn you about possible bugs in
              your code. For example, use of unassigned values is warned. It is
              extremely useful to find typos and to save time for debugging. NOTICE
              messages will warn you about bad style. For example, $arr[item] is
              better to be written as $arr['item'] since PHP tries to treat "item" as
              constant. If it is not a constant, PHP assumes it is a string index for
              the array.



              However, strangely enough this warning showed up in the log only the
              first time I accessed the page! No more warnings on any subsequent
              accesses to the page or any other page with that kind of error!

              I just checked the config, ignore_repeated _errors and
              ignore_repeated _source are set to off. I also confirmed these settings
              with phpinfo(). However, PHP reacts as if both are set to on! Did I just
              find a bug?

              That would explain why you get no warnings! You probably got your first
              and only warning a long time ago if you always program like that. ;-)

              P.S.: I use PHP 4.4.2 which is the latest released version 4 available.
              Can anyone confirm that erroneous logging behaviour of this PHP version?

              Comment

              • Norman Peelman

                #8
                Re: $request problem

                "Anonymous" <anonymous@nowh ere.invalid> wrote in message
                news:441326E4.6 6903A5B@nowhere .invalid...[color=blue]
                > ColdShine wrote:
                >[color=green]
                > > There's no clear example stating this, but:
                > >
                > > echo "$array[key]";
                > >
                > > Is EXACTLY the same as:
                > >
                > > echo "{$array['key']}";[/color]
                >
                > No, Jerry is right, they are not the same.
                >
                > $array[key] is an array with a constant called key as index,
                > $array['key'] is an array with the string 'key' as index. You will see
                > the difference as soon as you assign a value to the constant key. :-)
                >
                > When using $array[key] PHP will try to find the constant key, but
                > intelligently will assume you actually meant 'key' when it figures out
                > that the constant is undefined. Here is the error from the log:
                >
                > [Sat Mar 11 19:29:40 2006] [error] [client 127.0.0.1] PHP Notice: Use
                > of undefined constant key - assumed 'key' in C:\\webroot\\te st.php on
                > line 4
                >
                > The PHP manual says on this topic:
                >
                >
                > Note: Enabling E_NOTICE during development has some benefits. For
                > debugging purposes: NOTICE messages will warn you about possible bugs in
                > your code. For example, use of unassigned values is warned. It is
                > extremely useful to find typos and to save time for debugging. NOTICE
                > messages will warn you about bad style. For example, $arr[item] is
                > better to be written as $arr['item'] since PHP tries to treat "item" as
                > constant. If it is not a constant, PHP assumes it is a string index for
                > the array.
                >
                >
                >
                > However, strangely enough this warning showed up in the log only the
                > first time I accessed the page! No more warnings on any subsequent
                > accesses to the page or any other page with that kind of error!
                >
                > I just checked the config, ignore_repeated _errors and
                > ignore_repeated _source are set to off. I also confirmed these settings
                > with phpinfo(). However, PHP reacts as if both are set to on! Did I just
                > find a bug?
                >
                > That would explain why you get no warnings! You probably got your first
                > and only warning a long time ago if you always program like that. ;-)
                >
                > P.S.: I use PHP 4.4.2 which is the latest released version 4 available.
                > Can anyone confirm that erroneous logging behaviour of this PHP version?[/color]

                Consider this small piece of code:
                ---

                <?php
                error_reporting (E_ALL);

                define('one','I am the defined one');

                $str['one'] = 'I am the one!';

                echo "<br>$str[one]";
                echo '<br>'.one;
                echo "<br>{$str['one']}";
                ?>
                ---

                ....output is exactly this:

                I am the one!
                I am the defined one!
                I am the one!


                .... no conflicts at all, no NOTICES, ERRORS, etc. The thing to wrap your
                head around is that in a wierd technical way the associative index is
                considered quoted as long as the entire variable is within double-quotes to
                begin with. I personally prefer this way to multiple concatinations. ..

                Norm



                Comment

                • Richard Levasseur

                  #9
                  Re: $request problem

                  define('key', 'foobar', false);
                  $a = array('key'=>'v alue');
                  echo "$a[key]<br>";
                  echo "{$a[key]}<br>";
                  echo "{$a['key']}<br>";

                  output:
                  value
                  Errno: 8, Undefined index: foobar
                  value

                  It would seem constants are only expanded if they're in {}, and are
                  treated like strings otherwise. I wonder when this was changed, or if
                  its unintentional?

                  Comment

                  • Norman Peelman

                    #10
                    Re: $request problem

                    "Richard Levasseur" <richardlev@gma il.com> wrote in message
                    news:1142199015 .476842.320290@ i40g2000cwc.goo glegroups.com.. .[color=blue]
                    > define('key', 'foobar', false);
                    > $a = array('key'=>'v alue');
                    > echo "$a[key]<br>";
                    > echo "{$a[key]}<br>";
                    > echo "{$a['key']}<br>";
                    >
                    > output:
                    > value
                    > Errno: 8, Undefined index: foobar
                    > value
                    >
                    > It would seem constants are only expanded if they're in {}, and are
                    > treated like strings otherwise. I wonder when this was changed, or if
                    > its unintentional?
                    >[/color]

                    Interesting enough that in all actuality, you don't need to use curly
                    braces unless you are dealing with multi-dimensional arrays...

                    Norm


                    --
                    FREE Avatar hosting at www.easyavatar.com


                    Comment

                    • Richard Levasseur

                      #11
                      Re: $request problem

                      Or back in the day when PHP gave E_NOTICE errors and you cared ;)

                      Comment

                      • ColdShine

                        #12
                        Re: $request problem

                        Anonymous in news:441326E4.6 6903A5B@nowhere .invalid wrote:
                        [color=blue]
                        > ColdShine wrote:
                        >[color=green]
                        >> There's no clear example stating this, but:
                        >>
                        >> echo "$array[key]";
                        >>
                        >> Is EXACTLY the same as:
                        >>
                        >> echo "{$array['key']}";[/color]
                        >
                        > No, Jerry is right, they are not the same.
                        >
                        > $array[key] is an array with a constant called key as index,
                        > $array['key'] is an array with the string 'key' as index.[/color]

                        Please read my post twice. I'm not talking about using $arr[key], but I'm
                        talking 'bout using "$arr[key]". If you can't understand the difference, you
                        should not be correcting me.

                        [color=blue]
                        > You will see the difference as soon as you assign a value to the
                        > constant key. :-)[/color]

                        No way. The constant called key is ONLY used when referring to $arr[key]
                        from OUTSIDE a doublequote'd string, or when using curly syntax.

                        [color=blue]
                        > When using $array[key] PHP will try to find the constant key, but
                        > intelligently will assume you actually meant 'key' when it figures out
                        > that the constant is undefined. Here is the error from the log:
                        >
                        > [Sat Mar 11 19:29:40 2006] [error] [client 127.0.0.1] PHP Notice: Use
                        > of undefined constant key - assumed 'key' in C:\\webroot\\te st.php on
                        > line 4
                        >
                        > The PHP manual says on this topic:
                        >
                        >
                        > Note: Enabling E_NOTICE during development has some benefits. For
                        > debugging purposes: NOTICE messages will warn you about possible bugs in
                        > your code. For example, use of unassigned values is warned. It is
                        > extremely useful to find typos and to save time for debugging. NOTICE
                        > messages will warn you about bad style. For example, $arr[item] is
                        > better to be written as $arr['item'] since PHP tries to treat "item" as
                        > constant. If it is not a constant, PHP assumes it is a string index for
                        > the array.[/color]

                        Both you and the PHP manual are STILL talking about $arr[item] being used
                        OUTSIDE a complex (doublequote'd) string (or again, with curly syntax).

                        [color=blue]
                        > However, strangely enough this warning showed up in the log only the
                        > first time I accessed the page! No more warnings on any subsequent
                        > accesses to the page or any other page with that kind of error!
                        >
                        > I just checked the config, ignore_repeated _errors and
                        > ignore_repeated _source are set to off. I also confirmed these settings
                        > with phpinfo(). However, PHP reacts as if both are set to on! Did I just
                        > find a bug?
                        >
                        > That would explain why you get no warnings! You probably got your first
                        > and only warning a long time ago if you always program like that. ;-)[/color]

                        If you write $arr[key] you get an E_NOTICE unless there's a constant named
                        'key'.
                        If you write "$arr[key]" you get an E_NOTICE unless 'key' is a defined index
                        in $arr.
                        I don't think there's anything complex to understand here. And no, I didn't
                        turn on such an idiot-friendly option as ignoring
                        warnings/errors/whatsoever.

                        Here's a quick roundup, if you (or anyone f'wing this thread) still are
                        wondering WHAT is meant WHEN:

                        <?php
                        $arr = array('key' => 'foo');
                        define('key', 'another');

                        echo $arr[key]; // undefined index ('another')
                        echo $arr['key']; // foo
                        echo '$arr[key]'; // $arr[key]
                        echo "$arr[key]"; // foo
                        echo "$arr['key']"; // parse error
                        echo "{$arr[key]}"; // undefined index ('another')
                        echo "{$arr['key']}"; // foo
                        ?>

                        This should rule out any misunderstandin gs.

                        Btw,
                        <?php
                        $arr = array('c' => 'Cheers', 'f' => 'F**k off');
                        define('c', 'f');

                        echo "$arr[c] :-)";
                        ?>
                        --
                        ColdShine

                        "Experience is a hard teacher: she gives the test first, the lesson
                        afterwards." - Vernon Sanders law


                        Comment

                        • Erwin Moller

                          #13
                          Re: $request problem

                          ColdShine wrote:

                          <snip>

                          I liked this discussion, and it cleared a few thing up I never understood,
                          and thus simply avoided in all my code.
                          Your examples are a nice summary. :-)

                          I couldn't resist myself to give some comment.
                          [color=blue]
                          >
                          > Here's a quick roundup, if you (or anyone f'wing this thread) still are
                          > wondering WHAT is meant WHEN:
                          >
                          > <?php
                          > $arr = array('key' => 'foo');
                          > define('key', 'another');
                          >
                          > echo $arr[key]; // undefined index ('another')
                          > echo $arr['key']; // foo
                          > echo '$arr[key]'; // $arr[key]
                          > echo "$arr[key]"; // foo[/color]

                          Personally I think that it is a big mistake of PHP to let this construct
                          slip. :-/
                          Why try to be nice and 'correct' this internally to
                          $arr["key"] ??
                          Why not to defined key?
                          Sounds like a great source of bugs to me.

                          Why oh why not just produce an error?

                          These constructs are excactly why I walked away from Perl some years ago.
                          ;-)


                          Another thing: Try to be nice to yourself and fellowprogramme rs, and just
                          jump out of the string and concatenate what you need.

                          What's wrong with:
                          $result = "bla".$arr[key]." and of course: ".$arr["key"];

                          Why obfuscate code with complex parameters inside (double) quotes?
                          What is the point? Outsmart fellow programmers? If so: go Perl. ;-)

                          I think it is just unneccessary, and I never came across a situation where
                          it is actually needed...

                          Well, just my 2 cents.
                          And thanks for the examples.
                          I finally got it, but will surely never use it. :-)

                          Regards,
                          Erwin Moller
                          [color=blue]
                          > echo "$arr['key']"; // parse error
                          > echo "{$arr[key]}"; // undefined index ('another')
                          > echo "{$arr['key']}"; // foo
                          > ?>
                          >
                          > This should rule out any misunderstandin gs.
                          >
                          > Btw,
                          > <?php
                          > $arr = array('c' => 'Cheers', 'f' => 'F**k off');
                          > define('c', 'f');
                          >
                          > echo "$arr[c] :-)";
                          > ?>[/color]

                          Comment

                          • Anonymous

                            #14
                            Re: $request problem

                            ColdShine wrote:
                            [color=blue]
                            > Here's a quick roundup, if you (or anyone f'wing this thread) still are
                            > wondering WHAT is meant WHEN:
                            >
                            > <?php
                            > $arr = array('key' => 'foo');
                            > define('key', 'another');
                            >
                            > echo $arr[key]; // undefined index ('another')
                            > echo $arr['key']; // foo
                            > echo '$arr[key]'; // $arr[key][/color]

                            Ok, these results are obvious, IMHO.
                            [color=blue]
                            > echo "$arr[key]"; // foo
                            > echo "$arr['key']"; // parse error[/color]

                            But these two are not. For the first case: Where in the manual does it
                            state that "$arr[key]" should be evaluated like $arr["key"]? For the
                            second: Within double quotes variables should be evaluated, so why
                            doesn't $arr['key'] evaluate to foo?
                            [color=blue]
                            > echo "{$arr[key]}"; // undefined index ('another')
                            > echo "{$arr['key']}"; // foo
                            > ?>[/color]

                            Jep, these are obvious, too.
                            [color=blue]
                            >
                            > This should rule out any misunderstandin gs.[/color]

                            Yes, it did. But it also brought up two new questions on my part. :-)

                            Comment

                            • ColdShine

                              #15
                              Re: $request problem

                              Anonymous in news:4416CEBE.9 2F38532@nowhere .invalid wrote:
                              [color=blue]
                              > ColdShine wrote:
                              >[color=green]
                              >> Here's a quick roundup, if you (or anyone f'wing this thread) still are
                              >> wondering WHAT is meant WHEN:
                              >>
                              >> <?php
                              >> $arr = array('key' => 'foo');
                              >> define('key', 'another');
                              >>
                              >> echo $arr[key]; // undefined index ('another')
                              >> echo $arr['key']; // foo
                              >> echo '$arr[key]'; // $arr[key][/color]
                              >
                              > Ok, these results are obvious, IMHO.[/color]

                              I included those for the sake of completeness...

                              [color=blue][color=green]
                              >> echo "$arr[key]"; // foo
                              >> echo "$arr['key']"; // parse error[/color]
                              >
                              > But these two are not. For the first case: Where in the manual does it
                              > state that "$arr[key]" should be evaluated like $arr["key"]? For the
                              > second: Within double quotes variables should be evaluated, so why
                              > doesn't $arr['key'] evaluate to foo?[/color]

                              This answers your questions:


                              If you could not find this in the manual it's your fault, as it's just
                              where... you should expect it to be :)

                              [color=blue][color=green]
                              >> This should rule out any misunderstandin gs.[/color]
                              >
                              > Yes, it did. But it also brought up two new questions on my part. :-)[/color]

                              ....and anyone else's who's not read the manual of course.

                              --
                              ColdShine

                              "Experience is a hard teacher: she gives the test first, the lesson
                              afterwards." - Vernon Sanders law


                              Comment

                              Working...