Cover image for Wicked Cool PHP

Book description

PHP is an easy-to-use scripting language perfect for quickly creating the Web features you need. Once you know the basics of how the language works, wouldn't it be great to have a collection of useful scripts that solve those tricky problems and add interesting functionality to your site? We thought so, too.

Instead of starting at "Hello World," Wicked Cool PHP assumes that you're familiar with the language and jumps right into the good stuff. After you learn the FAQs of life-the most commonly wished for PHP scripts-you'll work your way through smart configuration options and the art of forms, all the way through to complex database-backed scripts.

Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text. The 76 easily implemented scripts will also teach you how to:

  • Send and receive email notifications

  • Track your visitors' behavior with cookies and sessions

  • Override PHP's default settings

  • Manipulate dates, images, and text on the fly

  • Harness SOAP and other web services

  • Create an online poll, ecard delivery system, and blog

But it's not all fun and games: Security is a big concern when programming any web application. So you'll learn how to encrypt your confidential data, safeguard your passwords, and prevent common cross-site-scripting attacks. And you'll learn how to customize all of the scripts to fit your own needs.

Dynamic Web content doesn't have to be difficult. Learn the secrets of the craft from two experienced PHP developers with Wicked Cool PHP.

Table of Contents

  1. Wicked Cool PHP
    1. WICKED COOL PHP.
    2. INTRODUCTION
    3. 1. THE FAQS OF LIFE—THE SCRIPTS EVERY PHP PROGRAMMER WANTS (OR NEEDS) TO KNOW
      1. #1: Including Another File as a Part of Your Script
        1. What Can Go Wrong?
      2. #2: Highlighting Alternate Row Colors in a Table
        1. Hacking the Script
      3. #3: Creating Previous/Next Links
        1. Using the Script
      4. #4: Printing the Contents of an Array
      5. #5: Turning an Array into a Nonarray Variable That Can Be Restored Later
        1. What Can Go Wrong?
      6. #6: Sorting Multidimensional Arrays
        1. Hacking the Script
      7. #7: Templating Your Site with Smarty
        1. Installing Smarty
        2. A Brief Smarty Tutorial
        3. What Can Go Wrong?
        4. Hacking the Script
    4. 2. CONFIGURING PHP
      1. Configuration Settings and the php.ini File
        1. Locating Your php.ini File
      2. #8: Revealing All of PHP's Settings
      3. #9: Reading an Individual Setting
      4. #10: Error Reporting
        1. Common Error Messages
      5. #11: Suppressing All Error Messages
      6. #12: Extending the Run Time of a Script
        1. What Can Go Wrong?
      7. #13: Preventing Users from Uploading Large Files
      8. #14: Turning Off Registered Global Variables
      9. #15: Enabling Magic Quotes
        1. What Can Go Wrong?
      10. #16: Restricting the Files that PHP Can Access
        1. What Can Go Wrong?
      11. #17: Shutting Down Specific Functions
      12. #18: Adding Extensions to PHP
        1. Adding PHP Extensions
          1. Checking to See If Extensions Are Already Loaded
          2. Asking Your Hosting Company to Load Extensions
        2. Installing Extensions with a Web-Based Control Panel
          1. Installing an Extension Manually
          2. Installing Libraries
        3. What Can Go Wrong?
    5. 3. PHP SECURITY
      1. Recommended Security Configuration Options
      2. #19: SQL Injection Attacks
      3. #20: Preventing Basic XSS Attacks
      4. #21: Using SafeHTML
        1. What Can Go Wrong?
      5. #22: Protecting Data with a One-Way Hash
        1. Hacking the Script
      6. #23: Encrypting Data with Mcrypt
        1. Hacking the Script
      7. #24: Generating Random Passwords
        1. Using the Script
    6. 4. WORKING WITH FORMS
      1. Security Measures: Forms Are Not Trustworthy
      2. Verification Strategies
      3. Using $_POST, $_GET, $_REQUEST, and $_FILES to Access Form Data
      4. #25: Fetching Form Variables Consistently and Safely
      5. #26: Trimming Excess Whitespace
      6. #27: Importing Form Variables into an Array
      7. #28: Making Sure a Response Is One of a Set of Given Values
        1. Hacking the Script
      8. #29: Using Multiple Submit Buttons
      9. #30: Validating a Credit Card
        1. Using the Script
        2. Hacking the Script
      10. #31: Double-Checking a Credit Card's Expiration Date
        1. Using the Script
      11. #32: Checking Valid Email Addresses
      12. #33: Checking American Phone Numbers
    7. 5. WORKING WITH TEXT AND HTML
      1. #34: Extracting Part of a String
        1. Hacking the Script
      2. #35: Making a String Uppercase, Lowercase, or Capitalized
        1. What Can Go Wrong?
      3. #36: Finding Substrings
        1. What Can Go Wrong?
      4. #37: Replacing Substrings
        1. What Can Go Wrong?
      5. #38: Finding and Fixing Misspelled Words with pspell
        1. Working with the Default Dictionary
        2. Adding a Custom Dictionary to pspell
        3. What Can Go Wrong?
      6. #39: Regular Expressions
        1. Regular Expression Basics
        2. Special Character Sequences
        3. Pattern Repeaters
        4. Grouping
        5. Character Classes
        6. Putting It All Together
        7. Matching and Extracting with Regular Expressions
          1. Extracting All Matches
        8. Replacing Substrings with Regular Expressions
      7. #40: Rearranging a Table
      8. #41: Creating a Screen Scraper
        1. Hacking the Script
      9. #42: Converting Plaintext into HTML-Ready Markup
      10. #43: Automatically Hyperlinking URLs
      11. #44: Stripping HTML Tags from Strings
    8. 6. WORKING WITH DATES
      1. How Unix Time Works
      2. #45: Getting the Current Timestamp
      3. #46: Getting the Timestamp of a Date in the Past or Future
        1. Creating Timestamps from a String
          1. Verifying Dates with strtotime()
        2. Creating Timestamps from Date Values
      4. #47: Formatting Dates and Times
      5. #48: Calculating the Day of the Week from a Given Date
      6. #49: Finding the Difference Between Two Dates
        1. Using the Script
        2. Hacking the Script
      7. MySQL Date Formats
    9. 7. WORKING WITH FILES
      1. File Permissions
        1. Permissions with an FTP Program
        2. The Command Line
        3. What Can Go Wrong?
      2. #50: Placing a File's Contents into a Variable
        1. Hacking the Script
        2. What Can Go Wrong?
      3. #51: Creating and Writing to a File
      4. #52: Checking to See If a File Exists
      5. #53: Deleting Files
      6. #54: Uploading Images to a Directory
        1. Using the Script
        2. What Can Go Wrong?
        3. Hacking the Script
      7. #55: Reading a Comma-Separated File
    10. 8. USER AND SESSION TRACKING
      1. Using Cookies and Sessions to Track User Data
        1. Cookies
          1. Advantages
          2. Disadvantages
        2. Sessions
          1. Advantages
          2. Disadvantages
      2. #56: Creating a "Welcome Back, Username!" Message with Cookies
        1. What Can Go Wrong?
      3. #57: Using Sessions to Temporarily Store Data
        1. What Can Go Wrong?
      4. #58: Checking to See If a User's Browser Accepts Cookies
      5. #59: Redirecting Users to Different Pages
      6. #60: Forcing a User to Use SSL-Encrypted Pages
      7. #61: Extracting Client Information
      8. #62: Session Timeouts
      9. #63: A Simple Login System
    11. 9. WORKING WITH EMAIL
      1. #64: Using PHPMailer to Send Mail
        1. Installing PHPMailer
        2. Using the Script
        3. Adding Attachments
        4. What Can Go Wrong?
      2. #65: Using Email to Verify User Accounts
    12. 10. WORKING WITH IMAGES
      1. #66: Creating a CAPTCHA (Security) Image
      2. #67: Creating Thumbnail Images
    13. 11. USING cURL TO INTERACT WITH WEB SERVICES
      1. #68: Connecting to Other Websites
      2. #69: Using Cookies
      3. #70: Transforming XML into a Usable Form
      4. #71: Using Mapping Web Services
      5. #72: Using PHP and SOAP to Request Data from Amazon.com
      6. #73: Building a Web Service
    14. 12. INTERMEDIATE PROJECTS
      1. #74: A User Poll
        1. Creating a Ballot Form
        2. Processing the Ballot
        3. Getting Poll Results
        4. Hacking the Script
      2. #75: Electronic Greeting Cards
        1. Choosing a Card
        2. Sending the Card
        3. Viewing the Card
        4. Hacking the Script
      3. #76: A Blogging System
        1. Creating Blog Entries
        2. Displaying an Entry
        3. Adding Comments
        4. Creating a Blog Index
        5. Hacking the Script
    15. A. APPENDIX
    16. About the Authors
    17. COLOPHON