PHP has earned its place as one of the premiere web scripting languages and is used in everything from small utility scripts to object-oriented enterprise applications. This book covers that entire spectrum, offering hacks focusing on everything from HTML and Ajax to code generation and database-driven message queuing.
We have written code and chosen authors from the cutting edge of web development, application development, graphics, and multimedia. Dynamic HTML is covered extensively, offering your users an interactive experience on the web page without having to watch a browser refresh; you’ll learn how to generate Flash movies on the fly; you’ll even see how to use PHP for database access, web services, and much more.
The book offers more than just canned solutions. It offers ideas and techniques that you can use in your own applications. And why stop there? We encourage you to take the ideas we’ve presented here and extend them, hacking our hacks, taking your scripts and classes even further.
Why PHP Hacks?
The term hacking has a bad reputation in the press. They use it to refer to someone who breaks into systems or wreaks havoc with computers as their weapon. Among people who write code, though, the term hack refers to a “quick-and-dirty” solution to a problem, or a clever way to get something done. And the term hacker is taken very much as a compliment, referring to someone as being creative, having the technical chops to get things done. The Hacks series is an attempt to reclaim the word, document the good ways people are hacking, and pass the hacker ethic of creative participation on to the uninitiated. Seeing how others approach systems and problems is often the quickest way to learn about a new technology.
How to Use This Book
You can read this book from cover to cover if you like, but each hack stands on its own, so feel free to browse and jump to the different sections that interest you most. If there’s a prerequisite you need to know about, a cross-reference will guide you to the right hack.
How This Book Is Organized
The book is divided into several chapters, organized by subject:
- Chapter 1, Installation and Basics
This chapter walks you through the basics of installing PHP and MySQL, as well as using the excellent PEAR library.
- Chapter 2, Web Design
In this chapter, we cover how to use HTML tricks in conjunction with PHP to jazz up your interface.
- Chapter 3, DHTML
- Chapter 4, Graphics
This chapter shows a wide variety of methods that you can use to display data in a graphical form.
- Chapter 5, Databases and XML
Databases are critical to PHP applications. In this chapter, we show you how to make flexible database objects and even to build your database layer automatically using code generation.
- Chapter 6, Application Design
In this chapter, we take the coverage up a notch and discuss techniques that you can use to develop applications quickly and reliably.
- Chapter 7, Patterns
C++, C#, and Java programmers have used design patterns for years. Can you use them in PHP as well? You betcha. This chapter shows how to use several of the design patterns from the original Design Patterns book (Addison Wesley) to make better PHP applications.
- Chapter 8, Testing
Do you stay awake at night thinking about whether your PHP application is still running? This chapter covers testing techniques that will find bugs for you and continuously monitor the operation of your site.
- Chapter 9, Alternative UIs
In this chapter, we show the use of different user interfaces to work with your PHP code. You can run PHP applications on the desktop, from your cell phone, and from your instant messaging application.
- Chapter 10, Fun Stuff
In this chapter, we let it all hang out and use the fun stuff on the Web to monitor multiplayer games, use Google Maps in our applications, and much more.
I often see several problems with PHP applications, and this book helps address a number of these:
- Bad database design
Most PHP applications work with a relational database, usually MySQL. Database design is not something that comes easily to most engineers trained in traditional programming languages. The first step in cleaning up an application is to make sure the database design is good [Hack #34] .
- Poor database use
PHP has provided several different variations on how to access databases, and doing this poorly can cause serious security problems. Fixing the database access layer can start with migrating to PEAR DB or PDO [Hack #35] . After that, you can see if it’s possible to generate the SQL [Hack #41] , the SELECT code [Hack #42] , or the CRUD code [Hack #37] .
- Code embedded in the page
The next problem I see a lot is code that’s embedded directly in the page. In particular, access to the database is embedded right in the code of the page. The code-generating hacks that build SELECT code [Hack #42] and CRUD code [Hack #37] show proper two-tier design with PHP. The SQL dynamic object hack [Hack #36] can also help factor SQL access code off the page.
- Processing during the page build
Another issue I see is applications that attempt to do a lot of processing in the web server during the page fetch. An example is when an application needs to send out a lot of email in response to some user action. Often this is done in the page that responds to the user action—which leaves the user waiting for a page to come back while the system is sending out a bunch of mail. A message queue [Hack #50] is one elegant solution to this problem.
- No testing
- Provide better user security
Most people use the same password on most or all of their accounts. If your application stores passwords in the clear and your application is compromised, you are giving your users’ passwords to the world. Use MD5 to scramble people’s passwords [Hack #59] . And use a roles system [Hack #58] to make sure users don’t see things they shouldn’t see.
- Make better use of patterns
I admit that the term design patterns has been overplayed. But there is a lot of good stuff there that has been underutilized in PHP applications. All of Chapter 7 is dedicated to showing how you can use design patterns pragmatically and effectively to make better PHP applications.
These are just a few ideas about how to upgrade your existing web application to something that is reliable and secure. But how about cool?
Making a Cool Web Application
You can do so much in browsers and with browser plug-ins, DHTML, and Ajax that it’s not hard to have an application that is cool, is easy to use, and has cutting-edge features. Here are some ideas taken from various parts of the book:
- Work from the desktop
Believe it or not, you can use PHP to make desktop applications [Hack #87] so that the same business logic code that runs on your web server can be used right on the desktop. Better yet, this code is portable between Mac OS X, Windows, and Linux, with little (and often no) code rewrite.
- Use maps
- Graph it dynamically
Graphics and graphing are always popular, and there are so many ways to do graphing in PHP. You can use basic HTML [Hack #8] , SVG [Hack #28] , Dynamic HTML [Hack #22] , and the GD library [Hack #31] . I’ve also included information on how to build an object-oriented layer on top of the graphics library [Hack #29] .
- Work with your users’ applications
Another way to create a more compelling application is to reach out to your users. RSS [Hack #88] has become a very popular way to do that. I even take RSS to your PlayStation Portable [Hack #90] . And you can use more traditional routes like email [Hack #10] . I’ve also included some information on how to generate Word RTF documents [Hack #48] and Excel spreadsheets [Hack #49] , as well as how to take Word [Hack #47] or Excel documents [Hack #45] as input.
- Upgrade your web interface
I also include some ideas for generating dynamic menus [Hack #17] and easily creating breadcrumb trails [Hack #4] , tabs [Hack #6] , skinnable interfaces [Hack #3] , pop ups [Hack #12] , stickies [Hack #16] , drag-and-drop interfaces [Hack #13] , calendars [Hack #25] , link graphs [Hack #24] , and more that will make your web interface the best it can be.
These are just a few ideas from the pages of this book. Dig right in and find out ways to take your PHP to the cutting edge.
Conventions Used in This Book
The following is a list of the typographical conventions used in this book:
Used to indicate URLs, filenames and extensions, and directory/folder names. For example, a path in the filesystem would appear as /Developer/Applications.
Used to show code examples, the contents of files, and console output, as well as the names of variables, commands, and other code excerpts.
Constant width bold
Used to highlight portions of code, typically new additions to old code.
Constant width italic
Used in code examples and tables to show sample text to be replaced with your own values.
- Gray type
Used to indicate a cross-reference within the text.
You should pay special attention to notes set apart from the text with the following icons:
This is a tip, suggestion, or general note. It contains useful supplementary information about the topic at hand.
This is a warning or note of caution, often indicating that your money or your privacy might be at risk.
The thermometer icons, found next to each hack, indicate the relative complexity of the hack:
Using Code Examples
This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: PHP Hacks by Jack D. Herrington. Copyright 2006 O’Reilly Media, Inc., 0-596-10139-2.”
If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at firstname.lastname@example.org.
How to Contact Us
We have tested and verified the information in this book to the best of our ability, but you may find that features have changed (or even that we have made mistakes!). As a reader of this book, you can help us to improve future editions by sending us your feedback. Please let us know about any errors, inaccuracies, bugs, misleading or confusing statements, and typos that you find anywhere in this book.
Please also let us know what we can do to make this book more useful to you. We take your comments seriously and will try to incorporate reasonable suggestions into future editions. You can write to us at:
|O’Reilly Media, Inc.|
|1005 Gravenstein Highway North|
|Sebastopol, CA 95472|
|(800) 998-9938 (in the U.S. or Canada)|
|(707) 829-0515 (international/local)|
|(707) 829-0104 (fax)|
To ask technical questions or to comment on the book, send email to:
The web site for PHP Hacks lists examples, errata, and plans for future editions. You can find this page at:
For more information about this book and others, see the O’Reilly web site:
Got a Hack?
To explore Hacks books online or to contribute a hack for future titles, visit:
When you see a Safari® Enabled icon on the cover of your favorite technology book, that means the book is available online through the O’Reilly Network Safari Bookshelf.
Safari offers a solution that’s better than e-books. It’s a virtual library that lets you easily search thousands of top tech books, cut and paste code samples, download chapters, and find quick answers when you need the most accurate, current information. Try it for free at http://safari.oreilly.com.