Chapter 9. Forms

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 through show how to validate common kinds of information as well as providing general guidelines on arbitrary form validation you might need to do. Preventing Cross-Site Scripting discusses escaping HTML entities to allow the safe display of user-entered data. Processing Uploaded Files 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 , , and 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, ...

Get PHP Cookbook, 3rd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.