Chapter 9. Form
Introduction
The genius of PHP is its seamless integration of form variables into your programs. It makes web programming smooth and simple, speeding the cycle from web form to PHP code to HTML output.
With that convenience, however, comes the responsibility to make sure that the user-provided information that flows so easily into your program contains appropriate content. External input can never be trusted, so it’s imperative always to validate all incoming data. Recipes 9.2 through 9.9 show how to validate common kinds of information as well as providing general guidelines on arbitrary form validation you might need to do. Recipe 9.10 discusses escaping HTML entities to allow the safe display of user-entered data. Recipe 9.14 covers how to process files uploaded by a user.
HTTP is a “stateless” protocol—it has no built-in mechanism that helps you to save information from one page so you can access it in other pages. Recipes 9.11, 9.12, and 9.13 all show ways to work around the fundamental problem of figuring out which user is making which requests to your web server.
Whenever PHP processes a page, it checks for URL and form
variables, uploaded files, applicable cookies, and web server and
environment variables. These are then directly accessible in the
following arrays: $_GET
, $_POST
, $_FILES
, $_COOKIE
, $_SERVER
, and $_ENV
. They hold, respectively, all variables
set in the query string, in the body of a post
request, by uploaded files, by cookies, by the web server, ...
Get PHP Cookbook, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.