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 XSLT Module

The XSLT extension is perhaps the most straightforward way to perform XML to HTML transformations using PHP4. It uses the Sablotron XML toolkit to perform these tasks and is considered a stable (that is, nonexperimental) extension.

Although a few vendors ship PHP4 with XSLT module support compiled in, most users will need to add support themselves if they want to use the XSLT extension. As was the case with DOM XML, code for the XSLT extension was removed from the standard PHP distribution for PHP5.

Sample Transformation Using PHP4 and XSLT

The PHP file shown in Listing 9.5 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 XSLT extension.

Listing 9.5. Sample Transformation File test-xslt.php
1   <?php
2
3       $path_xml = "freedomland.xml";
4       $path_style = "forest.xsl";
5
6       $xslt_parse = xslt_create();
7       if (!$output_html = xslt_process($xslt_parse, $path_xml, $path_style)) {
8           echo "Error using " . $path_style . " on " . $path_xml . "!\n";
9           exit;
10       }
11
12       xslt_free($xslt_parse);
13
14       echo $output_html;
15
16   ?>

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

  • Lines 1 and 16 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.

  • Line 6 creates a new XSLT processor resource that will use the Sablotron library to apply the XSLT stylesheet templates to the input XML file.

  • Line 7 calls the xslt_process function, the central function in the XSLT extension, to apply the XSLT stylesheet template to the input XML file using the processor resource created in line 6. The HTML output is returned to string variable $output_html.

  • Lines 8 and 9 display an error message and end execution if for some reason the processor resource $xslt_parse can't be accessed or the files given by $path_xml or $path_style can't be opened or accessed.

  • Line 12 frees the XSLT processor resource because the script has now finished using it.

  • Line 14 outputs the HTML contents of string variable $output_html for the Web browser.

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

XSLT Functions and Properties of Note

In addition to the tools discussed in Listing 9.5, several functions or properties may be useful to PHP users needing to access XSLT transformations with the Sablotron-based XSLT extension module. These are shown in Table 9.7.

Table 9.7. XSLT Extension Functions of Note

Function

Description

xslt_create()

Creates and returns a resource associated with an XSLT processor; this processor can then be used to apply XSLT transformations.

xslt_error()

Returns a string describing in plain text the last error that occurred on the passed XSLT processor resource.

xslt_process()

Applies an XSLT transformation using the XSLT processor resource passed in the first argument, the XML input file passed by name in the second, and the XSLT stylesheet passed by name in the third. Returns a string containing the HTML output of the transformation.

xslt_set_log()

When passed a processor resource and a Boolean value, enables or disables XSLT processor logging for the passed resource. When passed a processor resource and a filename, directs all messages about the XSLT processor resource in question (if logging is enabled) to the file in question.

xslt_set_error_handler()

Directs XSLT to call the error handling function passed in the second argument whenever an error has occurred with the XSLT processor resource passed in the first argument.

xslt_set_base()

Sets the base URI for all XSLT files passed to the xslt_process() function along with the processor resource provided in the first argument to the URI provided in the second argument.


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

Including XSLT Support in PHP4 via XSLT

If you are using the standard PHP4 distribution from http://www.php.net on a Windows system, you may be able to enable the XSLT extension by making a change to your php.ini configuration file. To do this, open up the file with a text editor such as Notepad and find the following line:

;extension=php_sablot.dll

Remove the leading semicolon on this line so that the line reads as shown:

extension=php_sablot.dll

After you have made this change, save the file and try to use the XSLT extension functions as described. If you can successfully perform XML to HTML transformations this way, you don't need to do anything further to enable the XSLT extension on your system.

If you are not a Windows user, or the technique described fails to produce working XSLT extension support in your PHP4 binary, you will need to recompile PHP4 to include support for the XSLT extension and Sablotron library. To do this, follow these steps:

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

2.
Ensure that you have the Sablotron XML toolkit from http://www.gingerall.com/charlie/ga/xml/d_sab.xml.

3.
Compile PHP with the following additional arguments: --with-xslt and --with-xslt-sablot.

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