spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / php / php5-advanced2

[previous] [next]

Market Data Analyst (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume
Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans


Security Techniques: Part 2

To use PECL Filter

  1. Begin a new PHP script in your text editor or IDE, starting with the HTML (Script 4.2).

    The script has one CSS class for printing errors in a different color.

    Script 4.2. With this minimalist registration form, the Filter library is used to perform data validation and sanitization.

  2. Check for the form submission.

  3. Filter the name data.

    For the name field, there's no type to validate against, but it can be filtered to remove any HTML tags. The FILTER_SANITIZE_STRING filter will accomplish that. The last argument, FILTER_FLAG_NO_ENCODE_QUOTES, says that any quotation marks in the name (e.g., O'Toole) shouldn't be turned into an HTML entity equivalent.

  4. Print the name value or an error.

    The conditional if ($name) will be true if the $_POST['name'] variable was set and passed the filter. In that case, I'll print the filtered version and the original version, just for comparison.

  5. Validate the email address.

    The FILTER_VALIDATE_EMAIL filter is perfect here. If the submitted email address has a valid format, it will be returned. Otherwise, $email will equal either FALSE or NULL.

  6. Validate the ICQ number.

    This is validated as an integer.

  7. Filter the comments field.

    For the comments, any tags will be stripped (as with the name), but the quotation marks will also be encoded.

  8. Complete the main conditional and the PHP code.

  9. Create the HTML form.

  10. Complete the page.

  11. Save the file as filter.php, place it in your Web directory, and test in your Web browser (Figures 4.5 and 4.6).


    Figure 4.5 These values will be submitted, then filtered, resulting in Figure 4.6.


    Figure 4.6 At the top of the form the filtered values are displayed.

  12. View the HTML source of the page to see how the name and comments fields were treated (Figure 4.7).


    Figure 4.7 The HTMLsource code shows how all tags are stripped from the name and comments fields, plus how quotation marks in the comments are encoded.

TIPS
home / programming / php / php5-advanced2

[previous] [next]


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger

URL: