Easy PHP Websites with the Zend Framework May 2009
Total Page:16
File Type:pdf, Size:1020Kb
Jumpstart your web development career using popular technologies including PHP and MySQL, Ajax, RSS, PayPal™, the Facebook Platform®, Amazon Web Services™, the Google Maps API™, the Google Analytics™ web analytics service, the Google AdSense™ and Google AdWords™ advertising services, and more! Easy PHP Websites with the Zend Framework W. Jason Gilmore Bestselling author of Beginning PHP and MySQL, Third Edition WJ Gilmore, LLC www.easyphpwebsites.com Easy PHP Websites with the Zend Framework W. Jason Gilmore Easy PHP Websites with the Zend Framework Copyright © 2009 W. Jason Gilmore Published by W.J. Gilmore, LLC 1373 Grandview Avenue, Suite 214 Columbus, Ohio 43212 All rights reserved. No part of this publication may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, scanning, recording or otherwise, except as permit- ted under the 1976 United States Copyright Act, without the prior written permission of the copyright owner and the publisher. Trademarks: Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to no ben- efit of the trademark owner, with no intention of infringement of the trademark. Zend is a trademark of Zend Technologies Ltd. Amazon Web Services™ is a trademark of Amazon.com, Inc. Facebook® is a registered trademark of Facebook Inc. PayPal® is a registered trademark of PayPal, Inc. Google Maps API™, GoogleAdSense™, and Google AdWords™ are trademarks of Google Inc. All other trademarks are the property of their respective owners. W.J. Gilmore, LLC is not associated with any product or vendor mentioned in this book. Limit of Liability: The information in this book is distributed on an "as is" basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor W.J. Gilmore, LLC shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. Source Code: The source code for this book is available at http://www.easyphpwebsites.com/. Dedicated to my family Table of Contents CHAPTER 1. Configuring Your Development Environment 1 CHAPTER 2. Introducing PHP 23 CHAPTER 3. Interacting With Your Users 51 CHAPTER 4. Introducing MySQL 75 CHAPTER 5. Introducing the Zend Framework 105 CHAPTER 6. Talking to the Database with Zend_Db 137 CHAPTER 7. Processing Forms and Sending Email 161 CHAPTER 8. Managing Your User Community 183 CHAPTER 9. Integrating Google Maps 211 CHAPTER 10. Introducing the Amazon Associates Web Service 231 CHAPTER 11. Enhancing the User Interface with Ajax 245 CHAPTER 12. Extend Your Website with RSS and Facebook 263 CHAPTER 13. Monitor Traffic and Manage Ads with Google 287 CHAPTER 14. Accepting Online Payments with PayPal 305 CHAPTER 15. Introducing Zend_Tool 315 Expanded Table of Contents CHAPTER 1. Configuring Your Development Environment 1 Step #1. Installing Apache, MySQL, and PHP on Windows 2 Step #2. Testing Your Installation 13 Step #3: Choosing a Code Editor 14 Step #4. Exploring Other Useful Software 17 Step #5. Choosing a Web Hosting Provider 19 CHAPTER 2. Introducing PHP 23 Step #1. Creating Your First PHP-Enabled Web Page 23 Step #2. Publishing Spreadsheet Data to the Web 38 Displaying Data in a Table 41 Step #3. Managing Your Site Design Using Templates 47 CHAPTER 3. Interacting With Your Users 51 Step #1. Creating a Contact Form 52 Step #2. Validating User Input 56 Step #3. Repopulating Form Data 65 Step #4. Sending Form Data via E-mail 67 Step #5. More on Securing User Input 72 CHAPTER 4. Introducing MySQL 75 Step #1. What Is a Relational Database? 76 Step #2. Introducing MySQL 77 Step #3. Introducing phpMyAdmin 83 Step #4. Moving Your Data to MySQL 86 Step #5. Connecting Your Website to MySQL 88 Step #6. Restricting Access 103 CHAPTER 5. Introducing the Zend Framework 105 Step #1. What Is a Web Framework? 106 Step #2. Introducing the Zend Framework 112 Step #3. Installing the Zend Framework 115 Step #4. Testing Your Installation 121 Step #6. Creating the Website Layout 125 Step #7. Creating a Configuration File 130 Step #8. The init() Method 133 Step #9. Creating Action Helpers 133 CHAPTER 6. Talking to the Database with Zend_Db 137 Step #1. Introducing Object-relational Mapping 138 Step #2. Introducing Zend_Db 139 Step #3. Creating Your First Model 140 Step #4. Querying Your Models 141 Step #5. Creating a Row Model 145 Step #6. Inserting, Updating, and Deleting Data 146 Step #7. Modeling Table Relationships 148 Step #8. JOINing Your Data 151 Step #9. Paginating Results with Zend_Paginator 155 Step #10. Creating and Managing Views 157 CHAPTER 7. Processing Forms and Sending Email 161 Step #1. Zend Framework Forms Fundamentals 162 Step #2. Creating the GameNomad Contact Form 163 Step #3. Validating Form Data 166 Step #5. Filtering Form Data 177 Step #6. Preventing Spamming Using CAPTCHAs 178 CHAPTER 8. Managing Your User Community 183 Step #1. Creating the Users Table and Model 184 Step #2. Registering Users 188 Step #4. Displaying User Profiles 203 CHAPTER 9. Integrating Google Maps 211 Step #1. Introducing the Google Maps API 212 Step #2. Adding Mapping Services to Your Website 223 CHAPTER 10. Introducing the Amazon Associates Web Service 231 Step #1. Introducing the Amazon Associates Web Service 232 Step #2. Introducing the Zend_Service_Amazon Component 234 Step #3. Searching for Products 239 Step #4. Customer Reviews 241 CHAPTER 11. Enhancing the User Interface with Ajax 245 Step #1. Introducing JavaScript 246 Step #2. Introducing Ajax 261 CHAPTER 12. Extend Your Website with RSS and Facebook 263 Step #1. Building and Publishing RSS Feeds 264 Step #2. Introducing the Facebook Platform 269 Step #3. Building Your First Facebook Application 274 Step #4. Introducing the Facebook PHP Client Library 276 Step #5. Creating a Facebook Platform Controller 277 Step #6. Creating a Facebook Profile Tab 280 Step #7. Sending a Facebook User Notification 282 Step #8. Adding Facebook Status Updates 283 Step #9. Deploying Your Facebook Application 285 CHAPTER 13. Monitor Traffic and Manage Ads with Google 287 Step #1: Monitoring Traffic with Google Analytics 287 Step #2. Advertising with Google AdWords 294 Step #3: Earning Money with Google AdSense 301 CHAPTER 14. Accepting Online Payments with PayPal 305 Step #1. Integrating PayPal Website Payments Standard 305 Step #2. Exploring Third-Party E-Commerce Solutions 309 CHAPTER 15. Introducing Zend_Tool 315 Step #1. Configuring Zend_Tool 315 Step #2. Creating and Managing Projects with Zend_Tool 316 About the Author Jason Gilmore is founder of W.J. Gilmore LLC (http://www.wjgilmore.com/), a publishing and consulting firm based out of Columbus, Ohio. Formerly Apress' open source editor, Jason fostered the development of more than 60 books, along the way helping to transform their open source line into one of the industry’s most respected publishing programs. Jason is the author of several books, including the bestselling Beginning PHP and MySQL: From Novice to Professional (currently in its third edition), Beginning PHP and PostgreSQL: From Nov- ice to Professional, and Beginning PHP and Oracle: From Novice to Professional. He has over 100 articles to his credit within prominent publications such as Developer.com, Linux Magazine, and TechTarget. Jason is a cofounder of CodeMash (http://www.codemash.org/), a nonprofit organization tasked with hosting an annual namesake developer’s conference, and was a member of the 2008 MySQL Conference speaker selection board. About the Technical Reviewer Chris J. Davis is a Senior Web Developer and Mac enthusiast living in North Texas. He is an inter- national speaker on the intersection of technology and community as well as a founder of the Habari Project, a next generation publishing platform. Also a published author, Chris coauthored Blog Design Solutions from Friends of Ed. Recently Chris has been invited to become a committer on the Apache Software Foundation Infra- structure team, which he enthusiastically accepted. You can find more out about Chris by visiting his website (http://chrisjdavis.org) or by following him on Twitter (http://twitter.com/chrisjdavis). Acknowledgements Ernest Hemingway once offered a particularly sober assessment of his trade, saying, "There is noth- ing to writing. All you do is sit down at a typewriter and bleed." This being my fifth book, and the first published under my eponymous press, W.J. Gilmore LLC, I can sympathize with his appraisal. Thankfully, psychological first aid is readily available from the family, friends, and colleagues who always make this process much less painful. Chris Davis did an excellent job as a tech reviewer, catching numerous mistakes made as I fought with both code and prose. Carlene DeFiore helped out immensely, reviewing chapters, helping with promotional strategy, and keeping me well fed. Countless other individuals played key advisory roles in helping this book to finally see the light. In alphabetical order, they include David Futato, Jonathan Hassell, Stu Johnson, and Matt Zenko. I'd also like to thank the great people at the Grandview Heights Fifth Third Bank and U.S. Post Office for helping get the business off the ground. I'd perhaps never have gotten into the publishing business without the help of Apress cofounder Gary Cornell, who first contacted me back in 2000 to write a book for what was then a very small but dar- ing computer book upstart (one which has since grown into a rather large but still daring publisher). He played a pivotal role in my development as an author and later an editor for Apress. Thank you Gary. Last but certainly not least, I'd like to thank you dear reader, for having enough faith to trade your hard-earned money for this book.