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

PHP4 and XSLT Using the DOM XML Module

The DOM XML module remained an experimental (and somewhat interface-unstable) extension throughout the lifespan of PHP4 and has been deprecated as of PHP5. In spite of this, it remains a relatively popular module and is shipped by several operating system maintainers, including, for example, Red Hat, Inc., in its Enterprise Linux and Fedora Core operating systems.

The DOM XML module is used to parse and manipulate XML input files in their entirety, as a single large data tree. For this reason, DOM XML may at times be slower or more resource intensive when transforming large XML files than the other PHP modules that can be used for XSLT transformations. Note also that the DOM XML interface described here is the post-PHP4.3.0 interfacenot the earlier interface.

Sample Transformation Using PHP4 and DOM XML

The PHP file shown in Listing 9.4 demonstrates the simplest way to perform an XSLT transformation on the sample files shown in Listing 9.1 and Listing 9.2 using the PHP4 DOM XML extension.

Listing 9.4. Sample Transformation File test-domxml.php
1   <?php
2
3       $path_xml = "freedomland.xml";
4       $path_style = "forest.xsl";
5
6       if(!$xml_doc = domxml_open_file($path_xml)) {
7           echo "Error! Unable to open " . $path_xml . "!\n";
8           exit;
9       }
10
11       if(!$stylesheet = domxml_xslt_stylesheet_file($path_style)) {
12           echo "Error! Unable to open " . $path_style . "!\n";
13           exit;
14       }
15
16       $transformed = $stylesheet->process($xml_doc);
17
18       echo $stylesheet->result_dump_mem($transformed);
19
20   ?>

Although this document is simple enough that many PHP users will understand it with little or no trouble, a brief walk-through will clarify its flow for those less familiar with PHP scripts.

  • Lines 1 and 20 begin and end the PHP script and should at this point need little further explanation.

  • Lines 3 and 4 create and define variables to hold the names of the input XML file and the XSLT stylesheet, respectively. Note that on Windows platforms, these must be absolute pathnames, although on other platforms they can be relative (as shown).

  • Line 6 attempts to create a new DomDocument object, $xml_doc, that will contain and operate on the XML tree in the sample XML file.

  • Lines 7 and 8 display an error message and end execution if the XML file can't be opened or parsed or the object $xml_doc can't be created.

  • Line 11 attempts to create a new DomXsltStylesheet object, $stylesheet, that will contain and operate on the XML tree in the sample XSLT stylesheet file.

  • Lines 12 and 13 display an error message and end execution if the XSLT file can't be opened or parsed or if the object $stylesheet can't be created.

  • Line 16 applies the XSLT transformation on the $xml_doc object tree using the stylesheet contained in the $stylesheet object tree; the results of the transformation are held in a new object, $transformed.

  • Line 18 uses the $stylesheet object tree to dump the results of the transformation held in $transformed back into a string containing HTML data; this string is then output by echo.

Using other PHP skills you have already acquired, you should be able to incorporate these tools easily into more complex scripts.

DOM XML Functions and Properties of Note for XSLT Users

In addition to the tools discussed in Listing 9.4, several additional functions or properties may be useful to PHP users needing to access XSLT transformations with the DOM XML module. These are shown in Tables 9.5 and 9.6.

Table 9.5. DomDocument Functions and Properties of Note

Property

Description

domxml_open_file()

Returns a new object holding the entire object tree of the supplied XML file.

domxml_open_mem()

Returns a new object holding the entire object tree of the XML data contained in the supplied string.


Table 9.6. DomXsltStylesheet Functions and Properties of Note

Property

Description

domxml_xslt_stylesheet_file()

Returns a new object containing the XSLT transformation instructions in the supplied file.

domxml_xslt_stylesheet_doc()

Returns a new object containing the XSLT transformation instructions in the supplied DomDocument XML object tree.

domxml_xslt_stylesheet()

Returns a new object containing the XSLT transformation instructions from the XSLT object tree contained in the supplied string.

process()

Returns a new object containing the transformed data from the supplied DomDocument XML object tree.

result_dump_mem()

Returns a new string containing the HTML data resulting from the supplied transformation.

result_dump_file()

Returns a new string containing the HTML data resulting from the supplied transformation; also creates a new file containing this string, using the filename given as its second argument.


Additional details on these and other functions and properties related to the PHP4 DOM XML module can be found by visiting the documentation at http://www.php.net/domxml.

Including XSLT Support in PHP4 via DOM XML

Depending on the operating system and PHP version you select or use, the DOM XML module and related functions and objects may or may not be present in your PHP binary.

To build PHP with support for the DOM XML module and related functions and objects, you will need to do all of the following:

  • Obtain the latest PHP4 source code from its home at http://www.php.net.

  • Ensure that you have the GNOME XML library (libxml) from http://www.xmlsoft.org.

  • Ensure that you have the XSLT library (libxslt) from http://www.xmlsoft.org/XSLT.

  • Ensure that you have the EXSLT extensions from http://www.exslt.org.

  • Compile PHP with the following additional arguments: --with-dom=path-to-dom (usually found in the ext directory of the source tree), --with-xslt=path-to-xslt, and --with-exslt=path-to-exslt.

For additional details on compiling and installing PHP4 with DOM XML support, visit http://www.php.net/manual/en/ref.domxml.php.