<p>If I pass PHP variables with <code>.</code> in their names via $_GET PHP auto-replaces them with <code>_</code> characters. For example:</p>

<pre><code>&lt;?php
echo "url is ".$_SERVER['REQUEST_URI']."&lt;p&gt;";
echo "x.y is ".$_GET['x.y'].".&lt;p&gt;";
echo "x_y is ".$_GET['x_y'].".&lt;p&gt;";
</code></pre>

<p>... outputs the following:</p>

<pre><code>url is /SpShipTool/php/testGetUrl.php?x.y=a.b
x.y is .
x_y is a.b.
</code></pre>

<p>... my question is this: is there <strong>any</strong> way I can get this to stop? Cannot for the life of me figure out what I've done to deserve this</p>

<p>PHP version I'm running with is 5.2.4-2ubuntu5.3.</p>

[{'answer_id': 68710, 'author': 'Jeremy Privett', 'author_id': 560, 'author_profile': 'https://Stackoverflow.com/users/560', 'pm_score': 2, 'selected': False, 'text': "<p>The reason this happens is because of PHP's old register_globals functionality. The . character is not a valid character in a variable name, so PHP coverts it to an underscore in order to make sure there's compatibility.</p>\n\n<p>In short, it's not a good practice to do periods in URL variables.</p>\n"}, {'answer_id': 68742, 'author': 'Paige Ruten', 'author_id': 813, 'author_profile': 'https://Stackoverflow.com/users/813', 'pm_score': 7, 'selected': True, 'text': '<p>Here\'s PHP.net\'s explanation of why it does it:</p>\n<blockquote>\n<h3>Dots in incoming variable names</h3>\n<p>Typically, PHP does not alter the\nnames of variables when they are\npassed into a script. However, it\nshould be noted that the dot (period,\nfull stop) is not a valid character in\na PHP variable name. For the reason,\nlook at it:</p>\n<pre><code>&lt;?php\n$varname.ext;  /* invalid variable name */\n?&gt;\n</code></pre>\n<p>Now, what\nthe parser sees is a variable named\n$varname, followed by the string\nconcatenation operator, followed by\nthe barestring (i.e. unquoted string\nwhich doesn\'t match any known key or\nreserved words) \'ext\'. Obviously, this\ndoesn\'t have the intended result.</p>\n<p>For this reason, it is important to\nnote that PHP will automatically\nreplace any dots in incoming variable\nnames with underscores.</p>\n</blockquote>\n<p>That\'s from <a href="http://ca.php.net/variables.external" rel="noreferrer">http://ca.php.net/variables.external</a>.</p>\n<p>Also, according to <a href="http://ca.php.net/manual/en/language.variables.external.php#81080" rel="noreferrer">this comment</a> these other characters are converted to underscores:</p>\n<blockquote>\n<p>The full list of field-name characters that PHP converts to _ (underscore) is the following (not just dot):</p>\n<ul>\n<li>chr(32) ( ) (space)</li>\n<li>chr(46) (.) (dot)</li>\n<li>chr(91) ([) (open square bracket)</li>\n<li>chr(128) - chr(159) (various)</li>\n</ul>\n</blockquote>\n<p>So it looks like you\'re stuck with it, so you\'ll have to convert the underscores back to dots in your script using <a href="https://stackoverflow.com/questions/68651/can-i-get-php-to-stop-replacing-characters-in-get-or-post-arrays#68667">dawnerd\'s suggestion</a> (I\'d just use <a href="http://php.net/str_replace" rel="noreferrer">str_replace</a> though.)</p>\n'}, {'answer_id': 1939911, 'author': 'crb', 'author_id': 51691, 'author_profile': 'https://Stackoverflow.com/users/51691', 'pm_score': 6, 'selected': False, 'text': '<p>Long-since answered question, but there is actually a better answer (or work-around). PHP lets you at the <a href="http://php.net/manual/en/wrappers.php.php" rel="noreferrer">raw input stream</a>, so you can do something like this:</p>\n\n<pre><code>$query_string = file_get_contents(\'php://input\');\n</code></pre>\n\n<p>which will give you the $_POST array in query string format, periods as they should be. </p>\n\n<p>You can then parse it if you need (as per <a href="http://www.php.net/manual/en/language.variables.external.php#94607" rel="noreferrer">POSTer\'s comment</a>)</p>\n\n<pre><code>&lt;?php\n// Function to fix up PHP\'s messing up input containing dots, etc.\n// `$source` can be either \'POST\' or \'GET\'\nfunction getRealInput($source) {\n    $pairs = explode("&amp;", $source == \'POST\' ? file_get_contents("php://input") : $_SERVER[\'QUERY_STRING\']);\n    $vars = array();\n    foreach ($pairs as $pair) {\n        $nv = explode("=", $pair);\n        $name = urldecode($nv[0]);\n        $value = urldecode($nv[1]);\n        $vars[$name] = $value;\n    }\n    return $vars;\n}\n\n// Wrapper functions specifically for GET and POST:\nfunction getRealGET() { return getRealInput(\'GET\'); }\nfunction getRealPOST() { return getRealInput(\'POST\'); }\n?&gt;\n</code></pre>\n\n<p>Hugely useful for OpenID parameters, which contain both \'.\' and \'_\', each with a certain meaning!</p>\n'}, {'answer_id': 4927461, 'author': 'Jason', 'author_id': 607256, 'author_profile': 'https://Stackoverflow.com/users/607256', 'pm_score': 2, 'selected': False, 'text': '<p>My solution to this problem was quick and dirty, but I still like it. I simply wanted to post a list of filenames that were checked on the form. I used <code>base64_encode</code> to encode the filenames within the markup and then just decoded it with <code>base64_decode</code> prior to using them.</p>\n'}, {'answer_id': 14432765, 'author': 'Ja͢ck', 'author_id': 1338292, 'author_profile': 'https://Stackoverflow.com/users/1338292', 'pm_score': 3, 'selected': False, 'text': '<p>This happens because a period is an invalid character in a variable\'s name, the <a href="http://php.net/variables.external#language.variables.external.dot-in-names" rel="nofollow">reason</a> for which lies very deep in the implementation of PHP, so there are no easy fixes (yet). </p>\n\n<p>In the meantime you can work around this issue by:</p>\n\n<ol>\n<li>Accessing the raw query data via either <code>php://input</code> for POST data or <code>$_SERVER[\'QUERY_STRING\']</code> for GET data</li>\n<li>Using a conversion function.</li>\n</ol>\n\n<p>The below conversion function (PHP >= 5.4) encodes the names of each key-value pair into a hexadecimal representation and then performs a regular <code>parse_str()</code>; once done, it reverts the hexadecimal names back into their original form:</p>\n\n<pre><code>function parse_qs($data)\n{\n    $data = preg_replace_callback(\'/(?:^|(?&lt;=&amp;))[^=[]+/\', function($match) {\n        return bin2hex(urldecode($match[0]));\n    }, $data);\n\n    parse_str($data, $values);\n\n    return array_combine(array_map(\'hex2bin\', array_keys($values)), $values);\n}\n\n// work with the raw query string\n$data = parse_qs($_SERVER[\'QUERY_STRING\']);\n</code></pre>\n\n<p>Or:</p>\n\n<pre><code>// handle posted data (this only works with application/x-www-form-urlencoded)\n$data = parse_qs(file_get_contents(\'php://input\'));\n</code></pre>\n'}, {'answer_id': 18028493, 'author': 'El Yobo', 'author_id': 217588, 'author_profile': 'https://Stackoverflow.com/users/217588', 'pm_score': 2, 'selected': False, 'text': '<p>After looking at Rok\'s solution I have come up with a version which addresses the limitations in my answer below, crb\'s above and Rok\'s solution as well.  See a <a href="/questions/68651/get-php-to-stop-replacing-characters-in-get-or-post-arrays/18163411#18163411">my improved version</a>.</p>\n\n<hr>\n\n<p>@crb\'s answer <a href="/questions/68651/get-php-to-stop-replacing-characters-in-get-or-post-arrays#1939911">above</a> is a good start, but there are a couple of problems.</p>\n\n<ul>\n<li>It reprocesses everything, which is overkill; only those fields that have a "." in the name need to be reprocessed.</li>\n<li>It fails to handle arrays in the same way that native PHP processing does, e.g. for keys like "foo.bar[]".</li>\n</ul>\n\n<p>The solution below addresses both of these problems now (note that it has been updated since originally posted).  This is about 50% faster than my answer above in my testing, but will not handle situations where the data has the same key (or a key which gets extracted the same, e.g. foo.bar and foo_bar are both extracted as foo_bar).</p>\n\n<pre><code>&lt;?php\n\npublic function fix2(&amp;$target, $source, $keep = false) {                       \n    if (!$source) {                                                            \n        return;                                                                \n    }                                                                          \n    preg_match_all(                                                            \n        \'/                                                                     \n        # Match at start of string or &amp;                                        \n        (?:^|(?&lt;=&amp;))                                                           \n        # Exclude cases where the period is in brackets, e.g. foo[bar.blarg]\n        [^=&amp;\\[]*                                                               \n        # Affected cases: periods and spaces                                   \n        (?:\\.|%20)                                                             \n        # Keep matching until assignment, next variable, end of string or   \n        # start of an array                                                    \n        [^=&amp;\\[]*                                                               \n        /x\',                                                                   \n        $source,                                                               \n        $matches                                                               \n    );                                                                         \n\n    foreach (current($matches) as $key) {                                      \n        $key    = urldecode($key);                                             \n        $badKey = preg_replace(\'/(\\.| )/\', \'_\', $key);                         \n\n        if (isset($target[$badKey])) {                                         \n            // Duplicate values may have already unset this                    \n            $target[$key] = $target[$badKey];                                  \n\n            if (!$keep) {                                                      \n                unset($target[$badKey]);                                       \n            }                                                                  \n        }                                                                      \n    }                                                                          \n}                                                                              \n</code></pre>\n'}, {'answer_id': 18163411, 'author': 'El Yobo', 'author_id': 217588, 'author_profile': 'https://Stackoverflow.com/users/217588', 'pm_score': 3, 'selected': False, 'text': '<p>This approach is an altered version of Rok Kralj\'s, but with some tweaking to work, to improve efficiency (avoids unnecessary callbacks, encoding and decoding on unaffected keys) and to correctly handle array keys.</p>\n\n<p>A <a href="https://gist.github.com/elyobo/6200838" rel="noreferrer">gist with tests</a> is available and any feedback or suggestions are welcome here or there.</p>\n\n<pre><code>public function fix(&amp;$target, $source, $keep = false) {                        \n    if (!$source) {                                                            \n        return;                                                                \n    }                                                                          \n    $keys = array();                                                           \n\n    $source = preg_replace_callback(                                           \n        \'/                                                                     \n        # Match at start of string or &amp;                                        \n        (?:^|(?&lt;=&amp;))                                                           \n        # Exclude cases where the period is in brackets, e.g. foo[bar.blarg]\n        [^=&amp;\\[]*                                                               \n        # Affected cases: periods and spaces                                   \n        (?:\\.|%20)                                                             \n        # Keep matching until assignment, next variable, end of string or   \n        # start of an array                                                    \n        [^=&amp;\\[]*                                                               \n        /x\',                                                                   \n        function ($key) use (&amp;$keys) {                                         \n            $keys[] = $key = base64_encode(urldecode($key[0]));                \n            return urlencode($key);                                            \n        },                                                                     \n    $source                                                                    \n    );                                                                         \n\n    if (!$keep) {                                                              \n        $target = array();                                                     \n    }                                                                          \n\n    parse_str($source, $data);                                                 \n    foreach ($data as $key =&gt; $val) {                                          \n        // Only unprocess encoded keys                                      \n        if (!in_array($key, $keys)) {                                          \n            $target[$key] = $val;                                              \n            continue;                                                          \n        }                                                                      \n\n        $key = base64_decode($key);                                            \n        $target[$key] = $val;                                                  \n\n        if ($keep) {                                                           \n            // Keep a copy in the underscore key version                       \n            $key = preg_replace(\'/(\\.| )/\', \'_\', $key);                        \n            $target[$key] = $val;                                              \n        }                                                                      \n    }                                                                          \n}                                                                              \n</code></pre>\n'}, {'answer_id': 18209799, 'author': 'Rok Kralj', 'author_id': 924109, 'author_profile': 'https://Stackoverflow.com/users/924109', 'pm_score': 4, 'selected': False, 'text': "<p>Do you want a solution that is <strong>standards compliant,</strong> and works with <strong>deep arrays</strong> (for example: <code>?param[2][5]=10</code>) ?</p>\n<p>To fix all possible sources of this problem, you can apply at the very top of your PHP code:</p>\n<pre><code>$_GET    = fix( $_SERVER['QUERY_STRING'] );\n$_POST   = fix( file_get_contents('php://input') );\n$_COOKIE = fix( $_SERVER['HTTP_COOKIE'] );\n</code></pre>\n<p>The working of this function is a neat idea that I came up during my summer vacation of 2013. Do not be discouraged by a simple regex, it just grabs all query names, encodes them (so dots are preserved), and then uses a normal <code>parse_str()</code> function.</p>\n<pre><code>function fix($source) {\n    $source = preg_replace_callback(\n        '/(^|(?&lt;=&amp;))[^=[&amp;]+/',\n        function($key) { return bin2hex(urldecode($key[0])); },\n        $source\n    );\n\n    parse_str($source, $post);\n    \n    $result = array();\n    foreach ($post as $key =&gt; $val) {\n        $result[hex2bin($key)] = $val;\n    }\n    return $result;\n}\n</code></pre>\n"}, {'answer_id': 18244502, 'author': 'humbletim', 'author_id': 1684079, 'author_profile': 'https://Stackoverflow.com/users/1684079', 'pm_score': 2, 'selected': False, 'text': '<p>If looking for <strong>any</strong> way to  <em>literally</em> get PHP to stop replacing \'.\' characters in $_GET or $_POST arrays, then one such way is to modify PHP\'s source (and in this case it is relatively straightforward).</p>\n\n<p><em>WARNING: Modifying PHP C source is an advanced option!</em></p>\n\n<p>Also see this <a href="https://bugs.php.net/bug.php?id=45272" rel="nofollow">PHP bug report</a> which suggests the same modification.</p>\n\n<p>To explore you\'ll need to:</p>\n\n<ul>\n<li>download <a href="http://www.php.net/downloads.php" rel="nofollow">PHP\'s C source code</a></li>\n<li>disable the <code>.</code> replacement check</li>\n<li><em>./configure</em>, <em>make</em> and deploy your customized build of PHP</li>\n</ul>\n\n<p>The source change itself is trivial and involves updating just <a href="https://github.com/php/php-src/blob/f01c295d/main/php_variables.c#L94" rel="nofollow">one half of one line</a> in <code>main/php_variables.c</code>:</p>\n\n<pre class="lang-c prettyprint-override"><code>....\n/* ensure that we don\'t have spaces or dots in the variable name (not binary safe) */\nfor (p = var; *p; p++) {\n    if (*p == \' \' /*|| *p == \'.\'*/) {\n        *p=\'_\';\n....\n</code></pre>\n\n<p><em>Note: compared to original <code>|| *p == \'.\'</code>  has been commented-out</em></p>\n\n<hr>\n\n<p>Example Output:</p>\n\n<p>given a QUERY_STRING of <code>a.a[]=bb&amp;a.a[]=BB&amp;c%20c=dd</code>,\nrunning <code>&lt;?php print_r($_GET);</code> now produces:</p>\n\n<pre>\nArray\n(\n    [a.a] => Array\n        (\n            [0] => bb\n            [1] => BB\n        )\n\n    [c_c] => dd\n)\n</pre>\n\n<p><em>Notes:</p>\n\n<ul>\n<li>this patch addresses the original question only (it stops replacement of dots, not spaces).</li>\n<li>running on this patch will be faster than script-level solutions, but those pure-.php answers are still generally-preferable (because they avoid changing PHP itself).</li>\n<li>in theory a polyfill approach is possible here and could combine approaches -- test for the C-level change using <code>parse_str()</code> and (if unavailable) fall-back to slower methods.\n</em></li>\n</ul>\n'}, {'answer_id': 20365198, 'author': 'scipilot', 'author_id': 209288, 'author_profile': 'https://Stackoverflow.com/users/209288', 'pm_score': 5, 'selected': False, 'text': '<p>Highlighting an actual answer by Johan in a comment above - I just wrapped my entire post in a top-level array which completely bypasses the problem with no heavy processing required.</p>\n\n<p>In the form you do </p>\n\n<pre><code>&lt;input name="data[database.username]"&gt;  \n&lt;input name="data[database.password]"&gt;  \n&lt;input name="data[something.else.really.deep]"&gt;  \n</code></pre>\n\n<p>instead of </p>\n\n<pre><code>&lt;input name="database.username"&gt; \n&lt;input name="database.password"&gt; \n&lt;input name="something.else.really.deep"&gt;  \n</code></pre>\n\n<p>and in the post handler, just unwrap it:</p>\n\n<pre><code>$posdata = $_POST[\'data\'];\n</code></pre>\n\n<p>For me this was a two-line change, as my views were entirely templated.</p>\n\n<p>FYI. I am using dots in my field names to edit trees of grouped data.</p>\n'}, {'answer_id': 25799378, 'author': 'ChrisNY', 'author_id': 420274, 'author_profile': 'https://Stackoverflow.com/users/420274', 'pm_score': 0, 'selected': False, 'text': '<p>Well, the function I include below, "getRealPostArray()", isn\'t a pretty solution, but it handles arrays and supports both names: "alpha_beta" and "alpha.beta":</p>\n\n<pre><code>  &lt;input type=\'text\' value=\'First-.\' name=\'alpha.beta[a.b][]\' /&gt;&lt;br&gt;\n  &lt;input type=\'text\' value=\'Second-.\' name=\'alpha.beta[a.b][]\' /&gt;&lt;br&gt;\n  &lt;input type=\'text\' value=\'First-_\' name=\'alpha_beta[a.b][]\' /&gt;&lt;br&gt;\n  &lt;input type=\'text\' value=\'Second-_\' name=\'alpha_beta[a.b][]\' /&gt;&lt;br&gt;\n</code></pre>\n\n<p>whereas var_dump($_POST) produces:</p>\n\n<pre><code>  \'alpha_beta\' =&gt; \n    array (size=1)\n      \'a.b\' =&gt; \n        array (size=4)\n          0 =&gt; string \'First-.\' (length=7)\n          1 =&gt; string \'Second-.\' (length=8)\n          2 =&gt; string \'First-_\' (length=7)\n          3 =&gt; string \'Second-_\' (length=8)\n</code></pre>\n\n<p>var_dump( getRealPostArray()) produces:</p>\n\n<pre><code>  \'alpha.beta\' =&gt; \n    array (size=1)\n      \'a.b\' =&gt; \n        array (size=2)\n          0 =&gt; string \'First-.\' (length=7)\n          1 =&gt; string \'Second-.\' (length=8)\n  \'alpha_beta\' =&gt; \n    array (size=1)\n      \'a.b\' =&gt; \n        array (size=2)\n          0 =&gt; string \'First-_\' (length=7)\n          1 =&gt; string \'Second-_\' (length=8)\n</code></pre>\n\n<p>The function, for what it\'s worth:</p>\n\n<pre><code>function getRealPostArray() {\n  if ($_SERVER[\'REQUEST_METHOD\'] !== \'POST\') {#Nothing to do\n      return null;\n  }\n  $neverANamePart = \'~#~\'; #Any arbitrary string never expected in a \'name\'\n  $postdata = file_get_contents("php://input");\n  $post = [];\n  $rebuiltpairs = [];\n  $postraws = explode(\'&amp;\', $postdata);\n  foreach ($postraws as $postraw) { #Each is a string like: \'xxxx=yyyy\'\n    $keyvalpair = explode(\'=\',$postraw);\n    if (empty($keyvalpair[1])) {\n      $keyvalpair[1] = \'\';\n    }\n    $pos = strpos($keyvalpair[0],\'%5B\');\n    if ($pos !== false) {\n      $str1 = substr($keyvalpair[0], 0, $pos);\n      $str2 = substr($keyvalpair[0], $pos);\n      $str1 = str_replace(\'.\',$neverANamePart,$str1);\n      $keyvalpair[0] = $str1.$str2;\n    } else {\n      $keyvalpair[0] = str_replace(\'.\',$neverANamePart,$keyvalpair[0]);\n    }\n    $rebuiltpair = implode(\'=\',$keyvalpair);\n    $rebuiltpairs[]=$rebuiltpair;\n  }\n  $rebuiltpostdata = implode(\'&amp;\',$rebuiltpairs);\n  parse_str($rebuiltpostdata, $post);\n  $fixedpost = [];\n  foreach ($post as $key =&gt; $val) {\n    $fixedpost[str_replace($neverANamePart,\'.\',$key)] = $val;\n  }\n  return $fixedpost;\n}\n</code></pre>\n'}, {'answer_id': 26206877, 'author': 'John', 'author_id': 606371, 'author_profile': 'https://Stackoverflow.com/users/606371', 'pm_score': 0, 'selected': False, 'text': "<p>Using crb's I wanted to recreate the <code>$_POST</code> array as a whole though keep in mind you'll still have to ensure you're encoding and decoding correctly both at the client and the server. It's important to understand when a character is <em>truly</em> invalid and it is truly <em>valid</em>. Additionally people should <em>still</em> and <em>always</em> escape client data before using it with <em>any</em> database command <em>without exception</em>.</p>\n\n<pre><code>&lt;?php\nunset($_POST);\n$_POST = array();\n$p0 = explode('&amp;',file_get_contents('php://input'));\nforeach ($p0 as $key =&gt; $value)\n{\n $p1 = explode('=',$value);\n $_POST[$p1[0]] = $p1[1];\n //OR...\n //$_POST[urldecode($p1[0])] = urldecode($p1[1]);\n}\nprint_r($_POST);\n?&gt;\n</code></pre>\n\n<p>I recommend using this only for individual cases only, offhand I'm not sure about the negative points of putting this at the top of your primary header file.</p>\n"}, {'answer_id': 33735559, 'author': 'sasha-ch', 'author_id': 4541523, 'author_profile': 'https://Stackoverflow.com/users/4541523', 'pm_score': 0, 'selected': False, 'text': "<p>My current solution (based on prev topic replies):</p>\n\n<pre><code>function parseQueryString($data)\n{\n    $data = rawurldecode($data);   \n    $pattern = '/(?:^|(?&lt;=&amp;))[^=&amp;\\[]*[^=&amp;\\[]*/';       \n    $data = preg_replace_callback($pattern, function ($match){\n        return bin2hex(urldecode($match[0]));\n    }, $data);\n    parse_str($data, $values);\n\n    return array_combine(array_map('hex2bin', array_keys($values)), $values);\n}\n\n$_GET = parseQueryString($_SERVER['QUERY_STRING']);\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/68651', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']