Table Of ContentPraise for Essential PHP Tools:
Modules, Extensions, and Accelerators:
"Sklar's writing is clear, concise, and accurate ... The book is a definite
recommendation for intermediate to advanced programmers both
as a text and as a reference. It certainly has earned itselfa spot on my
frequently used bookshelf!"
-Timothy Boronczyk, Codewalkers (www.codewalkers.com)
''The tools are easy to find and useful, the documentation excellent,
and the writing style easy to follow. All this makes Essential PHP
Tools a highly recommended book for anyone using PHP on their
web server."
-Harold McFarland, Midwest Book Review (www.midwestbookreview.com)
"From Pear packages to parsing XML files, this book is a huge time
saver to developing your own solutions."
-Richard Testani, AppleTalk weblog
(www.apple-sauce.com/blogger /blogger.html)
Essential PHP Tools:
Modules, Extensions,
and Accelerators
DAVID SKLAR
APress Media, LLC
Essential PHP Tools: Modules, Extensions, and Accelerators
Copyright ©2004 by David Sklar
Originally published by Apress in 2004
AII 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 978-1-59059-280-9 ISBN 978-1-4302-0714-6 (eBook)
DOI 10.1007/978-1-4302-0714-6
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.
Technical Reviewer: Adam Trachtenberg
Editorial Board: Steve Anglin, Dan Appleman, Gary Cornell, James Cox, Tony Davis, John
Franklin, Chris Mills, Steve Rycroft, Dominic Shakeshaft, Julian Skinner, Jim Sumser, Karen
Watterson, Gavin Wray, John Zukowski
Assistant Publisher: Grace Wong
Project Manager: Kylie Johnston
Copy Editor: Kim Wimpsett
Production Manager: Kari Brooks
Production Editor: Noemi Hollander
Proofreader: Thistle Hill Publishing Services, LLC
Compositor: Kinetic Publishing Services, LLC
Indexer: Valerie Perry
Artist: Kinetic Publishing Services, LLC
Cover Designer: Kurt Krames
Manufacturing Manager: Tom Debolski
The information in this book is distributed on an "as is" hasis, 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.
Contents at a Glance
About the Author ................................................... ix
Acknowledgments .................................................... xi
Introduction ...................................................... xiii
Part One Databases ........................................... 1
Chapter 1 Accessing Databases with DB ........................ 3
Chapter 2 Accessing Databases with ADODB ................... 23
Part Two HTML ................................................ 53
Chapter 3 Using HTML_QuickForm for Form Processing ........ 55
Chapter 4 Templating with Smarty ............................. 93
Part Three XML ................................................ 149
Chapter 5 Performing Event-Based XML Parsing with
XML Parser ......................................... 151
Chapter 6 Developing Lightweight Web Services
with XML RPC ....................................... 171
Chapter 7 Developing Heavyweight Web Services
with SOAP .......................................... 187
Chapter 8 Performing Easier XML Parsing with
SimpleXML .......................................... 215
Part Four Networking ........................................ 223
Chapter 9 Sending Mail ....................................... 225
Chapter 10 Working with Authentication, Users,
and Passwords ...................................... 241
Contents at a Glance
Part Five Debugging, Caching, and Optimizing .......... 263
Chapter 11 Understanding PHP Internals ...................... 265
Chapter 12 Profiling and Debugging with Xdebug ............. 275
Chapter 13 Accelerating with Code Caches .................... 297
Index .............................................................. 331
iv
Contents
About the Author ................................................... ix
Acknowledgments .................................................... xi
Introduction ...................................................... xiii
Part One Databases ...................................... 1
Chapter 1 Accessing Databases with DB ................ 3
Exploring a Simple DB Example .................................... 3
Introducing DSNs ................................................... 4
Sending Queries and Retrieving Results .......................... 5
Understanding Quoting and Placeholders .......................... 9
Examining Data Retrieval Convenience Methods .................. 13
Understanding Query Information ................................. 16
Running a Query Multiple Times .................................. 17
Introducing Sequences ............................................. 20
Introducing Error Handling ....................................... 20
Chapter 2 Accessing Databases with ADODB ........... 23
Connecting and Simple Queries ................................... 23
Introducing Record Sets .......................................... 27
Understanding Error Handling ..................................... 32
Introducing Sequences ............................................. 35
Generating HTML ................................................... 36
Caching ............................................................. 49
Exporting Data ..................................................... 50
Part Two HTML ............................................ 53
Chapter 3 Using HTML_QuickForm for Form
Processing ...................................... 55
Creating and Displaying a Form .................................. 55
Processing Submitted Data ........................................ 78
Setting Validation Rules ......................................... 82
v
Contents
Chapter 4 Templating with Smarty ......................
93
Installing and Configuring Smarty ............................... 94
Understanding Basic Smarty Concepts ............................. 96
Implementing Conditional Logic ................................. 110
Looping and Cycling Through Data ............................... 113
Including Other Files and Templates ........................... 124
Processing Text .................................................. 129
Caching Template Output ......................................... 140
Putting It All Together ......................................... 144
Part Three XML ............................................ 149
Chapter 5 Performing Event-Based XML Parsing
with XML Parser ..............................
151
Handling Tags and Character Data ............................... 152
Handling Processing Instructions ............................... 160
Handling Nested Tags ............................................. 163
Using Per-Element Methods ....................................... 165
Specifying Character Sets ....................................... 168
Chapter 6 Developing Lightweight Web Services
with XML RPC. .................................
171
Choosing XML-RPC ................................................. 171
Calling XML-RPC Procedures on Another Server ................. 177
Serving XML-RPC Methods ......................................... 180
Chapter 7 Developing Heavyweight Web Services
with SOAP ......................................
187
Choosing SOAP ..................................................... 187
Installing SOAP .................................................. 188
Making SOAP Requests ............................................. 188
Serving SOAP Requests ........................................... 199
Exploring Further ................................................ 213
vi
Contents
Chapter 8 Performing Easier XML Parsing with
SimpleXML ......................................
215
Creating SimpleXML Objects ...................................... 215
Working with SimpleXML Objects ................................. 217
Working with Namespaces ......................................... 220
Searching with XPath ............................................. 221
Part Four Networking ................................. 223
Chapter 9 Sending Mail. .................................
225
Sending Plain-Text Mail Messages with PEAR Mail .............. 225
Sending MIME Mail Messages with Mail_mime ..................... 229
Chapter 10 Working with Authentication, Users,
and Passwords ..................................
241
Using the Auth Module ........................................... 241
Auth HTTP ......................................................... 260
Part Five Debugging, Caching,
and Optimizing ........................... 263
Chapter 11 Understanding PHP Internals ............... 265
Going from Source File to Output ............................... 266
Seeing How a Code Cache Works .................................. 272
Seeing How a Debugger Works .................................... 272
Examining Some Differences Between the Internals
of PHP 4 and PHP 5 ............................................ 273
Chapter 12 Profiling and Debugging with Xdebug .... 275
Installing Xdebug ................................................ 275
Tracing Functions ................................................ 276
Profiling ......................................................... 283
Using Remote Debugging .......................................... 286
vii
Contents
Chapter 13 Accelerating with Code Caches ............ 297
Working with APC: The Alternative PHP Cache .................. 298
Working with ionCube PHP Accelerator .......................... 307
Working with Turck MMCache ...................................... 317
Benchmarking Cache Performance ........... ...................... 328
Index ............................................................. 331
viii
Description:This book is an essential guide to some of the best free add-ons to PHP. These add-ons, or tools, provide invaluable functionality for improving your PHP web applications, including accessing databases, generating robust Web forms, using page templating systems, creating and parsing XML documents, a