The Building Blocks: Data Types, Literals, Variables, and Constants - Part 2/Page 5
[previous] [next]
The Building Blocks:
Data Types, Literals, Variables, and Constants - Part 2
Introduction to Form Variables
Now we are starting to get into what makes PHP so popular. As we mentioned in the introduction to this book, PHP was designed as a Web-based programming language to create dynamic Web content, to gather and manipulate information submitted by HTML forms. For now, because we are talking about variables, we will examine a simple form and how PHP collects and stores the form information in variables. Chapter 10, "More on PHP Forms," provides a comprehensive discussion on HTML forms and introduces the special global arrays used to process them in your PHP scripts.
The php.ini File and register_globals. Before getting started, there are some issues to be aware of based on the version of PHP you are using. The PHP initialization file, called php.ini
, contains a directive called register_globals
. Older versions of PHP (prior to 4.2.0) had this directive turned to "On" as the default, allowing PHP to create simple variables from form data. Since then, register_globals
has been set to "Off" to avoid potential security breaches. If using PHP 5, you will have to turn this feature on before PHP can directly assign form input to simple global variables, or the data must be extracted programmatically. We discuss both ways to do this in the following section. The next excerpt is taken from the PHP 5 php.ini
file, showing the line where register_globals
is set. The default is "Off" and you should really try to adhere to this setting.
From the php.ini
file:
; You should do your best to write your scripts so that they do not require
; register_globals to be on; Using form variables as globals can easily lead
; to possible security problems, if the code is not very well thought of.
register_globals = Off
If you do not set register_globals
to "On," add the following line to your PHP program:
The $_REQUEST
superglobal array contains the information submitted to the server from the HTML form. After extracting this information, PHP will create simple variables corresponding to the form data as shown in Example 4.24. In Chapter 10, "More on PHP Forms," all aspects of extracting form data are discussed in detail. For now, assume register_globals is set to "On."
How PHP Handles Form Input. For each HTML form parameter, PHP creates a global variable by the same name and makes it available to your script. For example, consider this HTML input type for two text fields:
If you have a text field named "your_name
", PHP will create a variable called $your_name
. And if you have another text field named "your_phone
", PHP will in turn, create a variable called $your_phone
. The values assigned to the PHP variables are the same values the user entered in the HTML text fields when filling out the form.
Example 4.21 illustrates a simple HTML form consisting of two fields. The form starts with the opening <form>
tag. The ACTION
attribute of the form tag is assigned the name of the PHP script that will handle the form input: <form ACTION="php script">
.
After the user fills out the form (see Figure 4.25) and presses the submit button, the values that he or she typed in the text boxes will be sent to the PHP script (see Example 4.22). The browser knows where to send the data based on the ACTION
attribute of the