Atb.: Extra Data in POST response

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> fwrite($fx,("decoded_args:" . var_dump($decoded_args) . "\n"));

var_dump does not return any value, it outputs.

Janis


No: John Iliffe <john.iliffe@xxxxxxxxx>
Nosūtīts: pirmdiena, 2023. gada 13. februāris 00:29
Kam: php-general@xxxxxxxxxxxxx <php-general@xxxxxxxxxxxxx>
Tēma: Re: Extra Data in POST response
 
On Sun, 2023-02-12 at 21:15 +0000, Ashley Sheridan wrote:
> On 12/02/2023 19:49, John Iliffe wrote:
> > I seem to be getting extraneous data in the response sent to the browser by PHP.
> >
> > The server query is an AJAX request in json format which is received and
> > processed correctly and the result is sent using the "echo" function.  I have
> > checked and the area where the response is constructed is empty when processing
> > starts.
> >
> > The data received by the browser has BOTH the request json data followed by the
> > correct response in the received string (result not HTML, just a string that
> > will eventually be converted to json but so far will only be caught in the
> > browser for display).
> >
> > I'm using XMLHttpObject in the browser and I haven't been able to find a way to
> > separate the request from the response.  Before I construct an elegant
> > workaround does POST normally return the request?  If not, what am I doing
> > wrong?  So far I have always used GET so I know it doesn't return the request.
> >
> > For completeness, here is what the server says it is sending to the browser
> > (using echo) (The json may be wrong; I'm still just trying to receive it).
> >
> > ------
> > sent to browser:
> > "jres":[{"item_id":"5","i_type":"OL","i_name":"Student Success
> > Pages","i_stock":"99924","i_price":"14.00","i_books_box":"0"},{"item_id":"13","i
> > _type":"CD","i_name":"Basic Instructors Guide 9th
> > Ed.","i_stock":"9983","i_price":"19.95","i_books_box":"0"},{"item_id":"20","i_ty
> > pe":"SG","i_name":"Canadian Amateur Radio Basic Qualification Study
> > Guide","i_stock":"274","i_price":"44.95","i_books_box":"20"},{"item_id":"21","i_
> > type":"SG","i_name":"Canadian Amateur Radio Advanced Qualification Study
> > Guide","i_stock":"1586","i_price":"49.95","i_books_box":"16"}]
> > -----
> >
> > here is the original request from the browser, as decoded by server
> > ------
> > request method: POST
> > requests: array (
> >    'req' => '{"func":"act","item":"438"}',
> > )
> >
> > json last error message: No error
> > decoded_args:
> >     func=act
> >     item=438
> >
> > -------
> >
> > and here is what the browser says it received back:
> >
> > -----
> >   received:-->array(2) {
> >    ["func"]=>
> >    string(3) "act"
> >    ["item"]=>
> >    string(3) "438"
> > }
> >
> > "jres":[{"item_id":"5","i_type":"OL","i_name":"Student Success
> > Pages","i_stock":"99924","i_price":"14.00","i_books_box":"0"},{"item_id":"13","i
> > _type":"CD","i_name":"Basic Instructors Guide 9th
> > Ed.","i_stock":"9983","i_price":"19.95","i_books_box":"0"},{"item_id":"20","i_ty
> > pe":"SG","i_name":"Canadian Amateur Radio Basic Qualification Study
> > Guide","i_stock":"274","i_price":"44.95","i_books_box":"20"},{"item_id":"21","i_
> > type":"SG","i_name":"Canadian Amateur Radio Advanced Qualification Study
> > Guide","i_stock":"1586","i_price":"49.95","i_books_box":"16"}]<--
> > -----
> >
> > Thanks in advance for any suggestions you can give.
> >
> > John
> > ======
>
> Hi John,
>
> What does your PHP code look like? Something in it must be outputting
> the request, as that's not something that would just occur all by itself.
>
> Regards,
> Ash

Thanks for the quick response Ash.  That's what I have been asking myself and I
wondered if returning the query is a requirement of the POST protocol.  Without
sending the entire script, I can say that there are only two "echo" statements
in it (by doing a grep for echo) and they are mutually exclusive:

-----
[John@prod04 test]$ grep -n echo ./XXXXXXb.php
140:   echo $json_resp;
145:   echo "<br />XXXXXb: Either func or item was not set<br />err is true,
errno=" . $errno . "<br />";
-----
<?php
ob_start();
/* XXXXXXb responds to AJAX inventory report requests
 * from updated XXXXXXXX.
 *
 * Written J Iliffe 2/2023
 * copyright John Iliffe, 2023, all rights reserved.
 */
//header('Content-Type: application/json; charset=utf-8');
header('Content-Type: text/plain; charset=utf-8');
$func = "";
$item = 0;
$err = false;
$errno = 0;

        $fx = fopen("/tmp/XXXXXb.txt","w");

        fwrite($fx,("\nrequest method: " . $_SERVER['REQUEST_METHOD']));
        fwrite($fx,("\nrequests: " . var_export($_REQUEST,true)));

/* get the json request block as associative array */
if (isset($_POST['req']))
  {
   $decoded_args = json_decode($_POST['req'],true,5,JSON_INVALID_UTF8_IGNORE);

   if ((isset($decoded_args['func'])) && (isset($decoded_args['item'])))
     {
      $func = $decoded_args['func'];
      $item = $decoded_args['item'];
             fwrite($fx,("\nfunc and item isset as " . $func . " and " . $item .
"\n"));
     }
   else
     {
      $err = true;
      $errno = 2;
     }

        fwrite($fx,("\njson last error message: " . json_last_error_msg() .
"\n"));
        fwrite($fx,("decoded_args:" . var_dump($decoded_args) . "\n"));
        fwrite($fx,("func=" . $decoded_args['func'] . "\n"));
        fwrite($fx,("item=" . $decoded_args['item'] . "\n"));
  }
else
  {
   $err = true;
   $errno = 1;
  }

/* if we are good to go here, open database, create query */
$json_resp = '"jres":[';        //json opening

   ----I deleted the database activity from here---

if (! $err)
  {      
   if (! $res)
        {
         $err=true;
         $errno = 4;
        }
      else
        {
         for ($i=0;$i<$rows;$i++)   
          {
            $r_arr = pg_fetch_array($res,null,PGSQL_ASSOC);

            $json_rec=json_encode($r_arr,0,16);

                if ($json_rec === false)
                  {
                   fwrite ($fx,("\nrecord at " . $i . " bad, " .
json_last_error_msg() . "\n"));
                  }


            if (($i < $rows) && ($i != 0))      // no opening or trailing ','
              {
               $json_resp .= ',';
              }

            $json_resp .= $json_rec;
           }

         $json_resp .= ']';

fwrite($fx,("\njson sent to browser:\n   -->" . $json_resp . "<---\n\n\n"));
        }
     }
  }

if (! $err)
  {
   echo $json_resp;
   ob_flush();
  }
else
  {
   echo "<br />adm9005b: Either func or item was not set<br />err is true,
errno=" . $errno . "<br />";
   ob_flush();
  }

exit;
?>
------

This is pretty much what I have so far and why it has been driving me nuts for
several days now...the query is never repeated anywhere in the echoed output.
What actually comes back to the browser, as you can see above, is is the result
generated by this; prepended with PHP's analysis of the original POST query.

Thanks for taking the time to look.

John
======

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux