|
Receiving Non ASCII Characters from Input Forms
Part:
1
2
3
4
5
6
7
(Continued from previous part...)
3. From step "C3" to "C4", Internet will maintain the URL encoded input strings as is.
4. From step "C4" to "C5", Web server will maintain the URL encoded input strings as is.
5. From step "C5" to "C6", PHP CGI interface is doing something interesting for you:
- $_SERVER['QUERY_STRING'] stores the URL encoded input strings as is, if input is submitted
with the GET method.
- If input is submitted with the POST method, the URL encoded input strings will be maintained
in the input stream.
- PHP parses input strings out of $_SERVER['QUERY_STRING'] or input stream into an array called
$_REQUEST. During this parsing process, URL decoding is applied. All input strings are converted
back to how they are entered on the page.
6. What do you want to do with the characters in the input data is your decision. You could
output them back to the HTML document, or store them in a file. Of course, you can apply any conversion
you want to.
In the sections below, I will show you some sample PHP scripts to validate those rules.
Receiving Non ASCII Characters with GET Method
We know that there are two methods you can use to submit input data in a HTML form: GET and POST.
Let's work with GET method first. I wrote the following PHP script to demonstrate how non ASCII characters
are managed in the steps described in the previous section.
<?php # InputIsoGet.php
# Copyright (c) 2005 by Dr. Herong Yang, http://www.herongyang.com/
#
#- Promoting CGI values to local variables
global $r_English, $r_Spanish, $r_Korean, $r_ChineseUtf8;
global $r_ChineseGb2312;
import_request_variables("GPC","r_");
#- Generating HTML document
print("<html>");
print('<meta http-equiv="Content-Type"'
.' content="text/html; charset=iso-8859-1"/>');
print("<body>\n");
print("<form action=InputIsoGet.php method=get>");
print("English ASCII: <input name=English"
." value='$r_English' size=16><br>\n");
print("Spanish UTF-8: <input name=Spanish"
." value='$r_Spanish' size=16><br>\n");
print("Korean UTF-8: <input name=Korean"
." value='$r_Korean' size=16><br>\n");
print("Chinese UTF-8: <input name=ChineseUtf8"
." value='$r_ChineseUtf8' size=16><br>\n");
print("Chinese GB2312: <input name=ChineseGb2312"
." value='$r_ChineseGb2312' size=16><br>\n");
print("<input type=submit name=submit value=Submit>\n");
print("</form>\n");
#- Outputing input strings back to HTML document
print("<hr>");
print("<pre>");
foreach ($_GET as $k => $v) {
print "$k = ($v)\n";
}
print("</pre>");
print("</body>");
print("</html>");
#- Dumping input strings to a file
$file = fopen("\\temp\\InputIsoGet.txt", 'ab');
$str = "------\n";
fwrite($file, $str, strlen($str));
if (array_key_exists('QUERY_STRING',$_SERVER)) {
$str = $_SERVER['QUERY_STRING'];
} else {
$str = NULL;
}
fwrite($file, $str, strlen($str));
$str = "------\n";
fwrite($file, $str, strlen($str));
foreach ($_REQUEST as $k => $v) {
$str = "$k = ($v)\n";
fwrite($file, $str, strlen($str));
}
fclose($file);
?>
(Continued on next part...)
Part:
1
2
3
4
5
6
7
|