Professional LAMP
Linux®, Apache, MySQL®,
and PHP5 Web Development
Jason Gerner
Elizabeth Naramore
Morgan L. Owens
Matt Warden
01_59723x ffirs.qxd 10/31/05 6:37 PM Page iii
Professional LAMP
01_59723x ffirs.qxd 10/31/05 6:37 PM Page i
01_59723x ffirs.qxd 10/31/05 6:37 PM Page ii
Professional LAMP
Linux®, Apache, MySQL®,
and PHP5 Web Development
Jason Gerner
Elizabeth Naramore
Morgan L. Owens
Matt Warden
01_59723x ffirs.qxd 10/31/05 6:37 PM Page iii
Professional LAMP: Linux®, Apache, MySQL®, and PHP5
Web Development
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2006 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN-13: 978-0-7645-9723-7
ISBN-10: 0-7645-9723-X
FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMA-
TION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER,
READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DIS-
APPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.
For general information on our other products and services please contact our Customer Care Department within
the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are
trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other
countries, and may not be used without written permission. Linux is a registered trademark of Linus Torvalds.
MySQL is a registered trademark of MySQL AB A Company. All other trademarks are the property of their respec-
tive owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be
available in electronic books.
01_59723x ffirs.qxd 10/31/05 6:37 PM Page iv
About the Authors
Jason Gerner currently spends his days working as a web developer in Cincinnati and burns free time
complaining about lack of support for web standards and abusing XML. He can often be found lurking
in the PHPBuilder.com discussion forums, where he is a moderator.
Elizabeth Naramore earned her B.S. in Organizational Behavior from Miami University (Ohio) and
has been developing websites since 1997. Her main focus has been on PHP/MySQL, e-commerce, and
freelance writing and teaching. Her writing can be seen in PHPBuilder.com and International PHP
Magazine. She is a proud member of OINK-PUG (Ohio, Indiana, Northern Kentucky PHP Users Group)
and she lives in Cincinnati, Ohio with her husband and two small children.
After graduating from the University of Auckland with a mathematics degree no one was interested in,
Morgan L. Owens knocked around the IT industry, becoming a backend web developer and program-
mer for web-based applications for both intranet and Internet environments. He still lives in Auckland
but suggests that for the right career he might—might—consider moving.
Matt Warden has been developing web-based applications for over six years. His work has primarily
focused on designing and implementing LAMP applications for other businesses to use internally to
increase productivity. Recently, Matt has been using AJAX to offer a web-based alternative to productiv-
Kristie Rees
Graphics and Production Specialists
Carrie Foster
Lauren Goddard
Denny Hager
Barbara Moore
Alicia B. South
Quality Control Technicians
Amanda Briggs
John Greenough
Jessica Kramer
Joe Niesen
Media Development Specialists
Angie Denny
Kate Jenkins
Steve Kudirka
Kit Malone
Travis Silvers
Media Development Coordinator
Laura Atkinson
Proofreading and Indexing
TECHBOOKS Production Services
01_59723x ffirs.qxd 10/31/05 6:37 PM Page vii
01_59723x ffirs.qxd 10/31/05 6:37 PM Page viii
Acknowledgments
Thanks to my lovely wife for putting up with me for the past few years, and being so supportive. I’d
also like to thank my co-authors for their great work and the time they spent working on this project.
Also, thanks to the folks at Wiley for giving us a chance, and big thanks to Brian for his huge help and
having faith in us.—Jason Gerner
I’d like to thank my parents, who always made me feel cool, even though I am and always will be a geek.
Configuration Changes 12
MySQLi 12
XML Support 16
Tidy Extension 17
SQLite 17
Summary 17
Chapter 2: PHP5 OOP 19
Procedural Programming versus OOP 19
Basic Class Definitions 20
Visibility 23
Constructors and Destructors 25
Static Keyword 27
Class Constants 28
Assignment versus Cloning 29
02_59723x ftoc.qxd 10/31/05 6:35 PM Page xi
xii
Contents
Inheritance and Interfaces 30
Inheritance 31
Interfaces 35
Magic Methods 37
__call 38
__get and __set 38
__sleep 39
__wakeup 40
__toString 43
Summary 45
Chapter 3: More Obscure PHP 47
Array Functions and Callbacks 48
Using Callbacks 48
InnoDB Tables 88
InnoDB Advantages 88
InnoDB Disadvantages 89
Using InnoDB 89
Controlling Access 93
User Administration 93
Server Restriction 100
Analyzing the Database 100
SHOW COLUMNS 101
SHOW CREATE TABLE 101
SHOW DATABASES 102
SHOW GRANTS 102
Database Maintenance 103
Creating Backups 103
Restoring Databases from Backups 103
Summary 104
Chapter 5: PHP Configuration 105
Modifying php.ini 105
Recommended Configuration Directives 105
New to PHP5 107
PHP Configuration during Runtime 108
Obtaining Current Runtime Settings 108
Changing Configuration Dynamically 113
Automated Version and Feature Checking 116
Summary 128
Chapter 6: Apache Tricks 129
URL Rewriting 129
Enabling mod_rewrite 130
RewriteRule 131
RewriteCond 139
Chapter 8: PEAR and PECL 183
What Is PEAR? 183
What Is PECL? 184
Exploring PEAR 184
The PEAR Manager 184
Installing Packages 186
Using Installed Packages 187
Quick and Dirty PEAR Packages 187
What Else Is There? 195
Exploring PECL 196
Fileinfo 196
PDO 196
Xdebug 197
Summary 197
02_59723x ftoc.qxd 10/31/05 6:35 PM Page xiv
xv
Contents
Chapter 9: Code Efficiency 199
Why Bother? 199
A Lot for a Little 200
A Little for a Lot 200
Comparing the Speed of Strings — A Benchmarking Example 201
Unintuitive Results 206
Benchmarking and Profiling 206
PEAR Benchmark 207
top and ab 208
Hardware Improvements 210
Web Server Improvements 211
PHP Improvements 213
Coding Standards 213
History 274
The Image Source Trick 275
Hidden Frames 275
Hidden IFRAME 276
XMLHTTP and XMLHttpRequest 277
The Interfaces 277
Working with the Interfaces 279
Handling the Response 280
AJAX Libraries 282
SAJAX 282
CPAINT 283
JPSPAN 283
When Not to Use AJAX 283
Further Information 285
Summary 285
Chapter 12: Caching Engines 287
Alternative PHP Cache 287
Installing APC 288
Configuring APC 288
Removing APC 290
eAccelerator 290
Installing eAccelerator 291
Configuring eAccelerator 291
Removing eAccelerator 294
Zend Optimizer 294
Installing Zend Optimizer 294
Configuring Zend Optimizer 295
Removing Zend Optimizer 296
JPCache 296
Installing JPCache 296
Appendix B: Alternative Tools 347
Index 355
02_59723x ftoc.qxd 10/31/05 6:35 PM Page xvii
02_59723x ftoc.qxd 10/31/05 6:35 PM Page xviii
Introduction
So what’s all the hubbub surrounding LAMP? What is this “LAMP” and how do I get one? These
questions will be answered within the pages of this book. In this case, LAMP stands for Linux, Apache,
MySQL, and PHP, and has proven to be one of the fastest growing ways to develop enterprise-level web
applications. All these packages are open source, so you are basically free to use them as you wish; the
only limit is your own imagination. In addition, with Open Source technologies you not only have the
best minds in the industry working together to provide great packages, you also have the immense
support of the rest of the open source community if you come across any troubles. After reading this
book, you will have an expanded knowledge of LAMP and can put it to work for you as you develop
your dynamic websites. Because this is currently the only advanced level LAMP book available, and a
part of Wrox’s Professional Series, you can be assured that the information contained within its pages
will help make you a better coder and a step ahead of the rest.
It should be noted that although the term “LAMP” can also include Perl and Python in its broader
definition, we have chosen to focus on PHP in this book.
The goal of this book is to take the beginning to intermediate level web developer one step further,
equipping him or her with a more advanced-level knowledge of each of these modules. We give you the
tools in this book to allow you to think “out of the box” and find new solutions to old problems.
Whom Is This Book For?
This book is for web developers with some experience who want to take their websites to the next level.
If you’re interested in using Open Source tools to run a dynamic, exciting web site, you’ll find what you
need here. We assume that anyone reading this book has some basic experience in the following areas:
❑ Web server technology
❑ OO coding
❑ Database structure, set up, and interaction (retrieving, modifying, and adding data)
❑ Setting up users and permissions
❑ Installing new software
can be applied to Windows users as well. You will need PHP, MySQL, and Apache to do the exercises in
this book. All three are Open Source programs, so you can download and use them free of charge. You
will also need a text editor to enter your code. Finally, you’ll need a web browser, such as Internet
Explorer, Netscape Navigator, Firefox, Safari, or Opera, to view your web pages.
As any programmer knows, software is constantly being improved and debugged, and while we used
the latest and greatest versions of our modules at the time of publishing, chances are those versions
won’t be around for long. It is important for you to visit the source websites for PHP, Apache, and
MySQL to get the most updated versions and recent release notes. When developing websites using
applications, we recommend that you always use the most recent stable release. Using software versions
that have not been fully tested can be dangerous to your application and leave bugs in your code. The
same is true for the new learner—you should be learning on a stable release of the application, not on a
beta version.
The most recent stable versions that were in effect at the time of this book’s writing were:
03_59723x flast.qxd 10/31/05 6:40 PM Page xx
xxi
Introduction
❑ PHP: Version 5.0.4 (available at )
❑ Apache: Version 2.0.54 (available at
)
❑ MySQL: Version 4.1.12 (available at
)
Future editions of this book will address changes and improvements in these programs as they become
available.
Conventions
To help you get the most from the text and keep track of what’s happening, we’ve used a number of con-
ventions throughout the book.
Tips, hints, tricks, and asides to the current discussion are offset and placed in italics like this.
As for styles in the text:
❑ We highlight important words when we introduce them.
❑ We present URLs like so:
and locate the title using the Search box
or one of the title lists. Then, on the book details page, click the Book Errata link. On this page you can view
all errata that has been submitted for this book and posted by Wrox editors. A complete book list including
links to each book’s errata is also available at
www.wrox.com/misc-pages/booklist.shtml.
If you don’t spot “your” error on the Book Errata page, go to www.wrox.com/contact/techsupport
.shtml
and complete the form there to send us the error you have found. We’ll check the information
and, if appropriate, post a message to the book’s errata page and fix the problem in subsequent editions
of the book.
p2p.wrox.com
For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a web-based sys-
tem for you to post messages relating to Wrox books and related technologies and interact with other
readers and technology users. The forums offer a subscription feature to email you topics of interest of
your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts,
and your fellow readers are present on these forums.
At
you will find a number of different forums that will help you not only as you
read this book, but also as you develop your own applications. To join the forums, just follow these steps:
1. Go to p2p.wrox.com and click the Register link.
2. Read the terms of use and click Agree.
3. Complete the required information to join as well as any optional information you wish to pro-
vide and click Submit.
4. You will receive an email with information describing how to verify your account and complete
the joining process.
You can read messages in the forums without joining P2P but in order to post your own messages, you
must join.
Once you join, you can post new messages and respond to messages other users post. You can read
messages at any time on the web. If you would like to have new messages from a particular forum
emailed to you, click the Subscribe to This Forum icon by the forum name in the forum listing.