Encoding query string for storing in SESSION

I’m in trouble because the method I use to encode a query string works on one remote server but not on another at the same ISP. I don’t understand why, but I need to resolve it.

On one site the query string is encoded once, and remains fixed. On the other it is repeatedly encoded, so it ‘grows’. e.g. ‘[’ first becomes ‘%5B’ then ‘%255B’, then ‘%25255B’ and so on. When the string is recovered from the SESSION and used to repeat the search, the result is rubbish, of course.

An obvious reason for this to happen would be if the uploaded files were different. I’ve checked this many times.

Before I dive into code, can anyone tell me if it’s actually possible for ‘php.ini’ or other server settings to affect the way ‘htmlentities’ and similar functions work. I would not have thought so, yet that’s what appears to be happening in this case.

http://_test.loc/content/test.php?et[0]=ALL&loc[14]=SW&save=eatg&eatg=Search

echo ‘<a href="’.$_SERVER[‘PHP_SELF’].‘?’.$_SERVER[‘QUERY_STRING’].‘">link</a>’;

Works fine. Sorry that I cannot help you any further, but I cannot reproduce the problem.

Thank you. I think that will be helpful shortly. I’ve decided the only thing to do is to build myself a simple test script, shorn of all the complicating calls to MySQL and images, etc. Then I can try out all manner of methods in parallel, and see which produces the most consistent results.

Thank you. Yes, $_SERVER[‘QUERY_STRING’] does contain what I want,
“et%5B0%5D=ALL&loc%5B14%5D=SW&save=eatg&eatg=Search”
and I can save it to the SESSION.

My question was aimed at finding out how to retrieve it from the session into a variable called (for example) $get, and decode it so it can be added to a ‘Location’ statement as in:-

header("Location: /?$get");

which has to be the equivalent of:-

header("Location: /?et[0]=ALL&loc[14]=SW&save=eatg&eatg=Search");

I’ve tried several ways, not all successful, with various combinations of ‘urldecode’, ‘html_entity_decode’ etc.
Even stranger I found one way that worked fine on one web site but not at another hosted at the same ISP. In that case the ‘%’ were being repeatedly re-coded, so for example the ‘[’ of the query grew from ‘%5B’ to ‘%255B’ then to ‘%25255B’ with each iteration. I could find no difference in the PHP scripts, so tentatively conclude that the PHP environments must be different, but could this really be so ?

Perhaps it would be wise to use a loop to repeatedly decode any stored query variable to ensure there are no entities left before use ?

Thank you. If referring to $_GET as containing a ‘query string’ is technically incorrect, I hope my meaning was clear.
The $_SERVER array provides

[HTTP_REFERER] => ...et%5B0%5D=ALL&loc%5B14%5D=SW&save=eatg&eatg=Search

which exactly what I’ve saved as $_SESSION[‘get’]

What is the best way of retrieving it as a variable $get so I can use it as in

header("Location: /?$get");

and return to the original search.

That is the serialized version of the array, $_GET doesn’t contain a string, it is an array representation of the request string.

Take a look at the $_SERVER array for the request string.

You shouldn’t be using html entities to begin with. The solution is really really easy. Do not encode the query string at all until output. Just store it raw in the session array.

Thank you for your reply. I had tried doing that, but it doesn’t seem a good route for me.

A typical $_GET query string is

"et[0]=ALL&loc[14]=SW&save=eatg&eatg=Search"

If I save it directly to the SESSION with

$_SESSION['get'] = $_GET

I have a SESSION variable

get|a:4:{s:2:"et";a:1:{i:0;s:3:"ALL";}s:3:"loc";a:1:{i:14;s:2:"SW";}s:4:"save";s:4:"eatg";s:4:"eatg";s:6:"Search";}

which is a nightmare ! No doubt PHP thinks the query string is an array in it’s own right. How would I go about converting that back to a string in a variable ‘$get’ that I could use in:-

header("Location: /?$get");

My problem remains that what’s working on one site isn’t working on another at the same ISP and probably on the same server, and the difference lies in the repeated encoding of the SESSION variables.

var_dump($_SERVER[‘QUERY_STRING’])

That should contain what you want. The query string is just that, a string. PHP parses it into an array ($_GET or $_POST) for ease of access.