Processing data from the user is the other main topic of this
chapter. You should never trust the data coming from the browser, so
it's imperative to always validate all fields, even
hidden form elements. Validation takes many forms, from ensuring the
data match certain criteria, as discussed in Recipe 9.3, to escaping HTML entities to allow the safe
display of user entered data, as covered in Recipe 9.9. Furthermore, Recipe 9.8 tells how to protect the security of your web
server, and Recipe 9.7 covers how to process
files uploaded by a user.
When placing elements inside of $_REQUEST, if two
arrays both have a key with the same name, PHP falls back upon the
variables_order configuration directive. By
default, variables_order is
EGPCS (or GPCS, if
you're using the
php.ini-recommended configuration file). So, PHP
first adds environment variables to $_REQUEST and
then adds GET, POST, cookie, and web server variables to the array,
in this order. For instance, since C comes after
P in the default order, a cookie named
username overwrites a POST variable named
username.
If you don't have access to PHP's
configuration files, you can use ini_get( ) to
check a setting:
print ini_get('variables_order');
EGPCS
You may need to do this because your ISP doesn't let
you view configuration settings or because your script may run on
someone else's server. You can also use
phpinfo( ) to view settings. However, if you
can't rely on the value of
variables_order, you should directly access
$_GET and $_POST instead of
using $_REQUEST.
For simplicity, however, let's assume the value in
the variable is valid. (The term
"valid" is open for definition,
depending on certain criteria, such as not being empty, not being an
attempt to break into the system, etc.) This allows us to omit the
error checking stage, which is important but gets in the way of this
simple example. So, here is a simple hello.php
script to process the form:
echo 'Hello ' . $_POST['first_name'] . '!';
If the user's first name is Joe, PHP prints out:
Hello Joe!