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 the PEAR Package Manager

The PEAR Package Manager is not a necessary part of PEAR, but it is an important one, and it's part of what defines PEAR, as shown earlier. It streamlines the process of downloading packages from PEAR and checks for package dependencies. It places the package files in their correct locations and even helps developers make packages of their own for use in PEAR.

For the most part, the developer functionality of the PPM is not necessary for the average user of PEAR classes, so those commands and options are omitted from the following descriptions. What follows are basic commands for managing a local installation of PEAR, from listing packages to installing and removing them.

Listing Packages

To see what PEAR packages are installed, enter the following at a command prompt (assuming pear is in the PATH):

pear list

This command outputs a listing that looks similar to this:

Installed packages:
===================
Package        Version State
Archive_Tar    1.1     stable
Console_Getopt 1.2     stable
PEAR           1.3.3.1 stable
XML_RPC        1.1.0   stable

If no other packages have been installed other than those included by default, the packages in the listing will be those considered current PFC packages. Another similar listing is provided by the pear list-all command, which shows all packages in PEAR and notes the current version number of those packages installed on the local system. If it's not installed, no version number is listed in the "Local" column.

Finding Packages

There are many packages in the repository, and many more in the proposal stages. Although the initial PEAR installation starts out with a small set of packages, chances are that you'll need other packages. The PPM has a useful tool for searching by package name, aptly named search.

Although a search by package name may be performed at http://pear.php.net, the PPM provides the same functionality. For example, consider a Web-based application that requires some form of page-caching mechanism. Rather than build such a mechanism from scratch, first check the PEAR repository for a cache package by entering the following at a command prompt:

pear search cache

This will return results similar to the following:

Matched packages:
=================
Package    Stable/(Latest) Local
Cache      1.5.4                 Framework for caching of arbitrary data.
Cache_Lite 1.3                   Fast and Safe little cache system
memcache   1.1                   memcached extension

According to this listing, there are three packages that deal with caching; however, not much else is given in the way of helpful information about these packages. To retrieve useful information about a package, use the remote-info command: pear remote-info Cache. This will return a detailed listing about the package, including its maintainers, release date, license type, status, and more.

NOTE

By default, the PPM's master_server parameter is set to pear.php.net. This means that the PPM will use that server to check for and download packages. This can be changed with the pear config-set command. This chapter assumes the use of pear.php.net as the master server in all examples.


Installing and Upgrading Packages

The PPM may be used to install PEAR packages directly from the code repository. They do not even need to be downloaded from the website; the PPM takes care of this. In addition, the pear install command checks for package dependencies before proceeding with the install. If necessary, it will fail as it does in the following example:

$> pear install Cache
downloading Cache-1.5.4.tgz ...
Starting to download Cache-1.5.4.tgz (30,690 bytes)
.........done: 30,690 bytes
requires package `HTTP_Request'
Cache: Dependencies failed

The PPM does not download and install the package's dependencies by default. You may choose to install each dependency separately, or you may add the a option to download and install all required and optional dependencies or the o option to download and install required dependencies only.

$> pear install o Cache
downloading Cache-1.5.4.tgz ...
Starting to download Cache-1.5.4.tgz (30,690 bytes)
.........done: 30,690 bytes
downloading HTTP_Request-1.2.3.tgz ...
Starting to download HTTP_Request-1.2.3.tgz (12,823 bytes)
...done: 12,823 bytes
downloading Net_URL-1.0.14.tgz ...
Starting to download Net_URL-1.0.14.tgz (5,173 bytes)
...done: 5,173 bytes
install ok: Net_URL 1.0.14
install ok: HTTP_Request 1.2.3
install ok: Cache 1.5.4

As seen in the preceding listing, not only is HTTP_Request downloaded as a required dependency of Cache, but Net_URL is downloaded as a dependency of HTTP_Request. When the install is a success, the PPM will note it with an appropriate message:

install ok: Cache 1.5.4

Similar to the install command is upgrade. PEAR packages are in a state of constant development, so there are often upgrades to packages. To check for package upgrades, type pear list-upgrades. If an upgrade exists, the PPM will show both the local version of the package and the current (upgrade) version in the repository.

Available Upgrades (stable):
============================
Package     Local        Remote       Size
Archive_Tar 1.1 (stable) 1.2 (stable) 14.5kB

The preceding listing shows that an upgrade exists for Archive_Tar. To download and install this upgrade, type pear upgrade Archive_Tar. The process for downloading and checking dependencies is similar to that of the pear install command:

$> pear upgrade Archive_Tar
downloading Archive_Tar-1.2.tgz ...
Starting to download Archive_Tar-1.2.tgz (14,792 bytes)
.....done: 14,792 bytes
upgrade ok: Archive_Tar 1.2

As an alternative, there is also the pear upgrade-all command, which downloads and installs all available upgrades.

Uninstalling Packages

Sometimes it may be necessary to remove a package from the local PEAR installation. For this case, the PPM includes an uninstall command:

$> pear uninstall Cache
uninstall ok: Cache

If uninstall is a success, it displays a message to that effect. Otherwise, if other packages are dependent upon the one being removed, a dependency message will be displayed. If the dependency is considered optional, it will proceed with the removal. If it is a required dependency, uninstall will fail.

NOTE

Be aware that the PPM does not know whether the code in applications on the machine uses a PEAR package. It tests only known package dependencies. If a package is an optional dependency or has no dependencies, it will remove it regardless of whether any code uses it.


Alternative Installation Methods

The pear install command is versatile. It does not need to connect to pear.php.net to download packages. Because PEAR is a standard of coding and a system for distribution, other developers may develop packages according to the PEAR package standards and release them separately from the main repository. For the most part, these developers end up submitting a proposal to the PEAR Proposal system (PEPr), so there are few places outside of PEAR where these packages may be found, but they are therejust do a search for them.

To install any package that is not included in the PEAR database or to install packages by an alternative method, either download the gzipped file, or use the URL to install it. The install command works the same in both ways:

pear install package.tgz
pear install http://example.net/path/to/package

You may also unzip the gzipped file and install the package using its package.xml file:

pear install /path/to/package.xml

This is particularly helpful to developers for testing packages or for preparing packages for use with other package managers such as RPM Package Manager (RPM).

NOTE

For the sake of example, the Horde project has one such package that is not part of PEAR, although it follows the package conventions, so it may be easily installed via pear install.

The file is located at http://pear.horde.org. To download and install the Horde_VFS package that is located there, either download the gzipped file and install it locally or execute the following:

pear install http://pear.horde.org/Horde_VFS


TIP

To see all available PPM actions, type pear help. To see the help file for a particular command, type pear help command_name.