Easy PHP Websites with the Zend Framework May 2009

Total Page:16

File Type:pdf, Size:1020Kb

Easy PHP Websites with the Zend Framework May 2009 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.
Recommended publications
  • Introducting Innovations in Open Source Projects
    Introducing Innovations into Open Source Projects Dissertation zur Erlangung des Grades eines Doktors der Naturwissenschaften (Dr. rer. nat.) am Fachbereich Mathematik und Informatik der Freien Universität Berlin von Sinan Christopher Özbek Berlin August 2010 2 Gutachter: Professor Dr. Lutz Prechelt, Freie Universität Berlin Professor Kevin Crowston, Syracuse University Datum der Disputation: 17.12.2010 4 Abstract This thesis presents a qualitative study using Grounded Theory Methodology on the question of how to change development processes in Open Source projects. The mailing list communication of thirteen medium-sized Open Source projects over the year 2007 was analyzed to answer this question. It resulted in eight main concepts revolving around the introduction of innovation, i.e. new processes, services, and tools, into the projects including topics such as the migration to new systems, the question on where to host services, how radical Open Source projects can change their ways, and how compliance to processes and conventions is enforced. These are complemented with (1) the result of five case studies in which innovation introductions were conducted with Open Source projects, and with (2) a theoretical comparison of the results of this thesis to four theories and scientific perspectives from the organizational and social sciences such as Path Dependence, the Garbage Can model, Social-Network analysis, and Actor-Network theory. The results show that innovation introduction is a multifaceted phenomenon, of which this thesis discusses the most salient conceptual aspects. The thesis concludes with practical advice for innovators and specialized hints for the most popular innovations. 5 6 Acknowledgements I want to thank the following individuals for contributing to the completion of this thesis: • Lutz Prechelt for advising me over these long five years.
    [Show full text]
  • Process Innovations for Security Vulnerability Prevention in Open Source Web Applications
    Process Innovations For Security Vulnerability Prevention In Open Source Web Applications Diploma Thesis Florian Thiel April 30, 2009 Department of Mathematics and Computer Science Institute for Computer Science Software Engineering Working Group Responsible University Professor: Prof. Dr. Lutz Prechelt Supervisor: Dipl.-Medieninf. Martin Gruhn This work is licensed under the Creative Commons Attribution 3.0 Germany License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/de/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. Please note: Pictures in this work are not licensed under a Creative Commons Attribution license and may not be copied as freely as the rest of this work. Comic strips in this work are by Randall Munroe of xkcd and licensed under a Creative Commons Attribution-NonCommercial 2.5 license. The picture on the title page is by Matteo Carli and licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.0 license. It is available at http://www.flickr.com/photos/matteocarli/2489736887/. Affirmation Of Independent Work I hereby declare that I wrote this thesis myself without sources other than those indicated herein. All parts taken from published and unpublished scripts are indicated as such. Berlin, April 30, 2009 ii Acknowledgments First of all, I would like to thank Professor Lutz Prechelt who suggested and supported the topic of this thesis and enabled me to spend months of interesting research in the fascinating world of security research. I also owe a lot to Martin Gruhn, my supervisor, who provided lots of feedback and was especially helpful to flesh out the argumentation and create a good narrative.
    [Show full text]
  • "Automated Security Analysis of Web Application Technologies"
    Saarland University Faculty of Mathematics and Computer Science Department of Computer Science Automated Security Analysis of Web Application Technologies Malte Horst Arthur Skoruppa Dissertation zur Erlangung des Grades des Doktors der Ingenieurwissenschaften der Fakultät für Mathematik und Informatik der Universität des Saarlandes Saarbrücken, 2017 ii Tag des Kolloquiums: 14. Dezember 2017 Dekan: Prof. Dr. Frank-Olaf Schreyer Prüfungsausschuss Vorsitzender: Prof. Dr. Christian Rossow Berichterstattende: Prof. Dr. Michael Backes Prof. Dr. Andreas Zeller Akademischer Mitarbeiter: Dr. Robert Künnemann iii Zusammenfassung Das Web hat sich zu einem komplexen Netz aus hochinteraktiven Seiten und Anwendungen entwickelt, welches wir täglich zu kommerziellen und sozialen Zwecken einsetzen. Dementsprechend ist die Sicherheit von Webanwendungen von höchster Relevanz. Das automatisierte Auffinden von Sicherheitslücken ist ein anspruchsvolles, aber wichtiges Forschungsgebiet mit dem Ziel, Entwickler zu unterstützen und das Web sicherer zu machen. In dieser Arbeit nutzen wir statische Analysemethoden, um automatisiert Lücken in JavaScript- und PHP-Programmen zu entdecken. JavaScript ist clientseitig die wichtigste Sprache des Webs, während PHP auf der Serverseite am weitesten verbreitet ist. Im ersten Teil nutzen wir eine Reihe von Programmtransformationen und Informationsflussanalyse, um den JavaScript Helios Wahl-Client zu untersuchen. Helios ist ein modernes Wahlsystem, welches auf konzeptueller Ebene eingehend analysiert wurde und dessen Implementierung als sehr sicher gilt. Wir enthüllen zwei schwere und bis dato unentdeckte Sicherheitslücken. Im zweiten Teil präsentieren wir ein Framework, das es Entwicklern er- möglicht, PHP Code auf frei modellierbare Schwachstellen zu untersuchen. Zu diesem Zweck konstruieren wir sogenannte Code-Property-Graphen und im- portieren diese anschließend in eine Graphdatenbank. Schwachstellen können nun als geeignete Datenbankanfragen formuliert werden.
    [Show full text]
  • A Field Analysis of Relational Database Schemas in Open-Source Software (Extended) Fabien Coelho, Alexandre Aillos, Samuel Pilot, Shamil Valeev
    A Field Analysis of Relational Database Schemas in Open-source Software (Extended) Fabien Coelho, Alexandre Aillos, Samuel Pilot, Shamil Valeev To cite this version: Fabien Coelho, Alexandre Aillos, Samuel Pilot, Shamil Valeev. A Field Analysis of Relational Database Schemas in Open-source Software (Extended). The Third International Conference on Ad- vances in Databases, Knowledge, and Data Applications, Jan 2011, St Marteen, Netherlands Antilles. p. 9-15. hal-00903676 HAL Id: hal-00903676 https://hal-mines-paristech.archives-ouvertes.fr/hal-00903676 Submitted on 12 Nov 2013 HAL is a multi-disciplinary open access L’archive ouverte pluridisciplinaire HAL, est archive for the deposit and dissemination of sci- destinée au dépôt et à la diffusion de documents entific research documents, whether they are pub- scientifiques de niveau recherche, publiés ou non, lished or not. The documents may come from émanant des établissements d’enseignement et de teaching and research institutions in France or recherche français ou étrangers, des laboratoires abroad, or from public or private research centers. publics ou privés. 1 A Field Analysis of Relational Database Schemas in Open-source Software (Extended) Fabien Coelho, Alexandre Aillos, Samuel Pilot, and Shamil Valeev CRI, Maths & Systems, MINES ParisTech, 35, rue Saint Honore,´ 77305 Fontainebleau cedex, France. [email protected], [email protected] Abstract—The relational schemas of 512 open-source projects legal, and philosophical issues. Open-source software (OSS) storing their data in MySQL or PostgreSQL databases are in- is a subject of academic studies [12] in psychology, sociology, vestigated by querying the standard information schema, looking economics, or software engineering, including quantitative for various issues.
    [Show full text]
  • Creating a Web Application Contents
    Creating a Web Application Jesse Young Chief Technology Officer, Envaya [email protected] For the COSTECH Teknohama Incubator in Dar es Salaam August 2011 Contents Overview ............................................................................................................................................. 2 About the Author ................................................................................................................................ 2 Pre‐Existing Software Packages and Services ..................................................................................... 2 Hosting ................................................................................................................................................ 3 Domain Name Registration ................................................................................................................. 7 Programming Languages and Frameworks ......................................................................................... 8 Server Operating Systems ................................................................................................................. 12 HTTP Server Software ....................................................................................................................... 13 Data Storage...................................................................................................................................... 15 Development Tools ..........................................................................................................................
    [Show full text]
  • Introduction to Website Designing & Development
    SHREE SATGURUVE NAMAH Introduction to Website Designing & Development (Draft Version) Introduction to Website Designing & Development - 1 - List of Topics • Web-Application Management - An Overview • Web-Application / WebApp - Introduction • List of Web-Programming Language Popularity • What Web-Programming languages are people talking about? • Popular Web-Programming languages – An Overview • Database - Overview • Popular Web-Database Systems – An Overview • Database – Parameters • Application Service Provider (ASP) - What is it about? • Application Service Provider (ASP) - How it works? • Integrated Development Environment (IDE) • Comparison of Integrated Development Environments • Web-Application / WebApp - Development Guide • Before You Code: Part A – Reviewing Hosting Plans Features • Before You Code: Part B – Reviewing Hosting Plans Features • Before You Code: Web Site Basics: Stuff Beginners Need To Know • Before You Code: Database Websites from Scratch • Definition of Framework? • What is Application Framework? • What is Software Framework? • What is Web-Application Framework? • What is Enterprise Architecture Framework? • List of Content Management Frameworks (CMF) • List of Content Management Systems (CMS) • List of Web-application Frameworks • Glossary / Acronym / File Extensions • References • Conclusion Introduction to Website Designing & Development - 2 - Web-Application Management - An Overview - DATA PRESENTATION APPLICATION SERVER DATABASE SERVER Content Management Web-Programming Language Record/Files Management (Client-Side
    [Show full text]
  • Free Your IT Business in Africa!
    , Free your IT-Business in Africa! Advanced Training Material on African Free and Open Source Software (FOSS) Business Models for IT-SMEs Map:Including downloads of the OpenCase Source Studies Browser Firefox. of More information: see back side. successful African IT Businesses around FOSS http://www.ict-innovation.fossfa.net Version 1.3, published on December 19th 2012 On behalf of Implemented by [ict@innovation: Free your IT-Business in Africa! Advanced Training Material on African Free and Open Source Software (FOSS) Business Models for IT-SMEs] Created during the programme "ict@innovation - Creating Business and Learning Opportunities with Free and Open Source Software in Africa" implemented by FOSSFA and GIZ. Provided under a Creative Commons Attribution-Share Alike 3.0 Germany License. Copyright: FOSSFA & GIZ. For more information, see www.ict-innovation.fossfa.net 1 Imprint Published by GIZ – Deutsche Gesellschaft für Internationale Zusammenarbeit GmbH Friedrich-Ebert-Allee 40 53113 Bonn Germany Phone +49 228 4460-0 www.giz.de FOSSFA – Free Software and Open Source Foundation for Africa Secretariat hosted at Advanced Information Technology Institute (AITI) of the The Ghana-India Kofi Annan Centre of Excellence in ICT PMB, State House, Accra Ghana Phone +233 (244) 954 413 www.fossfa.net For more information, please contact: [email protected] FOSSFA Secretariat [email protected] FOSSFA Community Empowerment Manager (CEM) [email protected] GIZ Division Economic Development & Employment, ICT Advisor, Sector Project ICT4D Funding This Training Guide was produced with the financial assistance of the German Federal Ministry for Economic Cooperation and Development (BMZ). The content of this document are the sole responsibility of the authors and can under no circumstances be regarded as reflecting the position of the BMZ, GIZ, or FOSSFA.
    [Show full text]