| [main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql] | |
Note that all external links will open up in a separate window. This is a stripped down version of these pages for older browsers. These pages are really meant to be viewed in a standards compliant browser. |
Form Data ProcessingThese tutorials are about PHP and its use for server-side Web programming. PHP Form DataWhen you are using PHP to process information provided by the user, you are normally doing form processing. Thus, a discussion of how to retrieve data from the user is really about how to process form data submitted by the client to the server. To do this we make use one or more of the following four global arrays:
Since file processing is an advanced topic, using the We will stick with using the short forms for the array names. The Basics
The basic concept is really simple. Data returned from the client using the POST method are stored in the The names of the elements in the arrays directly corresponds to the field names in the form and/or query string. For instance: <form method="POST" action="myscript.php?flag=yes"> <p> <input type="text" name="field1" /> Sample field </p> [...]
The above coding snippet would generate a
To retrieve a value, you just check the array element in Tips and TricksThere are numerous little bits of information that go with the task of processing form data. You can improve your coding by knowing what they are. The following is a list of tips and tricks of for processing form data. They are not presented in any particular order. General TipsForms do not return fields that don't have values, so blank fields with no default values will not be returned to the server. Thus PHP will have to account for the fact that not every form field may be accounted for in the reply from the client.
For checkboxes, if a value has not been provided in the HTML code, will return a value of "
For compatibility with all systems, it is recommended that you write "GET" and "POST" in upper-case, thus
If, for some reason, you don't know the requesting method the client is using, it is stored by name in Data validation in PHP is more robust than data validation in JavaScript. Data validation on the server is in general more robust than client-side data validation. You should always revalidate form data on the server. CLient-side validation is just to avoid unecessary calls to the server. GET requests are cached when received by the client. A call to the same resource with the same query string may return the cached version instead of rechecking the server. If using a GET method with extremely time sensitive data,you should make sure that caching is turned off for the page. Here are the PHP commands to write out HTTP headers to tell the browser not to cache:
// Set past expiration date
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// Define mod date to indicate page is modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// HTTP 1.1 cache commands
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
// HTTP 1.0 cache commands
header("Pragma: no-cache");
You can also use the following HTML meta tag to set a past expiration date. <meta http-equiv="expires" content="Sun, 01 Dec 2001 12:00:00 EST" /> Resource requests using POST are always refreshed against the server. They are not cached. You should always use the POST method if the result data may change when the form data does not change. Form Field NamesIf the name of the form filed contains characters that are illegal characters for the array index key names, the characters will be replaced with underscores.You should code your forms to not have illegal characters in the field names.
If a form field can have multiple values, such as a selection field with
To submit multiple values from a single field name to PHP end the field name with square brackets. To avoid URL encoding problems, using the POST method is preferred for this. PHP will recognize that the values are a set of value and create an array to store them. The value of the
<form method="POST" action="myscript.php?flag=yes">
<p>
<input type="checkbox" name="field1[]" value="1" />
Sample 1
<br />
<input type="checkbox" name="field1[]" value="2" />
Sample 2
<br />
<input type="checkbox" name="field1[]" value="3" />
Sample 3
</p>
[...]
The above form fields will generate an array that will be stored in $field1Arr = $_POST["field1"]; $firstVal = $field1Var[0]; Practicing Safe Data ProcessingWhen working with user supplied data, you need to be aware of the fact that you are opening yourself up to attacks from malicious users, and, to be most honest, mistakes by users who are not the brightest bulb in the room. You need to be able to code for this. Coding for unsers who don't know what they are doing is a simple matter of thinking through the process carefully, providing clear directions, and testing everything to make sure the directions are followed. This is a guiding principle of good software design. Unfortunately, this is not the place for a detailed looked at best practices in GUI interface design. It is really a field of study in and of itself. So, for now, let us say, make sure forms are clear, comprehensible and easy to use, and test everything coming back from the client for every possible mistake you can think of. Then ask other people to think of more possible mistakes to code for, Instead we are going to look at how to reduce attacks on your system, both intentional and accidental, instigated through HTML forms processing code. The biggest hazard involves posting files to the server, which we discuss elsewhere. For now we will focus on two issues:
Bad DataIt is possible for a user to send bad data through form fields. Properly conceived, this can be used to get confidential information back from the server or hack into the system.
The first step to avoiding this is to make sure that
We can get rid of most commands in content being sent to the server simply by getting rid of the angle brackets, since a PHP command would need to begin with
If you are working with shell commands, there is a It is, of course, preferable to not let users execute any commands on your system, but this is not always possible. Don't be afraid to test user data for every eventuality and to restrict it to clearly defined sets of values. Both of these make your security job easier. For instance, the search strings on these pages identify where the page is and what it is called, but rather than directly addressing the page, they are used to look the page up in an array. The array tells where to get the page. This keeps someone from being able to spoof the search string and request documents I don't want them to have. Form Field SpoofingUsers, with some clever coding, can send data to your server which spoof a form submission from the page but contain field names that are not part of the form. This may seem pointless, but can be used to override system variables and thus allow them to hack into the server. For instance, by getting the server to echo out the password file.
The simplest solution to this is to make sure that
When it comes right down to it, the only reason for having an automated process for registering globals is laziness. It is not worth the security risk, so don't enable
If you do have a better reason and do need to enable
Note that server variables are last, preventing them from being overwritten by user data. However, the user could still use cookie data to spoof posted data, thus causing the server to think that the fiel they had uploaded was actually your password file or somesuch (and could you please echo back the contents to make sure it posted correctly). Since anything not in the list will not be processed, you can enhance security by inly registering those you need, or only system variables, using the global arrays for the rest. Thuse we could change it tovariables_order="ES" and maintain a reasonable level of security, though we would still need to use the long method to access user data.
Even with
$formField1 = '';
foreach ($_POST as $key => $value) {
if (isset(${$key}) {
${$key} = $value;
}
}
These pages can be found at:
[http://academ.hvcc.edu/~kantopet/]
|