Tải bản đầy đủ (.pdf) (592 trang)

Tài liệu Practical Web 2.0 Applications with PHP pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (7.07 MB, 592 trang )

this print for content only—size & color not accurate spine = 1.1163" 592 page count
Books for professionals By professionals
®
Practical Web 2.0 Applications with PHP
Dear Reader,
Many programming books on the market today focus specifically on a particu-
lar methodology or software package, and although you will gain a solid under-
standing of the subject matter from these books, you won’t always know how
to apply what you’ve learned in a real-world situation. This book is designed
to show you how to bring together many different ideas and features by start-
ing with a clean slate and gradually building the code base so it evolves into a
complete web application.
The premise of the application we build in this book is that it is a “Web 2.0”
application. What this means is that (among other things) our application gen-
erates accessible and standards-compliant code while making heavy of use of
Ajax. We achieve this by using the Smarty

Template Engine and Cascading
Style Sheets, as well as the Prototype JavaScript library. Additionally, we create
a fun and intuitive interface by applying simple visual effects on various pages
using the Script.aculo.us JavaScript library.
To help with the development of the extensive PHP code in this book, we
use the Zend Framework. This is an open source PHP 5 library that contains
many different components that you can easily use in any of your day-to-day
development. We use many of the Zend Framework components in this book, such
as database abstraction (with a focus on MySQL
®
and PostgreSQL), logging,
authentication, and search.
The “Web 2.0” application that we build in this book is a collaborative
blogging tool. It will allow users to register and create a personal blog. When


creating blog posts, users will be able upload images, apply tags, and assign
locations (using Google Maps). We will also look at how to use microformats
when displaying user blog posts.
Quentin Zervaas
US $44.99
Shelve in
PHP
User level:
Intermediate–Advanced
Zervaas
Web 2.0
Applications with
PHP
The eXperT’s Voice
®
in WeB DeVelopmenT
Practical
Web 2.0
Applications with PHP
cyan
maGenTa
yelloW
Black
panTone 123 c
Quentin Zervaas
Companion
eBook Available
www.apress.com
SOURCE CODE ONLINE
Companion eBook


See last page for details
on $10 eBook version
ISBN-13: 978-1-59059-906-8
ISBN-10: 1-59059-906-3
9 781590 599068
5 4 4 9 9
Develop a complete PHP web application
from start to finish
RELATED TITLES

Practical
www.it-ebooks.info
www.it-ebooks.info
Quentin Zervaas
Practical Web 2.0
Applications with PHP
9063CH00CMP3 11/19/07 8:39 PM Page i
www.it-ebooks.info
Practical Web 2.0 Applications with PHP
Copyright © 2008 by Quentin Zervaas
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-59059-906-8
ISBN-10 (pbk): 1-59059-906-3
ISBN-13 (electronic): 978-1-4302-0474-9
ISBN-10 (electronic): 1-4302-0474-5
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
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 the benefit of the trademark
owner, with no intention of infringement of the trademark.
Lead Editor: Ben Renow-Clarke
Technical Reviewer: Jeff Sambells
Editorial Board: Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick,
Jason Gilmore, Kevin Goff, Jonathan Hassell, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper,
Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh
Project Manager: Richard Dal Porto
Copy Editors: Andy Carroll, Kim Wimpsett
Assistant Production Director: Kari Brooks-Copony
Production Editor: Liz Berry
Compositor: Diana Van Winkle
Proofreader: Lisa Hamilton
Indexer: Broccoli Information Management
Artist: Diana Van Winkle
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail , or
visit .
For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600,
Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail , or visit
.
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 Apress 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.
The source code for this book is available to readers at .
9063CH00CMP3 11/19/07 8:39 PM Page ii
www.it-ebooks.info

Contents at a Glance
About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
About the Technical Reviewer
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi
Introduction
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
■CHAPTER 1 Application Planning and Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
■CHAPTER 2 Setting Up the Application Framework . . . . . . . . . . . . . . . . . . . . . . . . . . 9
■CHAPTER 3 User Authentication, Authorization, and Management . . . . . . . . . . . . . 45
■CHAPTER 4 User Registration, Login, and Logout . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
■CHAPTER 5 Introduction to Prototype and Scriptaculous . . . . . . . . . . . . . . . . . . 123
■CHAPTER 6 Styling the Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
■CHAPTER 7 Building the Blogging System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
■CHAPTER 8 Extending the Blog Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
■CHAPTER 9 Personalized User Areas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
■CHAPTER 10 Implementing Web 2.0 Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
■CHAPTER 11 A Dynamic Image Gallery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
■CHAPTER 12 Implementing Site Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
■CHAPTER 13 Integrating Google Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
■CHAPTER 14 Deployment and Maintenance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
iii
9063CH00CMP3 11/19/07 8:39 PM Page iii
www.it-ebooks.info
9063CH00CMP3 11/19/07 8:39 PM Page iv
www.it-ebooks.info
Contents
About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
About the Technical Reviewer
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi

Introduction
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
■CHAPTER 1
Application Planning and Design
. . . . . . . . . . . . . . . . . . . . . . . . . . 1
What Is Web 2.0? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Database Connectivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Web Site Templates
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Web Site Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Main Home Page and User Home Page . . . . . . . . . . . . . . . . . . . . . . . . 3
User Registration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Account Login and Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
User Blogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Web Site Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Application Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Other Aspects of Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Search-Engine Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
PHPDoc-Style Commenting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Application Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Maintainability and Extensibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Version Control and Unit Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
■CHAPTER 2 Setting Up the Application Framework . . . . . . . . . . . . . . . . . . . . 9
Web Server Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Operating System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Installing the Apache HTTP Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Installing MySQL 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Installing PHP 5.2.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

v
9063CH00CMP3 11/19/07 8:39 PM Page v
www.it-ebooks.info
■CONTENTSvi
Application Filesystem Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Web Root Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Data Storage Directory
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
PHP Classes Directory
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Templates Directory
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Full Directory Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Installing the Zend Framework
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Configuring the Web Server
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Creating a Virtual Host in Linux
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Creating a Virtual Host in Windows
. . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Restarting Your Web Server
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Setting Up the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Using the Model-View-Controller Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Separating Application Logic from Presentation Logic . . . . . . . . . . . 19
Directing All Requests to index.php . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Introduction to the Zend_Controller Class . . . . . . . . . . . . . . . . . . . . . 22
How Requests Work with Zend_Controller . . . . . . . . . . . . . . . . . . . . . 23
Creating the IndexController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

Defining Application Settings
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Connecting to the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Testing the Database Connection
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
The Smarty Template Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Why Not Use a Different Template Engine? . . . . . . . . . . . . . . . . . . . . 33
Downloading and Installing Smarty . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Automatic View Rendering with Zend_Controller . . . . . . . . . . . . . . . 36
Integrating Smarty with the Web Site Controllers . . . . . . . . . . . . . . . 39
Adding Logging Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Writing to the Log File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
■CHAPTER 3
User Authentication, Authorization, and Management
. . . . . . 45
Creating the User Database Table
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Timestamps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
User Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Introduction to Zend_Auth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Instantiating Zend_Auth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Authenticating with Zend_Auth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Introduction to Zend_Acl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
A Zend_Acl Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
9063CH00CMP3 11/19/07 8:39 PM Page vi
www.it-ebooks.info
Combining Zend_Auth, Zend_Acl, and Zend_Controller_Front . . . . . . . . 57
Managing User Records with DatabaseObject . . . . . . . . . . . . . . . . . . . . . . 61
The DatabaseObject_User Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

Using DatabaseObject_User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Managing User Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Using Profile_User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Integrating Profile_User with DatabaseObject_User . . . . . . . . . . . . 69
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
■CHAPTER 4 User Registration, Login, and Logout . . . . . . . . . . . . . . . . . . . . . 73
Adding User Registration to the Application . . . . . . . . . . . . . . . . . . . . . . . . 73
Creating the Form Processor for User Registration . . . . . . . . . . . . . 74
Displaying the Registration Form and Processing Registrations . . . 81
Adding CAPTCHA to the User Registration Form . . . . . . . . . . . . . . . . 88
Adding E-mail Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Implementing Account Login and Logout . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Creating the Login Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Adding the Account Controller Login Action . . . . . . . . . . . . . . . . . . . 102
Logging Successful and Failed Login Attempts . . . . . . . . . . . . . . . . 105
Logging Users Out of Their Accounts . . . . . . . . . . . . . . . . . . . . . . . . 107
Dealing with Forgotten Passwords
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Resetting a User’s Password
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Functions for Resetting Passwords . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Implementing Account Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Creating the Account Home Page
. . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Updating the Web Site Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Allowing Users to Update Their Details . . . . . . . . . . . . . . . . . . . . . . . 120
Summary
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
■CHAPTER 5 Introduction to Prototype and Scriptaculous . . . . . . . . . . . . 123
Downloading and Installing Prototype . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123

Prototype Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Selecting Objects in the Document Object Model . . . . . . . . . . . . . . . . . . . 124
The $() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
The getElementsByClassName() Function . . . . . . . . . . . . . . . . . . . . 125
The $$() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
The getElementsBySelector() Function . . . . . . . . . . . . . . . . . . . . . . . 129
Prototype’s Hash Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
■CONTENTS vii
vii
9063CH00CMP3 11/19/07 8:39 PM Page vii
www.it-ebooks.info
Other Element Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Showing and Hiding Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Retrieving Dimensions of Elements . . . . . . . . . . . . . . . . . . . . . . 131
Managing Classes of Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Manipulating Strings with Prototype . . . . . . . . . . . . . . . . . . . . . . . . . 133
Ajax Operations in Prototype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Ajax Request Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Ajax Callback Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
JavaScript Object Notation (JSON) . . . . . . . . . . . . . . . . . . . . . . . . . . 138
An Ajax.Request Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Event Handling in Prototype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Observing an Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Finding Out Which Element an Event Occurred On . . . . . . . . . . . . . 146
Canceling an Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Creating JavaScript Classes in Prototype . . . . . . . . . . . . . . . . . . . . . . . . . 147
Creating a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Binding Function Calls to Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
From Prototype to Scriptaculous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Prebuilt Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

Drag and Drop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Visual Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
DOM Element Builder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
JavaScript Unit Testing
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Downloading and Installing Scriptaculous
. . . . . . . . . . . . . . . . . . . . . . . . . 154
Combining Prototype, Scriptaculous, Ajax, and PHP
in a Useful Example
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Creating the Main HTML Page: index.php
. . . . . . . . . . . . . . . . . . . . 156
Styling the Application: styles.css . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Creating and Populating the Database: schema.sql . . . . . . . . . . . . 158
Managing the List Items on the Server Side: items.php . . . . . . . . 159
Processing Ajax Requests on the Server Side: processor.php . . . 161
Creating the Client-Side Application Logic: scripts.js . . . . . . . . . . . 163
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
■CHAPTER 6 Styling the Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Adding Page Titles and Breadcrumbs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
The Breadcrumbs Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Generating URLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Setting the Title and Trail for Each Controller Action . . . . . . . . . . . . 178
Creating a Smarty Plug-In to Output Breadcrumbs . . . . . . . . . . . . 180
Displaying the Page Title . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
■CONTENTSviii
9063CH00CMP3 11/19/07 8:39 PM Page viii
809b8b6f91d5ff50033254241f3132ed
www.it-ebooks.info
Integrating the Design into the Application . . . . . . . . . . . . . . . . . . . . . . . . 183

Creating the Static HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Moving the HTML Markup into Smarty Templates . . . . . . . . . . . . . 188
Constructing the CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Specifying Media Types and Loading the CSS File . . . . . . . . . . . . . 192
Creating the Application CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Creating a Print-Only Style Sheet . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
The Full Application Style Sheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Styling the Application Web Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Loading Prototype and Scriptaculous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Implementing Client-Side Form Validation . . . . . . . . . . . . . . . . . . . . . . . . . 208
Adding JSON Support to CustomControllerAction . . . . . . . . . . . . . . 209
Modifying the Form Processor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Modifying the Registration Controller Action . . . . . . . . . . . . . . . . . . 210
Creating the JavaScript Form Validator . . . . . . . . . . . . . . . . . . . . . . . 212
Loading the UserRegistrationForm Class . . . . . . . . . . . . . . . . . . . . . 216
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
■CHAPTER 7 Building the Blogging System . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Creating the Database Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Setting Up DatabaseObject and Profile Classes . . . . . . . . . . . . . . . . . . . . 221
Creating the DatabaseObject_BlogPost Class
. . . . . . . . . . . . . . . . . 221
Creating the Profile_BlogPost Class . . . . . . . . . . . . . . . . . . . . . . . . . 223
Creating a Controller for Managing Blog Posts
. . . . . . . . . . . . . . . . . . . . . 223
Extending the Application Permissions
. . . . . . . . . . . . . . . . . . . . . . . 223
The BlogmanagerController Actions . . . . . . . . . . . . . . . . . . . . . . . . . 225
Linking to Blog Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Creating and Editing Blog Posts
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228

Creating the Blog Post Submission Form Template . . . . . . . . . . . . 228
Instantiating FormProcessor_BlogPost in editAction() . . . . . . . . . . 231
Implementing the FormProcessor_BlogPost Class . . . . . . . . . . . . . 233
Generating a Permanent Link to a Blog Post . . . . . . . . . . . . . . . . . . 240
Filtering Submitted HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Creating a New Blog Post . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Previewing Blog Posts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Creating the Preview Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Implementing the Preview Template . . . . . . . . . . . . . . . . . . . . . . . . . 249
Requesting Confirmation for User Actions . . . . . . . . . . . . . . . . . . . . 252
■CONTENTS ix
9063CH00CMP3 11/19/07 8:39 PM Page ix
www.it-ebooks.info
Updating the Status of a Blog Post . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Completing setstatusAction() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Notifying the User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
■CHAPTER 8 Extending the Blog Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Listing Blog Posts on the Blog Manager Index . . . . . . . . . . . . . . . . . . . . . 265
Fetching Blog Posts from the Database . . . . . . . . . . . . . . . . . . . . . . 266
Assigning Recent Posts and the Monthly Summary
to the Template
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Displaying Recent Posts in the Template . . . . . . . . . . . . . . . . . . . . . 276
Displaying the Monthly Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Ajaxing the Blog Monthly Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Creating the Ajax Request Output . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
The BlogMonthlySummary JavaScript Class . . . . . . . . . . . . . . . . . . 285
Installing the BlogMonthlySummary Class . . . . . . . . . . . . . . . . . . . . 287
Notifying the User About the Content Update . . . . . . . . . . . . . . . . . . 287

Integrating a WYSIWYG Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Downloading and Installing FCKeditor . . . . . . . . . . . . . . . . . . . . . . . 292
Configuring FCKeditor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Loading FCKeditor in the Blog Editing Page . . . . . . . . . . . . . . . . . . . 294
Summary
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
■CHAPTER 9 Personalized User Areas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Controlling User Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Presenting Customizable Settings to Users
. . . . . . . . . . . . . . . . . . . 298
Processing Changes to User Settings . . . . . . . . . . . . . . . . . . . . . . . . 299
Creating Default User Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
The UserController Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Routing Requests to UserController . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Handling Requests to UserController . . . . . . . . . . . . . . . . . . . . . . . . . 309
Displaying the User’s Blog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Displaying the Blog Index Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Displaying Individual Blog Posts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Generating Blog Archive Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Displaying the Monthly Archive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Populating the Application Home Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Loading Recent Public Posts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Implementing the Application Home Page . . . . . . . . . . . . . . . . . . . . 327
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
■CONTENTSx
9063CH00CMP3 11/19/07 8:39 PM Page x
www.it-ebooks.info
■CHAPTER 10 Implementing Web 2.0 Features . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Implementing Tagging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336

Managing Blog Post Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
Displaying a User’s Tags on Their Blog . . . . . . . . . . . . . . . . . . . . . . . 344
Displaying a Tag Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Displaying Tags on Each Post . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Web Feeds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Data Formats for Web Feeds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
Creating an Atom Feed with Zend_Feed . . . . . . . . . . . . . . . . . . . . . . 352
Adding the Feed to UserController . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Linking to Your Feed
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
Other Feed Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Microformats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
An Example of Using Microformats . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Why Use Microformats? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Microformatting Your Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
Allowing Users to Create a Public Profile . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Allowing Users to Create a Public Profile . . . . . . . . . . . . . . . . . . . . . 363
Displaying a User’s Profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
■CHAPTER 11 A Dynamic Image Gallery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Storing Uploaded Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Creating the Database Table for Image Data . . . . . . . . . . . . . . . . . . 373
Controlling Uploaded Images with DatabaseObject . . . . . . . . . . . . 373
Uploading Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
Setting the Form Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Adding the Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Specifying the File Input Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Setting the Maximum File Size
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Handling Uploaded Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379

Sending Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
Resizing Images
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Creating Thumbnails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Linking the Thumbnailer to the Image Action Handler . . . . . . . . . . 395
■CONTENTS xi
9063CH00CMP3 11/19/07 8:39 PM Page xi
www.it-ebooks.info
Managing Blog Post Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Automatically Loading Blog Post Images . . . . . . . . . . . . . . . . . . . . . 399
Displaying Images on the Post Preview . . . . . . . . . . . . . . . . . . . . . . 401
Deleting Blog Post Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Using Scriptaculous and Ajax to Delete Images . . . . . . . . . . . . . . . 406
Deleting Images when Posts Are Deleted . . . . . . . . . . . . . . . . . . . . . 411
Reordering Blog Post Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Displaying Images on User Blogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Extending the GetPosts() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Displaying Thumbnail Images on Blog Index . . . . . . . . . . . . . . . . . . 418
Displaying Images on the Blog Details Page . . . . . . . . . . . . . . . . . . 420
Displaying Larger Images with Lightbox . . . . . . . . . . . . . . . . . . . . . . 422
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
■CHAPTER 12 Implementing Site Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Introduction to Zend_Search_Lucene . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Comparison to MySQL Full-Text Indexing . . . . . . . . . . . . . . . . . . . . . 428
Zend_Search_Lucene Field Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
Field Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Indexing Application Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Indexing Multiple Types of Data
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Creating a New Zend_Search_Lucene_Document

. . . . . . . . . . . . . 431
Retrieving the Index Location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Building the Entire Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
Indexing and Unindexing a Single Blog Post
. . . . . . . . . . . . . . . . . . 435
Triggering Search Index Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Creating the Search Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Adding the Search Form
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Handling Search Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Querying the Search Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Displaying Search Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
Types of Searches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Adding Autocompletion to the Search Tool . . . . . . . . . . . . . . . . . . . . . . . . 452
Providing Search Suggestions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Creating an Action Handler to Return Search Results . . . . . . . . . . 453
Retrieving Search Suggestions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Loading the SearchSuggestor Class . . . . . . . . . . . . . . . . . . . . . . . . . 457
Displaying Search Suggestions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Adding Mouse Navigation to Results . . . . . . . . . . . . . . . . . . . . . . . . . 460
Adding Keyboard Navigation to Results . . . . . . . . . . . . . . . . . . . . . . 462
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
■CONTENTSxii
9063CH00CMP3 11/19/07 8:39 PM Page xii
www.it-ebooks.info
■CHAPTER 13 Integrating Google Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Google Maps Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Geocoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Displaying Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
Controlling Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473

Planning Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
Limitations of Google Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
Browser Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Documentation and Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Creating a Google Maps API Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Adding Location Storage Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
Creating the Database Table
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
Creating the DatabaseObject_BlogPostLocation Class . . . . . . . . . 475
Modifying Blog Posts to Load Locations . . . . . . . . . . . . . . . . . . . . . . 477
Creating Our First Map
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
Creating a New Blog Manager Controller Action . . . . . . . . . . . . . . . 479
Displaying Your First Google Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
Managing Locations on the Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
Handling Location Management Ajax Requests . . . . . . . . . . . . . . . 487
Creating the Address Lookup Form . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Extending the BlogLocationManager JavaScript Class . . . . . . . . . 493
Using BlogLocationManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
Displaying the Map on Users’ Public Blogs . . . . . . . . . . . . . . . . . . . . . . . . 509
Outputting Locations Using the Geo Microformat . . . . . . . . . . . . . . 509
Creating the BlogLocations Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
Updating the Blog Post Display Template
. . . . . . . . . . . . . . . . . . . . . 514
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
■CHAPTER 14 Deployment and Maintenance . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Application Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
E-mailing Critical Errors to an Administrator
. . . . . . . . . . . . . . . . . . 519
Using Application Logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523

Site Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
Objectives of Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
Handling Predispatch Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
Application Runtime Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531
Web Site Administration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535
Administrator Section Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535
Implementing Administration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
■CONTENTS xiii
9063CH00CMP3 11/19/07 8:39 PM Page xiii
www.it-ebooks.info
Application Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
Different Configurations for Different Servers . . . . . . . . . . . . . . . . . 538
Deploying Application Files with Rsync . . . . . . . . . . . . . . . . . . . . . . . 542
Backup and Restore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Exporting a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Importing a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
9063CH00CMP3 11/19/07 8:39 PM Page xiv
www.it-ebooks.info
About the Author
■QUENTIN ZERVAAS is a web developer based in Adelaide, South Australia, where he has been
self-employed since 2003. After receiving his bachelor’s degree in computer science from the
University of Adelaide in 2001, Quentin worked for several web development firms before
branching out on his own, developing a wide range of custom web applications for customers
all around the world.
Quentin has recently started a new company called Recite Media (ite.
com.au) with two partners. Recite Media develops web applications primarily for other devel-
opment or design companies to resell. Its flagship product, Recite CMS, is being used by
some of Australia’s largest companies.

Quentin also runs and writes for his PHP development resource site, PhpRiot (www.phpriot.com),
which provides a number of useful articles on a wide variety of PHP-related topics.
After completing his role as the technical reviewer for Beginning Ajax with PHP: From
Novice to Professional (Apress, 2006), he decided to undertake writing this book.
xv
9063CH00CMP3 11/19/07 8:39 PM Page xv
www.it-ebooks.info
About the Technical Reviewer
■JEFFREY SAMBELLS is a graphic designer and self-taught web application developer best
known for his unique ability to merge the visual world of graphics with the mental realm of
code. After obtaining his bachelor’s of technology degree in graphic communications manage-
ment with a minor in multimedia, Jeffrey originally enjoyed the paper-and-ink printing
industry, but he soon realized the world of pixels and code was where his ideas would prosper.
Jeffrey has previously published articles related to print design and has contributed to
award-winning graphical and Internet software designs. His latest book, AdvancED DOM
Scripting: Dynamic Web Design Techniques (friends of ED, 2007), was an instant success. In
late 2005, Jeffrey also became a PHP 4 Zend Certified Engineer; he updated the certification to
PHP 5 in September 2006 to become one of the first PHP 5 Zend Certified Engineers. Jeffrey
also maintains a blog at where he discusses his thoughts about
everything from web development to photography.
He currently lives and plays in Ontario, Canada, with his wife, Stephanie; his daughter,
Addison; and their little dog, Milo.
xvi
9063CH00CMP3 11/19/07 8:39 PM Page xvi
www.it-ebooks.info
Introduction
Many of today’s web development books and articles cover single aspects of the development
life cycle, delving only into specific features rather than looking at the whole picture.
In this book, we will develop a complete web application. Although we will be using various
third-party libraries and tools to aid in development, we will be developing the application from

start to finish.
The focus of this book is on Web 2.0, a catchphrase that has been in use for a few years
now and is typically used to refer to web sites or web applications that have particular charac-
teristics. Some of these characteristics include the following:
•Correctly using HTML/XHTML, CSS, and other standards
•Using Ajax (Asynchronous JavaScript and XML) to provide a responsive application
without requiring a full refresh of pages
• Allowing syndication of web site content using RSS
•Adding wikis, blogs, or tags
Although not everybody is an advocate of the “Web 2.0” phrase, the term does signify
forward progress in web development. And although not everybody has the need to provide
a wiki or a blog on their web site, the other characteristics listed (such as correct standards
usage) provide a good basis for a web site and should be used by all developers, regardless of
how they want their web site or application categorized.
I wrote this book because I want to share with other users how I build web sites. Having
been a web developer for ten years now (full-time for the past seven), I have a solid under-
standing of a wide range of web-related topics and have much to offer newer developers or
developers looking to expand their own knowledge.
Who This Book Is For
This book has been written primarily for intermediate to expert PHP programmers. Although
programmers of all levels will benefit from this book, we do jump in to the deep end very
quickly, so some prior knowledge of PHP is assumed.
Having said that, if you’re relatively new to PHP, you will definitely benefit from this book
because it will formalize some of the techniques you have already learned and will show you
some different ways of approaching various problems.
In this book, I have made the assumption that you are familiar with HTML and CSS,
although since most of the code developed in this book is PHP and JavaScript, an advanced
knowledge of HTML and CSS is not critical. All JavaScript code is explained thoroughly, which,
in combination with the Prototype JavaScript library we will be using, makes the listings rela-
tively straightforward.

xvii
9063CH00CMP3 11/19/07 8:39 PM Page xvii
www.it-ebooks.info
How This Book Is Structured
We will start the book by determining which features to implement in our web application
and then implement each one as we progress through the book. Each chapter will add a new
set of features to the application, until reaching the final chapter where we look at strategies
for deploying the application.
The specific type of application we develop in this book (a multiuser blogging system) is
not particularly important; rather, it is used simply as a tool to show you the process of devel-
oping a web application. Each chapter is specifically designed to demonstrate particular
aspects of development that may arise regardless of the type of application:
• Chapter 1, Application Planning and Design.We begin the book by looking at what
defines Web 2.0, as well as looking briefly at the features that will be implemented in
the application. Additionally, this chapter covers various aspects of the web develop-
ment life cycle that should be considered when planning and implementing web
applications.
• Chapter 2, Setting Up the Application Framework. In this chapter, we begin to imple-
ment the web application. This process begins by correctly setting up the environment
(that is, installing the correct web server software) and then by creating the initial file
structure of the site. In addition to connecting to the database with PHP, we will handle
user requests with the Zend Framework and manage HTML code using the Smarty
Template Engine.
• Chapter 3, User Authentication, Authorization, and Management. This chapter gives
the first look at using a database. We look at how to easily manage database data when
we implement the user system. Additionally, we look at how a role-based permissions
system works and then implement it into the application.
• Chapter 4, User Registration, Login, and Logout. Continuing from Chapter 3, this
chapter shows how to implement a user registration system. Since this is the first time
the book deals with user-submitted data, this chapter looks at how to correctly deal

with such data when we create the registration and login forms.
• Chapter 5, Introduction to Prototype and Scriptaculous. Since we make heavy use of
JavaScript and Ajax in later chapters, we move away from the main application in this
chapter while we explore two of the most useful JavaScript libraries available. Prototype
helps programmers develop easily maintainable cross-platform JavaScript code, while
Scriptaculous simplifies the process of adding appealing visual effects to web pages.
• Chapter 6, Styling the Web Application. In this chapter, we step back slightly from the
web application in that we focus more on the user experience rather than on the main
application features. We first look at implementing various navigational items (which
also gives us a first taste of developing custom Smarty plug-ins), and we then complete
the chapter by implementing a simple and clean web design into the application.
■INTRODUCTIONxviii
9063CH00CMP3 11/19/07 8:39 PM Page xviii
www.it-ebooks.info
• Chapter 7, Building the Blogging System. This chapter moves on to beginning the
implementation of the blogging system. In this chapter, we give users the ability to add,
edit, and delete their blog posts. One of the key concepts covered is how to correctly
allow user-submitted HTML while keeping the site safe and secure for visitors.
• Chapter 8, Extending the Blog Manager. This chapter largely builds on what was
implemented in Chapter 7. A comprehensive Ajax example is included in this chapter
that we will use to help users manage their blogs. We also integrate an open source
What You See Is What You Get (WYSIWYG) editor into a blog post creation form.
• Chapter 9, Personalized User Areas. At this point in the book, users can create a new
account as well as manage their very own blogs. In this chapter, we make their blogs
public in the application. We give each user a public home page within our application
web site in which all of their blog posts are shown. This chapter shows how to imple-
ment more advanced URL schemes, as well as shows you how to enable users to
customize their own experience by managing their own profiles and settings.
• Chapter 10, Implementing Web 2.0 Features. Although several of the features we
define as Web 2.0 (such as standards compliancy and Ajax) apply throughout web

applications, a few concrete features are often defined as being part of the Web 2.0
movement. In this chapter, we will look at some of these, including microformats,
web feeds (RSS and Atom), and tagging.
• Chapter 11, A Dynamic Image Gallery.In this chapter, we expand the capabilities of
the blogging system by allowing users to upload photos for each of their blog posts.
This allows us to see how to correctly handle not only file uploads but also image-
specific issues, such as dynamically generating thumbnails.
• Chapter 12, Implementing Site Search. This chapter is essentially split into two parts:
creating search indexes based on user blog posts and then allowing site visitors to
search for posts. Indexing data can be a complicated topic, but by using the tools pro-
vided by the Zend Framework, the task is made simpler. After implementing the basic
search functionality, we extend it to use an intuitive Ajax-based autocompleter, similar
to that of Google Suggest.
• Chapter 13, Integrating Google Maps. You as a developer can use many freely available
web services on the Internet to improve your own web site. In this chapter, we extend
the blog capabilities further to allow users to add locations to their blog posts using
Google Maps. We create an advanced sample implementation of Google Maps that
combines the Google Maps API with our database using Ajax, as well as learn how to
manage map data in real-time.
• Chapter 14, Deployment and Maintenance. In this, the final chapter, we cover a num-
ber of miscellaneous topics related to developing a polished application. This is partly
an extension of some functionality implemented in Chapter 2 but also introduces sev-
eral new ideas (such as application deployment).
■INTRODUCTION xix
9063CH00CMP3 11/19/07 8:39 PM Page xix
www.it-ebooks.info
Prerequisites
A number of third-party applications and libraries are used in this book. We discuss down-
loading and installing each of these as required, but for your reference, the following are used:
•PHP 5.2.3

•Apache 2.2 on Linux (and its variants) or Windows (earlier versions of Apache may
also work)
•MySQL 5 or PostgreSQL 8
•Prototype 1.5.1.1
• Scriptaculous 1.7.1 beta 3
•Zend Framework 1.0.2 or newer
•Smarty Template Engine 2.6.18
• FCKeditor 2.4.3 (an open source JavaScript WYSIWYG editor)
In addition to these applications and libraries, in this book I use several custom PHP
classes that I have implemented. Each of these is available in the application source, which
can be downloaded as per the following instructions.
Downloading the Code
All code listings in this book are available from the book’s web site at .
The source code for this book is also available to readers at on this book’s
page on the Apress web site. You can download the full web application as it stands at the end of
any of the chapters.
Additionally, I’ve included a number of bonus add-ons in the source code, including an
administration area and a blog post commenting system.
Contacting the Author
If you have any questions about the code in this book, your first stop should be the book’s web
site at . This web site contains answers to frequently asked ques-
tions as well as various other web development resources.
Alternatively, you can contact me directly at Please ensure
your questions relate directly to the content of the book. It is likely I will publish your ques-
tions and the answers on the FAQ section of the book’s web site.
■INTRODUCTIONxx
9063CH00CMP3 11/19/07 8:39 PM Page xx
www.it-ebooks.info
Application Planning and Design
In this book we will be creating a blogging web application that will allow us to cover not only

all of the different PHP and database considerations involved, but also a number of different
Web 2.0 principles (such as Ajax and tagging). The blogging application will allow users to create
and manage their own blog. Each user will have their own public page on which their blog posts
are published.
Figure 1-1 shows how the application will be structured. As you can see, we will use a data-
base to store application data, and we will create separate logical areas in the application to
manage each feature as required. Additionally, one of the core aspects of Web 2.0 applications is
using standards-compliant XHTML and CSS. We will focus on developing clean markup and
well-structured JavaScript classes to ensure maximum compatibility and accessibility.
Figure 1-1. The basic structure of our web application
There are a number of different aspects of the application that we must cover, including
database connectivity, template management, user authentication and permissions, and con-
sumption of third-party web services.
In this chapter we will look at all features of the web application from a “black box” point
of view. Each specific feature will be broken down in its respective chapter; here we will look at
the application as a whole and discuss various options that need to be considered.
In essence, this chapter can be viewed as an informal design document, including an
analysis of all required features and a look at design from a high-level. In developing the web
application, we will be using both custom-written code as well as various third-party libraries
(such as Prototype for JavaScript development, Smarty for template management in PHP, and
the Zend Framework for several other features).
1
CHAPTER 1
9063Ch01CMP2 10/20/07 1:47 PM Page 1
www.it-ebooks.info
What Is Web 2.0?
So exactly what defines a web site as being “Web 2.0”? There are many different opinions on
this, making it difficult to pinpoint an exact definition; however, some of the features typically
associated with Web 2.0 sites are as follows:
• Using standards-compliant HTML and CSS. This allows sites to work across many plat-

forms and helps with accessibility. This includes the use of microformats to generate
friendly HTML that can be used across a variety of platforms (as we will see in Chapter 10).
• Using Ajax to provide a rich user interface. By performing trivial operations in the
background using XMLHttpRequest, web pages can be more functional and intuitive.
■Note XMLHttpRequest is a JavaScript API that allows a background HTTP request to occur while a user
is viewing a web page. This means that the current page can be updated based on a response from the
server without the user navigating to another page on the web site. The phrase “making an Ajax request”
(or similar) typically refers to performing an HTTP request in the background using XMLHttpRequest.
• Sharing data using web feeds and web services. Users like to aggregate many feeds to
easily receive content updates from their favorite sites using web feeds (such as RSS or
Atom). Additionally, web services can enable one site to use data from other sites (for
instance, we will display maps on our site using Google Maps).
• Incorporating social networking tools. Blogs and forums can enable users to commu-
nicate with each other.
While none of these features or aspects of development are new, we use the Web 2.0 term
to describe the current generation of web sites that make good use of HTML and CSS while
perhaps improving their interface with Ajax and social-networking tools. These are sites that
“do things right.” However, that’s not to say that a site that uses any of these features is neces-
sarily a good site.
Database Connectivity
In this application, we will need to save a number of different types of data, including
•User accounts
•User settings
•User-submitted data (such as blog posts, images, tags)
We will make use of a database abstraction layer to insert, update, and delete data from the
database. This allows us to develop PHP code that will work regardless of the type of underlying
database server. Within this book we will make use of MySQL, but if you want to use PostgreSQL
instead, it would simply be a matter of changing the application’s database connection settings.
CHAPTER 1 ■ APPLICATION PLANNING AND DESIGN2
9063Ch01CMP2 10/20/07 1:47 PM Page 2

www.it-ebooks.info
We will be using the Zend Framework’s Zend_DB class to handle the database abstraction.
This is essentially an interface to the PDO extension for PHP 5. We will cover the installation of
all required software in Chapter 2.
■Note In this book, all “database code” (i.e., PHP code that interacts with the database) will be self-
contained within its relevant class or function. This means that if you want to use a different database
abstraction layer (such as PEAR DB, ADOdb, or your own custom layer), it will be fairly straightforward to
implement in place of Zend_Db.
Web Site Templates
One of the reasons PHP has become so popular is that you can easily include PHP code
directly within the HTML code you want to output. This makes developing simple and small
web applications very easy; however, this typically doesn’t scale well. When an application
grows large, it becomes difficult either to add new functionality within a bunch of HTML
markup or to change the site design by sifting through the PHP code.
To deal with this, we aim to separate our application logic from our display logic. Essen-
tially, this means the code that does the hard work (such as processing forms, reading data
from the database, or checking user permissions) is performed in one place, while the HTML
that will be output to the end user is stored in its own template file.
In Chapter 2 we will look at Model-View-Controller (MVC), which is a design pattern
specifically describing this separation of application and display logic. We will be using the
Smarty Template Engine to manage the display of templates, as this is a very popular and
powerful template engine (Smarty will essentially make up the “view” portion of MVC, as we
will see in Chapter 2).
Web Site Features
So far we have only looked at peripheral aspects of web application development, so let’s take
a look at some specifics. Let’s look at what the end users of the web application would see.
Main Home Page and User Home Page
The home page of our web application will display blog posts from all users in a single journal.
Registered users will be able to decide whether or not their posts are public and therefore are
displayed on the home page.

In addition to the main home page, each user will have a public home page. This will dis-
play all of their blog posts in a single listing.
CHAPTER 1 ■ APPLICATION PLANNING AND DESIGN 3
9063Ch01CMP2 10/20/07 1:47 PM Page 3
www.it-ebooks.info

×