PHP converts form field characters to underscores



I'm currently writing a system which contains relatively arbitrary
form field names (the field names are re-used as headings in a
subsequent form-to-mail kind of script). I've noticed that PHP
converts certain form field-name characters to underscores.

After doing some searching for related bugs, these are the only
bugs.php.net entries I could find:
http://bugs.php.net/bug.php?id=34578
http://bugs.php.net/bug.php?id=42677
http://bugs.php.net/bug.php?id=17574

The first two responses cite the "because of register globals, any
form field which does not contain valid variable-name characters is
converted" whereas the last one points out where this really
contradicts itself, PHP accepts form field names with all manner of
non-printable characters _including_ form field names which start with
numbers. The following code writes a form to test which characters are
acceptable to PHP:

---------------------------
<html>
<head><title>test</title></head>
<body>
<form method="post">
<?php

for ($i=1; $i<255; $i++) {
print "<input type=\"hidden\" name=\"&#$i;\" />\n";
}

?>
<input type="Submit" />
</form>
<?php

if (count($_POST)) {
for ($i=1; $i<255; $i++) {
if (!isset($_POST[chr($i)])) {
print $i.' ('.dechex($i).')(&#'.$i.';) doesnt exist<br />';
}
}
}

?>
</body>
</html>
---------------------------

The sticking point for me is that space characters (%20) as well as
dot characters (%2E) are converted to underscores, yet all other ASCII
characters (with the exception of square brackets which at least have
some explainable meaning) are left as-is. I lose data because I can't
tell which fields had spaces, dots or underscores in them originally.
The only solution I can think of is replacing dots or spaces with non-
printable characters and then converting them back when I parse the
$_REQUEST array.

Can anyone shed light on why normal (and very useful) characters are
converted whereas others are left alone?
.



Relevant Pages

  • Re: PHP converts form field characters to underscores
    ... I've noticed that PHP ... converts certain form field-name characters to underscores. ... form field which does not contain valid variable-name characters is ... printable characters and then converting them back when I parse the ...
    (comp.lang.php)
  • Re: template with editable and non-editable areas
    ... A form field will accept far more than a row of characters. ... Please read Dian Chapman's articles, ...
    (microsoft.public.word.pagelayout)
  • Re: TextBox limit
    ... limit the number of characters, but users will be entering a mix of capital ... and lower case letters, so it is hard to know what to set the max length to. ... that's not possible with a text form field. ... text box controls in a UserForm allow you to examine their contents on ...
    (microsoft.public.word.vba.general)
  • Re: TextBox limit
    ... I am talking about a TextBox on a UserForm. ... that's not possible with a text form field. ... from pressing Enter or otherwise inserting more characters, ...
    (microsoft.public.word.vba.general)
  • Re: Count remaining characters in a form field
    ... I was wondering if it's possible to display the number of characters ... remaining in a form field. ... It certainly can be done in a text box in a UserForm (just write a ... Make a macro that ...
    (microsoft.public.word.vba.general)