More Books
PHP 5 Unleashed
PHP 5 Unleashed
Table of Contents
Copyright
Lead Author
Contributing Authors
Acknowledgments
We Want to Hear from You!
Reader Services
Introduction
Organization of the Book
Part I. Working with PHP for General Web Development
Chapter 1. Basic PHP Development
How PHP Scripts Work
Basic PHP Syntax
Basic PHP Data Types
Variable Manipulation
Control Structures
User-Defined Functions
Dynamic Variables and Functions
Multiple File PHP Scripts
References
Strings in PHP
Comparing Strings
Advanced String Comparison
Search and Replacement
Formatting Strings
Strings and Locales
Formatting Date and Time Values
Summary
Chapter 2. Arrays
Basic Arrays
Implementing Arrays
More Array Materials
Chapter 3. Regular Expressions
The Basics of Regular Expressions
Limitations of the Basic Syntax
POSIX Regular Expressions
Perl-Compatible Regular Expressions (PCRE)
PCRE Modifiers
A Few Final Words
Chapter 4. Working with Forms in PHP
HTML Forms 101
Working with Form Submissions in PHP
Summary
Chapter 5. Advanced Form Techniques
Data Manipulation and Conversion
Form Data Integrity
Form Processing
Summary
Chapter 6. Persistent Data Using Sessions and Cookies
HTTP Cookies
PHP Sessions
Advanced Sessions
Summary
Chapter 7. Using Templates
The What and Why of Templates
The Smarty Template Engine
Summary
Part II. Advanced Web Development
Chapter 8. PEAR
What Is PEAR?
Getting and Installing PEAR
Using the PEAR Package Manager
Using the PEAR Website
Using PEAR Packages in Applications
Summary
Reference
Chapter 9. XSLT and Other XML Concerns
Relating XML to HTML
Using XSLT to Describe HTML Output Using XML Input
PHP4 and XSLT Using the DOM XML Module
PHP4 and XSLT Using the XSLT Module
PHP5 and XSLT
Accessing XML Data Using SimpleXML
Generating XML Documents Using PHP
Summary
References
Chapter 10. Debugging and Optimizations
Debugging Your PHP Scripts
Optimizing Your PHP Scripts
Summary
Chapter 11. User Authentication
Authenticating Users in PHP
Securing PHP Code
Summary
Chapter 12. Data Encryption
Shared Secret Versus Public Key
Shared Secret Algorithms
Public Key Cryptography
Using Public Keys in PHP
Summary
Chapter 13. Object-Oriented Programming in PHP
Why Objects?
Creating Basic Classes
Advanced Classes
Special Methods
Class Autoloading
Object Serialization
Exceptions
Iterators
Summary
Chapter 14. Error Handling
The PHP Error-Handling Model
What to Do About Errors
The Default Error Handler
Error Suppression
Custom Error Handlers
Causing Errors
Putting It All Together
Summary
Chapter 15. Working with HTML/XHTML Using Tidy
Introduction
Basic Tidy Usage
Tidy Configuration Options
Using the Tidy Parser
Applications of Tidy
Summary
Chapter 16. Writing Email in PHP
The MIME Protocol
Implementing MIME Email in PHP
Summary
Part III. Building Applications in PHP
Chapter 17. Using PHP for Console Scripting
Core CLI Differences
Working with PHP CLI
CLI Tools and Extensions
Summary
Chapter 18. SOAP and PHP
What Are Web Services?
Installation
Creating Web Services
Consuming Web Services
Looking for Web Services
Summary
Chapter 19. Building WAP-Enabled Websites
What Is WAP?
System Requirements
Introduction to WML
Serving WAP Content
Sample Applications
Summary
Part IV. I/O, System Calls, and PHP
Chapter 20. Working with the File System
Working with Files in PHP
File Permissions
File Access Support Functions
Summary
Chapter 21. Network I/O
DNS/Reverse DNS Lookups
Socket Programming
Network Helper Functions
Summary
Chapter 22. Accessing the Underlying OS from PHP
Introduction
Unix-Specific OS Functionality
Platform-Independent System Functions
A Brief Note About Security
Summary
Part V. Working with Data in PHP
Chapter 23. Introduction to Databases
Using the MySQL Client
Basic MySQL Usage
Summary
Chapter 24. Using MySQL with PHP
Performing Queries from PHP
A MySQLi Session Handler
What Is a Custom Session Handler?
Summary
Chapter 25. Using SQLite with PHP
What Makes SQLite Unique?
Basic SQLite Functionality
Working with PHP UDFs in SQLite
Odds and Ends
Summary
Chapter 26. PHP's dba Functions
Preparations and Settings
Creating a File-Based Database
Writing Data
Reading Data
Sample Application
Conclusion
Part VI. Graphical Output with PHP
Chapter 27. Working with Images
Basic Image Creation Using GD
Using the PHP/GD Drawing Functions
Working with Colors and Brushes
Using Fonts and Printing Strings
General Image Manipulation
Other Graphics Functions
Summary
Chapter 28. Printable Document Generation
A Note Regarding the Examples in This Chapter
Generating Dynamic RTF Documents
Generating Dynamic PDF Documents
Related Resources
Part VII. Appendixes
Appendix A. Installing PHP5 and MySQL
Installing PHP5
Installing MySQL and PHP Modules
Installing PEAR
Appendix B. HTTP Reference
What Is HTTP?
PHP Programming Libraries for HTTP Work
Understanding an HTTP Transaction
HTTP Client Methods
What Comes Back: Server Response Codes
HTTP Headers
Encoding
Identifying Clients and Servers
The "Referer"
Fetching Content from an HTTP Source
Media Types
Cookies: Preserving State and a Tasty Treat
Security and Authorization
Client-Side Caching of HTTP Content
Appendix C. Migrating Applications from PHP4 to PHP5
Configuration
Object-Oriented Programming (OOP)
New Behavior of Functions
Further Reading
Appendix D. Good Programming Techniques and Performance Issues
Common Style Mistakes
Common Security Concerns
Style and SecurityLogging
Summary
Appendix E. Resources and Mailing Lists
Relevant Websites
Mailing Lists and Newsgroups
Index
SYMBOL
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z

Using PEAR Packages in Applications

The true beauty of PEAR is the use of its packages in PHP applications. Likened to Perl's CPAN, PEAR behaves similarly. The packages exist in a central location on the machine running the PHP applicationhowever, this is not necessary, and an alternative method is discussed in "Using Packages Not Installed Through pear." Because the packages (class scripts) are centrally located on the machine, PHP can easily find them for use in any application running locally.

Setting Up php.ini

To use PEAR packages installed through the use of the pear command (and therefore located in the directory set up for PEARusually /usr/local/lib/php or C:\php\PEAR, depending on the system), the only change to be made to php.ini is to set the include_path variable and point it to the correct directory.

For *NIX systems, this may look something like this:

include_path = ".:/usr/local/lib/php"

On Windows systems, it may look like this:

include_path = ".;C:\php\PEAR"

Notice the use of the dot (.) at the beginning of the path. This dot tells PHP to include the current working directory (of any running script) in the include path, as well as any other directories that may be specified in include_path.

TIP

To specify other directories in *NIX, use a colon (:) to separate the values; on Windows, use a semicolon (;).


Including the Package

Including the PEAR package is simple. Following the PEAR Coding Standardsthough it is not necessary to adhere to these standards in code that is not being developed for PEARuse require_once() to unconditionally include a PEAR package; use include_once() to conditionally include one.

require_once 'DB.php';

The name PEAR::DB itself suggests it is at the top level of PEAR. Therefore, no further path is required to include it. As long as the include_path in php.ini correctly points to the PEAR installation, all is well. If the included package were PEAR::Net_SMTP, the statement may look like this:

require_once 'Net/SMTP.php';

NOTE

Because require_once() and include_once() are PHP language statements and not functions, the parentheses are not required.


Using Packages Not Installed Through pear

Sometimes it may be necessary to use a PEAR package from directly within an application. Such an example is an application intended for distribution. In this case, the PEAR installation on multiple platforms cannot be relied upon. It may not have the correct packages installed, or it may not be installed at all. In a case like this, the package need not be installed through the PPM as discussed earlier.

PEAR packages are made to live together, so to speak, in a specific directory structure that is defined in the package.xml file accompanying every package. For the most part, this XML file goes unnoticed to those installing a package through the PPM. However, it is this very file that tells the PPM how to install the package and how it relates to other packages in the way of dependencies. For this reason, every PEAR developer is required to include package.xml in the package release.

If installing a package without the use of the PPM and from within the structure of an application, this file may prove useful. For example, assuming that all PEAR packages in an application will be installed off the root of the application in the lib/pear directory (a fictional directory for an imaginary application), download the package from the PEAR website (as described earlier) and unzip it.

Now, open package.xml to look at the package's dependencies and file structure. Without going into too much detail on how this file is arranged, note that the areas of greatest concern are the <deps> and <filelist> tags.

The <deps> tag for PEAR::DB gives the following information:

<deps>
  <dep type="php" rel="ge" version="4.2.0"/>
  <dep type="pkg" rel="ge" version="1.0b1">PEAR</dep>
</deps>

For PEAR::DB, two dependencies are identified and the relationships are noted as ge, meaning that the versions of these items must be greater-than-or-equal-to that in the version attribute. Thus, to use PEAR::DB, PHP 4.2.0 or greater must be installed, as well as PEAR 1.0b1 or greater.

Because PEAR::DB is being installed apart from the PPM, there is no dependency check; it is up to the developer to ensure that PEAR >= 1.0b1 is installed in a location that DB can find.

The <filelist> tag holds the other important information for installing a PEAR package. It describes the file structure of the package in relation to the other packages in PEAR. Continuing with the PEAR::DB example, the <filelist> tag displays the following:

<filelist>
  <file role="php" baseinstalldir="/" name="DB.php"/>
  <file role="php" name="DB\common.php"/>
  <file role="php" name="DB\dbase.php"/>
  <file role="php" name="DB\fbsql.php"/>
  <file role="php" name="DB\ibase.php"/>
  <file role="php" name="DB\ifx.php"/>
  <file role="php" name="DB\msql.php"/>
  <file role="php" name="DB\mssql.php"/>
  <file role="php" name="DB\mysql.php"/>
  <file role="php" name="DB\mysqli.php"/>
  <file role="php" name="DB\oci8.php"/>
  <file role="php" name="DB\odbc.php"/>
  <file role="php" name="DB\pgsql.php"/>
  <file role="php" name="DB\sybase.php"/>
  <file role="php" name="DB\storage.php"/>
  <file role="php" name="DB\sqlite.php"/>
  ...
</filelist>

Notice that the role of these files is "php." These are the files that are important to install for the package. Other common roles include "doc" and "test," which can be excluded from the application, but can be helpful in learning how the package works. Following the structure given in <filelist> and given the lib/pear base location of all PEAR packages for this application, DB.php would go to lib/pear, whereas all the other files in the DB package would go to lib/pear/DB.

Finally, now that the files have a home and the dependencies have been satisfied, the best method for configuring the application to correctly use the PEAR packages is to modify the include_path from the application level. This will ensure that the code within the PEAR packages can find and include any files upon which it is dependent. To do this, place the following code at the top of all files that will use any of the PEAR packages, or place it in a global configuration file:

$include_path = ini_get('include_path') . PATH_SEPARATOR . '/path/to/lib/pear';
ini_set('include_path', $include_path);

This will add the application's lib/pear directory to the include_path so that the PEAR packages may be included as described in the section "Including the Package."

TIP

To find out more about the package.xml file definition, visit http://pear.php.net/manual/en/developers.packagedef.php.