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
CopyEditors: 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 Debolsk
594 trang |
Chia sẻ: tlsuongmuoi | Lượt xem: 2370 | Lượt tải: 0
Bạn đang xem trước 20 trang tài liệu Practical Web 2.0 Applications with PHP, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
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
W
eb 2.0
Applications w
ith 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
54499
Develop a complete PHP web application
from start to finish
RelAted titleS
Practical
Quentin Zervaas
Practical Web 2.0
Applications with PHP
9063CH00CMP3 11/19/07 8:39 PM Page i
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 orders-ny@springer-sbm.com, 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 info@apress.com, 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
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
9063CH00CMP3 11/19/07 8:39 PM Page iv
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
■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
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
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
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
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
■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
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
■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
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
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 (
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
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
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
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
• 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.
• C
Các file đính kèm theo tài liệu này:
- Practical Web 2.0 Applications with PHP.pdf