You are previewing PHP Hacks.
O'Reilly logo
PHP Hacks

Book Description

Programmers love its flexibility and speed; designers love its accessibility and convenience. When it comes to creating web sites, the PHP scripting language is truly a red-hot property. In fact, PHP is currently used on more than 19 million web sites, surpassing Microsoft's ASP .NET technology in popularity. Not surprisingly, this surge in usage has resulted in a number of PHP books hitting the market. Only one, though, takes the language beyond traditional Web programming and into mapping, graphing, multimedia, and beyond: PHP Hacks.

In PHP Hacks, author Jack Herrington wrings out his 20 years of code generation experience to deliver hands-on tools ranging from basic PHP and PEAR installation and scripting to advanced multimedia and database optimizing tricks.

On the practical side of things, PHP Hacks helps you develop more robust PHP applications by explaining how to improve your database design, automate application testing, and employ design patterns in your PHP scripts and classes. In the category of "cool," Herrington explains how to upgrade your Web interface through the creation of tabs, stickies, popups, and calendars. He even examines how to leverage maps and graphics in PHP. There's also a bounty of image and application hacks, including those that show you how to:

  • Integrate web sites with Google maps and satellite imaging

  • Dynamically display iPhoto libraries online

  • Add IRC, SMS, and Instant Messaging capabilities to your Web applications

  • Drop the latest Wikipedia dictionary onto your Sony PSP

  • Render graphics and user interfaces with SVG, DHTML, and Ajax

  • Whether you're a newcomer or an expert, you'll find great value in PHP Hacks, the only PHP guide that offers something useful and fun for everyone.

    Table of Contents

    1. PHP Hacks
      1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
      2. A Note Regarding Supplemental Files
      3. Credits
        1. About the Author
        2. Contributors
      4. Preface
        1. Why PHP Hacks?
        2. How to Use This Book
        3. How This Book Is Organized
          1. Common Problems
          2. Making a Cool Web Application
        4. Conventions Used in This Book
        5. Using Code Examples
        6. How to Contact Us
        7. Got a Hack?
        8. Safari Enabled
      5. 1. Installation and Basics
        1. 1.1. Hacks 1–2: Introduction
        2. Hack #1. Install PHP
          1. 1.2.1. Installing PHP on Windows
            1. 1.2.1.1. Installing PHP in Apache.
            2. 1.2.1.2. Installing PHP in IIS.
          2. 1.2.2. Installing PHP on Mac OS X
          3. 1.2.3. Installing PHP on Linux
          4. 1.2.4. Checking Your ISP Installation
          5. 1.2.5. Installing MySQL
          6. 1.2.6. Managing the Databases
          7. 1.2.7. See Also
        3. Hack #2. Install PEAR Modules
          1. 1.3.1. Installing PEAR Modules on Your ISP
      6. 2. Web Design
        1. 2.1. Hacks 3–10: Introduction
        2. Hack #3. Create a Skinnable Interface
          1. 2.2.1. The Code
          2. 2.2.2. Running the Hack
          3. 2.2.3. See Also
        3. Hack #4. Build a Breadcrumb Trail
          1. 2.3.1. The Code
          2. 2.3.2. Running the Hack
          3. 2.3.3. Hacking the Hack
          4. 2.3.4. See Also
        4. Hack #5. Create HTML Boxes
          1. 2.4.1. The Code
          2. 2.4.2. Running the Hack
          3. 2.4.3. Hacking the Hack
          4. 2.4.4. See Also
        5. Hack #6. Add Tabs to Your Web Interface
          1. 2.5.1. The Code
          2. 2.5.2. Running the Hack
          3. 2.5.3. See Also
        6. Hack #7. Give Your Customers Formatting Control with XSL
          1. 2.6.1. The Code
          2. 2.6.2. Running the Hack
          3. 2.6.3. See Also
        7. Hack #8. Build Lightweight HTML Graphs
          1. 2.7.1. The Code
          2. 2.7.2. Running the Hack
          3. 2.7.3. Hacking the Hack
          4. 2.7.4. See Also
        8. Hack #9. Properly Size Image Tags
          1. 2.8.1. The Code
          2. 2.8.2. Running the Hack
          3. 2.8.3. Hacking the Hack
          4. 2.8.4. See Also
        9. Hack #10. Send HTML Email
          1. 2.9.1. The Code
          2. 2.9.2. Running the Hack
          3. 2.9.3. See Also
      7. 3. DHTML
        1. 3.1. Hacks 11–26: Introduction
        2. Hack #11. Put an Interactive Spreadsheet on Your Page
          1. 3.2.1. The Code
          2. 3.2.2. Running the Hack
          3. 3.2.3. See Also
        3. Hack #12. Create Pop-Up Hints
          1. 3.3.1. The Code
          2. 3.3.2. Running the Hack
          3. 3.3.3. See Also
        4. Hack #13. Create Drag-and-Drop Lists
          1. 3.4.1. The Code
          2. 3.4.2. Running the Hack
          3. 3.4.3. See Also
        5. Hack #14. Build Dynamic HTML Graphs
          1. 3.5.1. The Code
          2. 3.5.2. Running the Hack
          3. 3.5.3. See Also
        6. Hack #15. Section Your Content with Spinners
          1. 3.6.1. The Code
          2. 3.6.2. Running the Hack
          3. 3.6.3. See Also
        7. Hack #16. Create Drop-Down Stickies
          1. 3.7.1. The Code
          2. 3.7.2. Running the Hack
          3. 3.7.3. See Also
        8. Hack #17. Create Dynamic Navigation Menus
          1. 3.8.1. The Code
          2. 3.8.2. Running the Hack
          3. 3.8.3. See Also
        9. Hack #18. Obscure JavaScript Dynamically
          1. 3.9.1. The Code
          2. 3.9.2. Running the Hack
        10. Hack #19. Build a DHTML Binary Clock
          1. 3.10.1. The Code
          2. 3.10.2. Running the Hack
          3. 3.10.3. See Also
        11. Hack #20. Tame Ajax with JSON
          1. 3.11.1. The Code
          2. 3.11.2. Running the Hack
        12. Hack #21. Make a DHTML Slideshow
          1. 3.12.1. The Code
          2. 3.12.2. Running the Hack
          3. 3.12.3. See Also
        13. Hack #22. Add Vector Graphics with PHP
          1. 3.13.1. The Code
          2. 3.13.2. Running the Hack
          3. 3.13.3. See Also
        14. Hack #23. Build a Color Selector
          1. 3.14.1. The Code
          2. 3.14.2. Running the Hack
        15. Hack #24. Create Link Graphs
          1. 3.15.1. The Code
          2. 3.15.2. Running the Hack
          3. 3.15.3. See Also
        16. Hack #25. Create an Interactive Calendar
          1. 3.16.1. The Code
          2. 3.16.2. Running the Hack
          3. 3.16.3. See Also
        17. Hack #26. Create the Google Maps Scrolling Effect
          1. 3.17.1. The Code
          2. 3.17.2. Running the Hack
          3. 3.17.3. See Also
      8. 4. Graphics
        1. 4.1. Hacks 27–33: Introduction
        2. Hack #27. Create Thumbnail Images
          1. 4.2.1. The Code
          2. 4.2.2. Running the Hack
          3. 4.2.3. See Also
        3. Hack #28. Create Beautiful Graphics with SVG
          1. 4.3.1. The Code
          2. 4.3.2. Running the Hack
          3. 4.3.3. See Also
        4. Hack #29. Simplify Your Graphics with Objects
          1. 4.4.1. The Code
          2. 4.4.2. Running the Hack
          3. 4.4.3. See Also
        5. Hack #30. Split One Image into Multiple Images
          1. 4.5.1. The Code
          2. 4.5.2. Running the Hack
          3. 4.5.3. Hacking the Hack
          4. 4.5.4. See Also
        6. Hack #31. Create Graphs with PHP
          1. 4.6.1. The Code
          2. 4.6.2. Running the Hack
          3. 4.6.3. See Also
        7. Hack #32. Create Image Overlays
          1. 4.7.1. The Code
          2. 4.7.2. Running the Hack
          3. 4.7.3. Hacking the Hack
          4. 4.7.4. See Also
        8. Hack #33. Access Your iPhoto Pictures with PHP
          1. 4.8.1. A Look Behind the Scenes: iPhoto Data
          2. 4.8.2. The Code
          3. 4.8.3. Running the Hack
          4. 4.8.4. Hacking the Hack
          5. 4.8.5. See Also
      9. 5. Databases and XML
        1. 5.1. Hacks 34–50: Introduction
        2. Hack #34. Design Better SQL Schemas
          1. 5.2.1. Bad Primary Keys
          2. 5.2.2. Misunderstanding Relational Databases
          3. 5.2.3. Use Not Null Fields
          4. 5.2.4. See Also
        3. Hack #35. Create Bulletproof Database Access
          1. 5.3.1. See Also
        4. Hack #36. Create Dynamic Database Access Objects
          1. 5.4.1. The Code
          2. 5.4.2. Running the Hack
          3. 5.4.3. Hacking the Hack
          4. 5.4.4. See Also
        5. Hack #37. Generate CRUD Database Code
          1. 5.5.1. The Code
          2. 5.5.2. Running the Hack
          3. 5.5.3. See Also
        6. Hack #38. Read XML on the Cheap with Regular Expressions
          1. 5.6.1. The Code
          2. 5.6.2. Running the Hack
          3. 5.6.3. Hacking the Hack
          4. 5.6.4. See Also
        7. Hack #39. Export Database Schema as XML
          1. 5.7.1. The Code
          2. 5.7.2. Running the Hack
          3. 5.7.3. See Also
        8. Hack #40. Create a Simple XML Query Handler for Database Access
          1. 5.8.1. The Code
          2. 5.8.2. Running the Hack
          3. 5.8.3. See Also
        9. Hack #41. Generate Database SQL
          1. 5.9.1. The Code
          2. 5.9.2. Running the Hack
          3. 5.9.3. See Also
        10. Hack #42. Generate Database Select Code
          1. 5.10.1. The Code
          2. 5.10.2. Running the Hack
          3. 5.10.3. See Also
        11. Hack #43. Convert CSV to PHP
          1. 5.11.1. The Code
          2. 5.11.2. Running the Hack
        12. Hack #44. Scrape Web Pages for Data
          1. 5.12.1. The Code
          2. 5.12.2. Running the Hack
          3. 5.12.3. Problems with Screen Scraping
          4. 5.12.4. See Also
        13. Hack #45. Suck Data from Excel Uploads
          1. 5.13.1. The Code
          2. 5.13.2. Running the Hack
          3. 5.13.3. See Also
        14. Hack #46. Load Your Database from Excel
          1. 5.14.1. The Code
          2. 5.14.2. Running the Hack
          3. 5.14.3. See Also
        15. Hack #47. Search Microsoft Word Documents
          1. 5.15.1. The Code
          2. 5.15.2. Running the Hack
          3. 5.15.3. See Also
        16. Hack #48. Create RTF Documents Dynamically
          1. 5.16.1. The Code
          2. 5.16.2. Running the Hack
          3. 5.16.3. See Also
        17. Hack #49. Create Excel Spreadsheets Dynamically
          1. 5.17.1. The Code
          2. 5.17.2. Running the Hack
          3. 5.17.3. See Also
        18. Hack #50. Create a Message Queue
          1. 5.18.1. The Code
          2. 5.18.2. Running the Hack
          3. 5.18.3. See Also
      10. 6. Application Design
        1. 6.1. Hacks 51–66: Introduction
        2. Hack #51. Create Modular Interfaces
          1. 6.2.1. The Code
          2. 6.2.2. Running the Hack
          3. 6.2.3. See Also
        3. Hack #52. Support Wiki Text
          1. 6.3.1. The Code
          2. 6.3.2. Running the Hack
          3. 6.3.3. Hacking the Hack
        4. Hack #53. Turn Any Object into an Array
          1. 6.4.1. The Code
          2. 6.4.2. Running the Hack
          3. 6.4.3. See Also
        5. Hack #54. Create XML the Right Way
          1. 6.5.1. The Code
          2. 6.5.2. Running the Hack
          3. 6.5.3. See Also
        6. Hack #55. Fix the Double Submit Problem
          1. 6.6.1. The Code
          2. 6.6.2. Running the Hack
        7. Hack #56. Create User-Customizable Reports
          1. 6.7.1. The Code
          2. 6.7.2. Running the Hack
          3. 6.7.3. See Also
        8. Hack #57. Create a Login System
          1. 6.8.1. The Code
          2. 6.8.2. Running the Hack
          3. 6.8.3. See Also
        9. Hack #58. Apply Security by Role
          1. 6.9.1. The Code
          2. 6.9.2. Running the Hack
          3. 6.9.3. See Also
        10. Hack #59. Migrate to MD5 Passwords
          1. 6.10.1. The Code
          2. 6.10.2. Running the Hack
          3. 6.10.3. See Also
        11. Hack #60. Make Usable URLs with mod_rewrite
          1. 6.11.1. An Introduction to Rewriting
          2. 6.11.2. Basic Rewriting
            1. 6.11.2.1. Basic redirects.
            2. 6.11.2.2. Forcing new requests.
          3. 6.11.3. Using Regular Expressions
            1. 6.11.3.1. Adding trailing slashes.
            2. 6.11.3.2. Match modifiers.
        12. Hack #61. Build an Ad Redirector
          1. 6.12.1. The Code
          2. 6.12.2. Running the Hack
        13. Hack #62. Add a Buy Now Button
          1. 6.13.1. Creating a Buy Now Button
          2. 6.13.2. Tracking the Sale
          3. 6.13.3. Verifying the Sale
          4. 6.13.4. The Code
          5. 6.13.5. Running the Hack
          6. 6.13.6. Hacking the Hack
          7. 6.13.7. See Also
        14. Hack #63. Find Out Where Your Guests Are Coming From
          1. 6.14.1. The Code
          2. 6.14.2. Running the Hack
          3. 6.14.3. See Also
        15. Hack #64. Import Information from vCards
          1. 6.15.1. The Code
          2. 6.15.2. Running the Hack
          3. 6.15.3. See Also
        16. Hack #65. Create vCard Files from Your Application's Data
          1. 6.16.1. The Code
          2. 6.16.2. Running the Hack
          3. 6.16.3. See Also
        17. Hack #66. Create a Shopping Cart
          1. 6.17.1. The Code
          2. 6.17.2. Running the Hack
          3. 6.17.3. See Also
      11. 7. Patterns
        1. 7.1. Hacks 67–78: Introduction
        2. Hack #67. Observe Your Objects
          1. 7.2.1. The Code
          2. 7.2.2. Running the Hack
          3. 7.2.3. See Also
        3. Hack #68. Create Objects with Abstract Factories
          1. 7.3.1. The Code
          2. 7.3.2. Running the Hack
          3. 7.3.3. See Also
        4. Hack #69. Flexible Object Creation with Factory Methods
          1. 7.4.1. The Code
          2. 7.4.2. Running the Hack
          3. 7.4.3. See Also
        5. Hack #70. Abstract Construction Code with a Builder
          1. 7.5.1. The Code
          2. 7.5.2. Running the Hack
        6. Hack #71. Separate What from How with Strategies
          1. 7.6.1. The Code
          2. 7.6.2. Running the Hack
        7. Hack #72. Link Up Two Modules with an Adapter
          1. 7.7.1. The Code
          2. 7.7.2. Running the Hack
        8. Hack #73. Write Portable Code with Bridges
          1. 7.8.1. The Code
          2. 7.8.2. Running the Hack
        9. Hack #74. Build Extensible Processing with Chains
          1. 7.9.1. The Code
          2. 7.9.2. Running the Hack
        10. Hack #75. Break Up Big Classes with Composites
          1. 7.10.1. The Code
          2. 7.10.2. Running the Hack
        11. Hack #76. Simplify APIs Using a Façade
          1. 7.11.1. The Code
          2. 7.11.2. Running the Hack
        12. Hack #77. Create Constant Objects with Singletons
          1. 7.12.1. The Code
          2. 7.12.2. Running the Hack
          3. 7.12.3. Hacking the Hack
        13. Hack #78. Ease Data Manipulation with Visitors
          1. 7.13.1. The Code
          2. 7.13.2. Running the Hack
          3. 7.13.3. Hacking the Hack
      12. 8. Testing
        1. 8.1. Hacks 79–85: Introduction
        2. Hack #79. Test Your Code with Unit Tests
          1. 8.2.1. The Code
          2. 8.2.2. Running the Hack
          3. 8.2.3. See Also
        3. Hack #80. Generate Your Unit Tests
          1. 8.3.1. The Code
          2. 8.3.2. Running the Hack
          3. 8.3.3. See Also
        4. Hack #81. Check for Broken Links
          1. 8.4.1. The Code
          2. 8.4.2. Running the Hack
          3. 8.4.3. See Also
        5. Hack #82. Test Your Application with Simulated Users
          1. 8.5.1. The Code
          2. 8.5.2. Running the Hack
          3. 8.5.3. See Also
        6. Hack #83. Test Your Application with Robots
          1. 8.6.1. The Code
          2. 8.6.2. Running the Hack
          3. 8.6.3. Hacking the Hack
          4. 8.6.4. See Also
        7. Hack #84. Spider Your Site
          1. 8.7.1. The Code
          2. 8.7.2. Running the Hack
          3. 8.7.3. See Also
        8. Hack #85. Generate Documentation Automatically
          1. 8.8.1. The Code
          2. 8.8.2. Running the Hack
          3. 8.8.3. See Also
      13. 9. Alternative UIs
        1. 9.1. Hacks 86–94: Introduction
        2. Hack #86. Create Custom Maps with MapServer
          1. 9.2.1. MapServer Concepts
          2. 9.2.2. Getting the PHP MapScript Extension
          3. 9.2.3. Hacking Maps with PHP
            1. 9.2.3.1. Step 1: Prepare data.
            2. 9.2.3.2. Step 2: Render a map.
            3. 9.2.3.3. Step 3: Modify the map.
            4. 9.2.3.4. Step 4: Mapping from scratch.
          4. 9.2.4. Learning More
          5. 9.2.5. See Also
        3. Hack #87. Build GUI Interfaces with GTk
          1. 9.3.1. The Code
          2. 9.3.2. Running the Hack
          3. 9.3.3. See Also
        4. Hack #88. Send RSS Feeds to Your IM Application Using Jabber
          1. 9.4.1. The Code
          2. 9.4.2. Running the Hack
          3. 9.4.3. Hacking the Hack
          4. 9.4.4. See Also
        5. Hack #89. IRC Your Web Application
          1. 9.5.1. The Code
          2. 9.5.2. Running the Hack
          3. 9.5.3. See Also
        6. Hack #90. Read RSS Feeds on Your PSP
          1. 9.6.1. The Code
          2. 9.6.2. Running the Hack
          3. 9.6.3. See Also
        7. Hack #91. Search Google by Link Graph
          1. 9.7.1. The Code
          2. 9.7.2. Running the Hack
          3. 9.7.3. See Also
        8. Hack #92. Create a New Interface for Amazon.com
          1. 9.8.1. The Code
          2. 9.8.2. Running the Hack
          3. 9.8.3. See Also
        9. Hack #93. Send SMS Messages from Your IM Client
          1. 9.9.1. The Code
          2. 9.9.2. Running the Hack
          3. 9.9.3. See Also
        10. Hack #94. Generate Flash Movies on the Fly
          1. 9.10.1. The Code
          2. 9.10.2. Running the Hack
          3. 9.10.3. Hacking the Hack
      14. 10. Fun Stuff
        1. 10.1. Hacks 95–100: Introduction
        2. Hack #95. Create Custom Google Maps
          1. 10.2.1. The Code
          2. 10.2.2. Running the Hack
          3. 10.2.3. See Also
        3. Hack #96. Create Dynamic Playlists
          1. 10.3.1. The Code
          2. 10.3.2. Running the Hack
          3. 10.3.3. See Also
        4. Hack #97. Create a Media Upload/Download Center
          1. 10.4.1. The Code
          2. 10.4.2. Running the Hack
          3. 10.4.3. See Also
        5. Hack #98. Check Your Network Game with PHP
          1. 10.5.1. The Code
          2. 10.5.2. Running the Hack
          3. 10.5.3. See Also
        6. Hack #99. Put Wikipedia on Your PSP
          1. 10.6.1. The Code
          2. 10.6.2. Running the Hack
          3. 10.6.3. See Also
        7. Hack #100. Create a Weather Showdown
          1. 10.7.1. The Code
          2. 10.7.2. Running the Hack
          3. 10.7.3. See Also
      15. About the Author
      16. Colophon
      17. SPECIAL OFFER: Upgrade this ebook with O’Reilly