Can't get parameters from $_SERVER['QUERY_STRING']
Posted by: brycel (---.hfc.comcastbusiness.net)
Date: July 16, 2012 10:17PM

I have just installed WAMP server. I am trying to get the query string from $_SERVER['QUERY_STRING'], but it always is empty. I can get individual parameters using the $_REQUEST variable. I need to get the QUERY_STRING because some of the parameters can be the same and $_REQUEST won't handle that.

I also tried $_SERVER['REQUEST_URI'], but this gives me the application path, but with no parameters.

Is there a setting in php.ini or something that I am missing?

Options: ReplyQuote
Re: Can't get parameters from $_SERVER['QUERY_STRING']
Posted by: stevenmartin99 (Moderator)
Date: July 16, 2012 10:24PM

First of all - "Some of the parameters can be the same" is an AWFUL excuse to look for what you want.
Go and change this so they are not the same.

But you didnt mention that the values are GET....


Does it work ok when you use GET instead of REQUEST

Steven Martin
stevenmartin99@gmail.com
stevenmartin99@hotmail.com
PampServer.com - [pampserver.com]

Options: ReplyQuote
Re: Can't get parameters from $_SERVER['QUERY_STRING']
Posted by: RiggsFolly (---.ppp.as43234.net)
Date: July 17, 2012 05:09PM

If $_SERVER['QUERY_STRING'] is empty then you are POSTING your form data and the data will be in $_POST[''] variables.
<form action="form_action.asp" method="POST">
can be changed to
<form action="form_action.asp" method="GET">
to make the variables appear in the GET array


if you use an anchor type link to a page
<a href="page.php?a=1&b=2">click</a>
Then variables are passed on the query string and will be available in $_GET[''] variables always.

$_REQUEST is a merge of the $_POST and $_GET arrays and sometimes SERVER and ENV. It is better to use the _POST and _GET arrays as this self documents the code you write and some configurations of PHP do not fill the $_REQUEST array as this is a configurable feature of php,

SEE php.ini variable request_order = "GP"

Options: ReplyQuote
Re: Can't get parameters from $_SERVER['QUERY_STRING']
Posted by: brycel (---.hfc.comcastbusiness.net)
Date: July 17, 2012 08:22PM

I appreciate the feedback. I am admittedly new to php and WAMP.

I agree that having parameters with the same name is not considered good practice. The problem is that the client is a device (not a web browser) which I do not have direct control over. I may submit a request to to see if the device can be modified.

Essentially the device is trying to pass an array of data. I have read on other forums that it is not necessarily against any standards to send arrays this way, but again I don't know much about these things.

At least in my current state, I would just like to somehow get the full URL so the server can do what it needs with the array.

I have tried using $_GET, but get nothing. I have used $_POST, but only get the last item of the array. I have verified that request_order = "GP".

Options: ReplyQuote
Re: Can't get parameters from $_SERVER['QUERY_STRING']
Posted by: RiggsFolly (---.ppp.as43234.net)
Date: July 18, 2012 12:31AM

If it is passing an array of data do you mean its passing p[0],p[1],...,p[n]
If thats what you mean that is not described as lots of fields with the same name. Its an array.

Try adding this to the top of the form recieving the data. If you dont get it after that post the results of this code for us to have a look at.


if ( isset( $_POST) )
{
echo '<pre>POST ARRAY' . print_r($_POST, true) . echo '</pre>';
}
if ( isset( $_GET) )
{
echo '<pre>GET ARRAY' . print_r($_GET, true) . echo '</pre>';
}

Options: ReplyQuote
Re: Can't get parameters from $_SERVER['QUERY_STRING']
Posted by: brycel (---.hfc.comcastbusiness.net)
Date: July 18, 2012 10:16PM

I have tried that and only get the last element.

Here is an example which admittedly I should have posted before:
[192.168.105.12]

My php code:
if( isset($_POST) )
{
error_log('<pre>POST_ARRAY'.print_r($_POST,true).'</pre>');
}
if( isset($_GET) )
{
error_log('<pre>GET_ARRAY'.print_r($_GET,true).'</pre>');
}


Gives me:

[18-Jul-2012 18:28:52 UTC] <pre>GET_ARRAYArray
(
[deviceId] => 00040002
[val] => 33
)
which is only the last set of values.

I have spoken with the device vendor and they will try using the following post instead:
[192.168.105.12][]=00040002&val[]=36&deviceId[]=00040003&val[]=23&deviceId[]=00040002&val[]=33

I'll post here again when I have some results.

Thanks

Options: ReplyQuote
Re: Can't get parameters from $_SERVER['QUERY_STRING']
Posted by: brycel (---.hfc.comcastbusiness.net)
Date: July 18, 2012 10:17PM

My example post got changed to a link, so I am reposting it textually without the base
postEvent?deviceId=00040002&val=36&deviceId=00040003&val=23&deviceId=00040002&val=33

Options: ReplyQuote
Re: Can't get parameters from $_SERVER['QUERY_STRING']
Posted by: brycel (---.hfc.comcastbusiness.net)
Date: July 18, 2012 10:19PM

The vendor will be changing the post to:
postEvent?deviceId[]=00040002&val[]=36&deviceId[]=00040003&val[]=23&deviceId[]=00040002&val[]=33



Edited 1 time(s). Last edit at 07/18/2012 10:20PM by brycel.

Options: ReplyQuote
Re: Can't get parameters from $_SERVER['QUERY_STRING']
Posted by: RiggsFolly (---.ppp.as43234.net)
Date: July 19, 2012 02:08AM

The data in that querystring will be available to your php script in the $_GET array as 2 seperate arrays one called deviceId and the other val

_GET Array
(
[deviceId] => Array
(
[0] => 00040002
[1] => 00040003
[2] => 00040002
)

[val] => Array
(
[0] => 36
[1] => 23
[2] => 33
)

)


Here is a simple test script save it as arraytest.php in whatever directory your index.php script is in.

<?php

echo '<pre> _GET Array' . print_r( $_GET, TRUE ) . '</pre>';

// move the arrays into something more managable
$deviceId = $_GET['deviceId'];
$val = $_GET['val'];

// loop through the deviceId array and print it and the associated val data.
for ( $x=0; $x < count($deviceId); $x++ )
{
echo $deviceId[$x] . ' = ' . $val[$x] . '<br>';
}
?>


Then put this into your browser

arraytest.php?deviceId[]=00040002&val[]=36&deviceId[]=00040003&val[]=23&deviceId[]=00040002&val[]=33


And see what is printed.


Documentation
[uk.php.net]
[uk.php.net]
[php.net]

Options: ReplyQuote
Re: Can't get parameters from $_SERVER['QUERY_STRING']
Posted by: brycel (---.hfc.comcastbusiness.net)
Date: July 20, 2012 12:51AM

It works.

I put in the URL deviceId[]=00040002&val[]=36&deviceId[]=00040003&val[]=23&deviceId[]=00040002&val[]=33

and was indeed able to get an array for each parameter type.

The device engineers are now using this method and I am successfully able to receive all the data from the device.

Thank you for the help!

Options: ReplyQuote


Sorry, only registered users may post in this forum.