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

PHP5 and XSLT

With the improved XML handling in PHP5, PHP4 modules like DOM XML and XSLT have been rendered obsolete, or at least deprecated. In PHP5, XSLT transformations are incorporated more cleanly into the features of its XML, DOM, and XSL extensions.

Because of this, the amount of flexibility available to PHP developers in working with XML, XSL, and HTML files in PHP5 is greatly increased; all of these separate file types are manipulated with the same, more generalized toolkit.

Sample Transformation Using PHP5

The PHP file shown in Listing 9.6 demonstrates the simplest way to perform an XSLT transformation on the sample files shown in Listing 9.1 and Listing 9.2 using the PHP5 DOM, XML, and XSLT extensions.

Listing 9.6. Sample Transformation File test-php5.php
1   <?php
2
3       $path_xml = "freedomland.xml";
4       $path_style = "forest.xsl";
5
6       $xml_obj = new DomDocument;
7       $xsl_obj = new DomDocument;
8
9       if (!$xml_obj->load($path_xml)) {
10           echo "Error! Unable to open " . $path_xml . "!\n";
11           exit;
12       }
13
14       if (!$xsl_obj->load($path_style)) {
15           echo "Error! Unable to open " . $path_style . "!\n";
16           exit;
17       }
18
19       $xslt_parse = new xsltprocessor;
20
21       $xslt_parse->importStyleSheet($xsl_obj);
22
23       echo $xslt_parse->transformToXML($xml_obj);
24
25   ?>

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 25 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.

  • Lines 6 and 7 create new DomDocument objects, capable of holding and manipulating well-formed XML documents (including XHTML and XSLT documents, which are also generally well-formed XML documents). These will be used to hold the XML input file and the XSLT stylesheet, respectively.

  • Line 9 calls the load() property of the $xml_obj DomDocument object to load the XML file given by the $path_xml variable defined in line 3.

  • Lines 10 and 11 display an error message and end execution if for some reason the XML input file can't be loaded or processed.

  • Line 14 calls the load() property of the $xsl_obj DomDocument object to load the XSLT file given by the $path_style variable defined in line 4.

  • Lines 15 and 16 display an error message and end execution if for some reason the XSLT input file can't be loaded or processed.

  • Line 19 creates a new XSLT processor resource at $xslt_parse, which can then be used for XSLT transformations.

  • Line 21 calls the importStyleSheet() property of the $xslt_parse resource to parse the XML document stored in $xsl_obj as an XSLT stylesheet in particular.

  • Line 23 calls the transformToXML() property of the $xslt_parse resource to apply the parsed XSLT stylesheet to the XML tree in $xml_obj; because the transformToXML() property returns a string in this case (the transformed HTML document), the echo command has been called to output this string to the Web browser.

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

PHP5 Functions and Properties of Note for XSLT Users

In addition to the tools discussed in Listing 9.6, several additional functions or properties may be useful to PHP users needing to access XML documents and XSLT transformations in PHP5. These are shown in Tables 9.8 and 9.9.

Table 9.8. DOM Extension Properties of Note

Function

Description

load()

Loads a well-formed XML tree from the passed XML input file into a DomDocument object.

loadXML()

Loads a well-formed XML tree from the passed string (containing XML-formatted data) into a DomDocument object.

save()

Saves the XML tree stored in a DomDocument object back into a text file under the passed path name.

validate()

Validates the document tree stored in a DomDocument object based on the document's declared XML Document Type Definition (DTD).


Table 9.9. XSL Extension Properties of Note

Function

Description

importStyleSheet()

Parses the passed XML object tree as an XSL stylesheet to be used in transforming the XML document.

transformToXML()

Uses an already imported XSL stylesheet to transform the passed XML document. Returns a string containing the transformed XML (for this book's purposes, THML) output, suitable for echo to and subsequent rendering in a Web browser.


Additional details on these and other functions and properties related to the PHP5 DOM, XML, and XSL modules can be found by visiting the documentation for each extension at these links:

Including XSL Support in PHP5

The DOM and XML extension modules are built and included by default with PHP5. If you find that you are unable to use the XSL functions or properties in conjunction with these extensions, you will need to recompile your PHP5 installation to add support for XSL processing and transformations. To do so, follow these steps:

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

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

3.
Compile the PHP5 binary using the additional configuration option --with-xsl=xslt_library_path, where xslt_library_path is the location of the XSLT library.

For more information on PHP5, DOM, XML, and XSL, refer to individual extension documentation and the PHP5 migration documentation and notes, all available at the official PHP website via the link at http://www.php.net/manual/en/.