Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

http_build_query() dot is converted to underscore

Tags:

php

Please check the below array:

Array([bunrey] => Array ([0] => 20130730181908615391000000)
      [mt.shasta] => Array ( 
        [0] => 20130708203742347410000000
        [1] => 20130213201456984069000000
        [2] => 20130712144459481348000000
      )
      [shingletwon] => Array
      (
        [0] => 20130801233842122771000000
      )
)

I want to send this array as query string using http_build_query(), I got the below string after using http_build_query():

bunrey%5B0%5D=20130730181908615391000000&mt.shasta%5B0%5D=20130708203742347410000000&mt.shasta%5B1%5D=20130213201456984069000000&mt.shasta%5B2%5D=20130712144459481348000000&shingletwon%5B0%5D=20130801233842122771000000

As you can see after sending this query string to some other file, there I am trying to retrieve. I had echoed the $_REQUEST object:

Array (
[bunrey] => Array
    (
        [0] => 20130730181908615391000000
    )

[mt_shasta] => Array
    (
        [0] => 20130708203742347410000000
        [1] => 20130213201456984069000000
        [2] => 20130712144459481348000000
    )
[shingletwon] => Array
    (
        [0] => 20130801233842122771000000
    )
)

please check one of the key mr.shasta had changed to mr_shasta. Can you people please provide any solution for this.

like image 924
user2653000 Avatar asked Aug 05 '13 12:08

user2653000


1 Answers

This is the standard PHP behaviour. Points are converted in underscores when used as array keys in a POST request.

From the documentation:

Dots and spaces in variable names are converted to underscores. For example < input name="a.b" /> becomes $_REQUEST["a_b"].

The only solution is: stop using spaces and/or dots in array keys when using them in POST requests or, else, operate a string replace on every array key your receive.

$post = array();
foreach ($_POST as $key => $value)
$post[str_replace("_", ".", $key)] = $value;

Note that the code above would fix only the problem of . (converted to _) but not spaces. Also, if you have any _ in your original key this would be converted to . as well (as pointed out in the comments).

As you can see, the only real solution is to avoid . and spaces in $_POST keys. They just can't be received, not with PHP (and not with other server-side solutions that I know of): you'll loose that information.

No, this is not a limitation or a crap feature: this is a programming guideline. If you're using array keys names for something more than what you would normally do with a variable name, you're most likely doing something conceptually wrong (and I've done it many times too).

Just to give you an example on how wrong is that: in some programming solutions like asp.net-mvc (and, I think, codeigniter too) POST/GET requests are supposed to be mapped over functions in what's called a "controller". Which means that if you send a POST which looks like ["myKey" => "myValue", "myOtherKey" => "someValue"] you should then have a function which takes keys as arguments.

function(String myKey, String myOtherKey){ }

PHP have no default "on-top" framework (that I know of) which do this: it allows you to access $_POST directly. Cool: but this toy can breake easely. Use it with caution.

like image 50
Saturnix Avatar answered Oct 17 '22 11:10

Saturnix