You are previewing PHP 5 Unleashed.
O'Reilly logo
PHP 5 Unleashed

Book Description

Over 12 million Internet domains worldwide use the PHP language to power their websites. If you are a programmer included in this group, or would like to be one, you should pick up a copy of PHP Unleashed. The definitive guide in PHP programming, PHP Unleashed thoroughly and authoritatively covers the release of PHP 5, as well as advanced topics not found in other books. It begins with a detailed summary of the basics and then quickly moves on to more advanced topics such as working with data, outputting graphical data and building complete applications. Whether you're an experienced programmer and new to PHP, or an experienced PHP programmer but new to PHP 5, this is the one PHP book you need in your library.

Table of Contents

  1. Copyright
    1. Dedication
  2. Lead Author
    1. Contributing Authors
  3. Acknowledgments
  4. We Want to Hear from You!
    1. Reader Services
  5. Introduction
    1. Organization of the Book
  6. I. Working with PHP for General Web Development
    1. 1. Basic PHP Development
      1. How PHP Scripts Work
      2. Basic PHP Syntax
      3. Basic PHP Data Types
      4. Variable Manipulation
      5. Control Structures
        1. Logical Control Structures
        2. Repetition Control Structures
        3. Embedding Control Structures
      6. User-Defined Functions
      7. Dynamic Variables and Functions
        1. Dynamic Variables
        2. Dynamic Functions
      8. Multiple File PHP Scripts
      9. References
        1. Variable References
        2. References Used in Functions
      10. Strings in PHP
        1. Speed and Efficiency of String Expressions
      11. Comparing Strings
      12. Advanced String Comparison
        1. Comparing Phrases
      13. Search and Replacement
        1. Replacing Strings
      14. Formatting Strings
        1. Alternatives to printf()
      15. Strings and Locales
        1. Formatting Currency Values
      16. Formatting Date and Time Values
      17. Summary
    2. 2. Arrays
      1. Basic Arrays
        1. Array Syntax
          1. Multidimensional Arrays
        2. Working with Arrays
          1. Array Traversal
          2. Array Callbacks
      2. Implementing Arrays
        1. Using an Array as a List
        2. Using Arrays as a Sortable Table
        3. Using Arrays as a Lookup Table
        4. Converting from Strings to Arrays and Back
      3. More Array Materials
    3. 3. Regular Expressions
      1. The Basics of Regular Expressions
      2. Limitations of the Basic Syntax
      3. POSIX Regular Expressions
      4. Perl-Compatible Regular Expressions (PCRE)
        1. Named Patterns
      5. PCRE Modifiers
      6. A Few Final Words
    4. 4. Working with Forms in PHP
      1. HTML Forms 101
        1. How Forms Are Created
        2. HTML Widgets
          1. Text and Password Field Widgets
          2. Option Button and Check Box Widgets
          3. File Upload Widget
          4. Lists and Drop-Down Lists
          5. Multiple-Line Text Fields
          6. Hidden Form Values
          7. Submission Widgets and Button Widgets
      2. Working with Form Submissions in PHP
        1. Retrieving Form Values
          1. Data-Access “Gotchas”
        2. Using Arrays as Widget Names
        3. Handling File Uploads
      3. Summary
    5. 5. Advanced Form Techniques
      1. Data Manipulation and Conversion
        1. Dealing with Magic Quotes
        2. Data Conversion and Encoding
          1. Encoding and Decoding Data for URLs
          2. Encoding and Decoding Binary Data
          3. Converting to HTML Entities
        3. Serialization
      2. Form Data Integrity
        1. Securing Hidden Elements
        2. The protect() Function
        3. The validate() Function
        4. Putting protect() and validate() into Action
      3. Form Processing
        1. Basic Form Processing and Validation
        2. General-Purpose Form Validation
        3. Separation of Presentation from Validation
      4. Summary
    6. 6. Persistent Data Using Sessions and Cookies
      1. HTTP Cookies
        1. Cookie Features and Restrictions
        2. How Cookies Are Implemented
        3. Implementing Cookies in Your Scripts
      2. PHP Sessions
        1. Basic Session Use
          1. Starting a Session
          2. Registering Session Variables
          3. Unregistering Session Variables
          4. Destroying Sessions
          5. Working with Session Variables
        2. Session Propagation
      3. Advanced Sessions
        1. Custom Session Handling
        2. Customizing Session Support
      4. Summary
    7. 7. Using Templates
      1. The What and Why of Templates
        1. Separating Common Elements from Code
        2. A (Quick) Template System Example
      2. The Smarty Template Engine
        1. Installing Smarty
        2. Basic Smarty: Variables and Modifiers
        3. Configuration Files and Functions
      3. Summary
  7. II. Advanced Web Development
    1. 8. PEAR
      1. What Is PEAR?
        1. A Code Library
        2. A Coding Standard
        3. A System for Distribution and Maintenance
        4. The PHP Foundation Classes
        5. The PEAR Package Manager
        6. A Diverse Community
      2. Getting and Installing PEAR
        1. On *NIX Systems
        2. On Windows Systems
        3. Through a Web Browser
      3. Using the PEAR Package Manager
        1. Listing Packages
        2. Finding Packages
        3. Installing and Upgrading Packages
        4. Uninstalling Packages
        5. Alternative Installation Methods
      4. Using the PEAR Website
        1. Browsing the Package List
        2. Searching for a Package
        3. Downloading and Installing a Package
      5. Using PEAR Packages in Applications
        1. Setting Up php.ini
        2. Including the Package
        3. Using Packages Not Installed Through pear
      6. Summary
      7. Reference
        1. Mailing Lists/Newsgroups
        2. WWW
        3. Other
    2. 9. XSLT and Other XML Concerns
      1. Relating XML to HTML
      2. Using XSLT to Describe HTML Output Using XML Input
        1. XSL Stylesheets
        2. XSLT File Format Basics
        3. Commonly Used XSLT Instructions
        4. Using XSLT Instruction Elements with XSLT Patterns
        5. Sample XML to HTML Transformation Using XSLT
      3. PHP4 and XSLT Using the DOM XML Module
        1. Sample Transformation Using PHP4 and DOM XML
        2. DOM XML Functions and Properties of Note for XSLT Users
        3. Including XSLT Support in PHP4 via DOM XML
      4. PHP4 and XSLT Using the XSLT Module
        1. Sample Transformation Using PHP4 and XSLT
        2. XSLT Functions and Properties of Note
        3. Including XSLT Support in PHP4 via XSLT
      5. PHP5 and XSLT
        1. Sample Transformation Using PHP5
        2. PHP5 Functions and Properties of Note for XSLT Users
        3. Including XSL Support in PHP5
      6. Accessing XML Data Using SimpleXML
        1. Using SimpleXML in PHP Scripts
        2. Additional Notes About SimpleXML in PHP Scripts
      7. Generating XML Documents Using PHP
        1. Functions and Properties for Storing XML Objects as Files
      8. Summary
      9. References
    3. 10. Debugging and Optimizations
      1. Debugging Your PHP Scripts
        1. Syntax-Related Bugs
        2. Logical Bugs
          1. Preventing Bugs
          2. Simple Script Tracing
          3. Using Assertions in PHP
      2. Optimizing Your PHP Scripts
        1. The Secret to Finding Optimizations—Profiling
        2. Common PHP Bottlenecks and Solutions
          1. Regular Expressions
          2. Invariant Loop Optimization
          3. Output Optimizations
          4. Caching and PHP
          5. Caching Entire Documents
          6. Caching Function Calls
      3. Summary
    4. 11. User Authentication
      1. Authenticating Users in PHP
        1. Why?
        2. Using HTTP Authentication with Apache
        3. Using HTTP Authentication
          1. Using Static Usernames and Passwords
          2. Using Names and Passwords from a Database
        4. Using PHP Sessions
          1. Using Static Usernames and Passwords
          2. Using Usernames and Passwords from a Database
      2. Securing PHP Code
        1. Register_Globals
        2. Maximum Error
        3. Trust No One—Especially Not User Data
        4. Printing User Data
        5. Working with Files
        6. Working with Databases
      3. Summary
    5. 12. Data Encryption
      1. Shared Secret Versus Public Key
      2. Shared Secret Algorithms
        1. Phrase Substitution
        2. Character Substitution
        3. Taking It Further
        4. Stronger Encryption Algorithms
      3. Public Key Cryptography
        1. The RSA Algorithm
        2. Signing Versus Safeguarding
        3. Man in the Middle
      4. Using Public Keys in PHP
        1. SSL Streams
        2. Generating a Public Key Certificate and Private Key
        3. Encrypting/Decrypting Data
        4. Encrypting and Sending Secure Emails Using S/MIME
      5. Summary
    6. 13. Object-Oriented Programming in PHP
      1. Why Objects?
      2. Creating Basic Classes
        1. Private, Protected, and Public
          1. Type Hinting
          2. Cloning
        2. Constructors and Destructors
        3. Class Constants
        4. Static Methods
        5. Class Inheritance
      3. Advanced Classes
        1. Abstract Classes and Methods
        2. Interfaces
        3. Final Classes and Methods
      4. Special Methods
        1. Getters and Setters
        2. The __call() Method
        3. The __toString() Method
      5. Class Autoloading
      6. Object Serialization
      7. Exceptions
        1. Understanding the Call Stack
        2. The Exception Class
        3. Throwing and Catching Exceptions
      8. Iterators
      9. Summary
    7. 14. Error Handling
      1. The PHP Error-Handling Model
        1. Error Types
      2. What to Do About Errors
      3. The Default Error Handler
      4. Error Suppression
      5. Custom Error Handlers
      6. Causing Errors
      7. Putting It All Together
      8. Summary
    8. 15. Working with HTML/XHTML Using Tidy
      1. Introduction
      2. Basic Tidy Usage
        1. Parsing Input and Retrieving Output
        2. Cleaning and Repairing Documents
        3. Identifying Problems Within Documents
      3. Tidy Configuration Options
        1. Tidy Options at Runtime
          1. Reading Configuration Values
        2. Tidy Configuration Files
      4. Using the Tidy Parser
        1. How Documents Are Stored in Tidy
        2. The Tidy Node
      5. Applications of Tidy
        1. Tidy as an Output Buffer
        2. Converting Documents to CSS
        3. Reducing Bandwidth Usage
        4. Beautifying Documents
        5. Extracting URLs from a Document
      6. Summary
    9. 16. Writing Email in PHP
      1. The MIME Protocol
      2. Implementing MIME Email in PHP
        1. The MIMEContainer and MIMESubcontainer Classes
        2. The MIMEAttachment, MIMEContent, and MIMEMessage Classes
      3. Summary
  8. III. Building Applications in PHP
    1. 17. Using PHP for Console Scripting
      1. Core CLI Differences
      2. Working with PHP CLI
        1. Command-Line Arguments and Return Codes
      3. CLI Tools and Extensions
        1. The Readline Extension
        2. Creating User Interfaces
      4. Summary
    2. 18. SOAP and PHP
      1. What Are Web Services?
        1. Transport with SOAP
        2. Description with WSDL
        3. Directory Lookup with UDDI
      2. Installation
      3. Creating Web Services
      4. Consuming Web Services
      5. Looking for Web Services
      6. Summary
    3. 19. Building WAP-Enabled Websites
      1. What Is WAP?
      2. System Requirements
        1. Nokia Mobile Internet Toolkit
        2. Ericsson WapIDE
        3. Openwave SDK
        4. Motorola Wireless IDE/SDK
      3. Introduction to WML
        1. WML Structure
        2. Text
        3. Links
        4. Graphics
        5. WML Forms
          1. Text and Password Fields
          2. Selection Lists, Option Buttons, and Check Boxes
          3. Grouping Form Elements
          4. Processing Form Data
          5. Sending Form Data Server Side
      4. Serving WAP Content
        1. MIME Types
        2. Web Server Configuration
        3. Setting MIME Type from PHP
        4. Client Detection
        5. Displaying Graphics
      5. Sample Applications
        1. Server-Side Form Data Processing
        2. WAP Cinema Reservation System
      6. Summary
  9. IV. I/O, System Calls, and PHP
    1. 20. Working with the File System
      1. Working with Files in PHP
        1. Reading and Writing Text Files
        2. Reading and Writing Binary Files
        3. Working with Directories in PHP
      2. File Permissions
        1. How Unix Permissions Work
        2. Working with Permissions from PHP
      3. File Access Support Functions
        1. Logic Functions
        2. File Manipulation
        3. Specialized File Access
      4. Summary
    2. 21. Network I/O
      1. DNS/Reverse DNS Lookups
        1. Retrieving the DNS Record by IP
        2. Retrieving IP Addresses Based on Hostname
        3. Determining DNS Record Information
      2. Socket Programming
        1. Socket Basics
        2. Creating a New Socket
        3. Dealing with Socket Errors
        4. Creating Client Sockets
        5. Creating Server Sockets
        6. Working with Multiple Sockets at Once
      3. Network Helper Functions
      4. Summary
    3. 22. Accessing the Underlying OS from PHP
      1. Introduction
      2. Unix-Specific OS Functionality
        1. Direct Input and Output (I/O)
          1. Opening and Closing a Direct I/O Connection
          2. Reading Data from the Connection
          3. Writing Data to the Connection
          4. Adjusting the Direct I/O File Pointer
          5. Retrieving Information About the Connection
          6. Configuring Your Direct I/O Connection
        2. PHP POSIX Functions
          1. POSIX and Security in PHP
          2. Knowing When Something Has Gone Wrong
          3. User and Group POSIX Retrieval Functions
          4. Changing the Group or User
          5. POSIX Process Functions
        3. Unix Process Control
          1. Forking Processes in PHP
          2. Catching Signals Sent to Processes
          3. Setting an Alarm Signal
          4. Jumping Processes in PHP
      3. Platform-Independent System Functions
        1. Executing Applications from PHP
        2. Basic External Application Execution
        3. Single-Direction External Command Pipes
        4. Dealing with the System Environment
      4. A Brief Note About Security
      5. Summary
  10. V. Working with Data in PHP
    1. 23. Introduction to Databases
      1. Using the MySQL Client
      2. Basic MySQL Usage
        1. RDBMS Fundamentals
        2. Performing Queries Using SQL
      3. Summary
    2. 24. Using MySQL with PHP
      1. Performing Queries from PHP
        1. MySQLi Basics
          1. Connecting to the Database
          2. Selecting a Database
          3. Performing a Basic Query
          4. Fetching Resultset Rows
          5. Counting Rows and Columns
          6. Freeing Results
          7. Retrieving Error Messages
          8. Closing the Database Connections
        2. Executing Multiple Queries
        3. Creating a Visitor-Tracking System
        4. Prepared Statements
          1. Bound Parameter Statements
          2. Binding Result Values
        5. Transactions
      2. A MySQLi Session Handler
      3. What Is a Custom Session Handler?
        1. Defining Your Own Session Handler
        2. The MySQLi Session Handler
      4. Summary
    3. 25. Using SQLite with PHP
      1. What Makes SQLite Unique?
        1. General Differences Between SQLite and MySQL
          1. SQLite Is Typeless
        2. How SQLite Deals with Textual and Numeric Types
          1. Comparing Two Textual Columns
          2. Comparing Two Numeric Columns
          3. Comparing Two Columns of Conflicting Types
        3. How SQLite Treats NULL Values
        4. Accessing a Database from Multiple Processes
      2. Basic SQLite Functionality
        1. Opening and Closing Databases
        2. Performing Queries
        3. Retrieving Results
          1. Returning Rows as Arrays Sequentially
          2. Returning Entire Resultsets as an Array
          3. Returning a Single Value
          4. Counting Resultsets and Affected Rows
          5. Retrieving Field Names and Column Values
          6. Retrieving the Last Insert ID
        4. Handling Errors
        5. Navigating Resultsets
      3. Working with PHP UDFs in SQLite
        1. Calling PHP Functions in SQL Queries
      4. Odds and Ends
      5. Summary
    4. 26. PHP's dba Functions
      1. Preparations and Settings
      2. Creating a File-Based Database
      3. Writing Data
      4. Reading Data
      5. Sample Application
      6. Conclusion
  11. VI. Graphical Output with PHP
    1. 27. Working with Images
      1. Basic Image Creation Using GD
        1. Retrieving Image information
      2. Using the PHP/GD Drawing Functions
        1. Drawing Line-Based Geometric Shapes
        2. Drawing Curved Surfaces
        3. Filled Shapes and Image Functions
      3. Working with Colors and Brushes
        1. Working with the Image Palette
          1. Matching Colors in an Existing Palette
          2. Removing and/or Modifying Palette Colors
          3. Creating Transparency in Images
          4. Color Transparency Using Alpha Blending
        2. Drawing Using Brushes
          1. Using Custom Brushes for Filling
      4. Using Fonts and Printing Strings
        1. Using GD's Internal Fonts
        2. Using TrueType Fonts
        3. Using Postscript Type 1
      5. General Image Manipulation
        1. Copying One Image to Another
          1. Duplicating Palettes
      6. Other Graphics Functions
        1. EXIF Functions
      7. Summary
    2. 28. Printable Document Generation
      1. A Note Regarding the Examples in This Chapter
      2. Generating Dynamic RTF Documents
      3. Generating Dynamic PDF Documents
        1. The PDFLib Coordinate System
        2. Using PDFLib Configuration Parameters
        3. Generating PDF Documents from Scratch
          1. A Skeleton PDF Document
          2. Rendering Text in a PDF Document
          3. Rendering Shapes in PDF Documents
          4. Adding Colors to PDF Documents
          5. Adding Images to PDF Documents
          6. Manipulating the PDF Document Coordinate System
          7. Defining and Using Patterns
          8. Defining and Using Templates
          9. Setting PDF Publisher Information
      4. Related Resources
  12. VII. Appendixes
    1. A. Installing PHP5 and MySQL
      1. Installing PHP5
        1. Linux
        2. Windows
          1. Apache
          2. Microsoft Web Servers
        3. Mac OS X
      2. Installing MySQL and PHP Modules
        1. Linux
          1. MySQL
          2. PDF Support
          3. XML
          4. DBA extension
          5. GD extension
          6. Data encryption
        2. Windows
          1. MySQL
          2. PDF Support
          3. Support for data encryption
      3. Installing PEAR
    2. B. HTTP Reference
      1. What Is HTTP?
      2. PHP Programming Libraries for HTTP Work
      3. Understanding an HTTP Transaction
      4. HTTP Client Methods
      5. What Comes Back: Server Response Codes
      6. HTTP Headers
      7. Encoding
      8. Identifying Clients and Servers
      9. The “Referer”
      10. Fetching Content from an HTTP Source
      11. Media Types
      12. Cookies: Preserving State and a Tasty Treat
      13. Security and Authorization
      14. Client-Side Caching of HTTP Content
    3. C. Migrating Applications from PHP4 to PHP5
      1. Configuration
      2. Object-Oriented Programming (OOP)
      3. New Behavior of Functions
      4. Further Reading
    4. D. Good Programming Techniques and Performance Issues
      1. Common Style Mistakes
        1. Configuration Directives
        2. PHP Is Forgiving, to a Fault
        3. Reinventing the Wheel
        4. Variables—Use Them, Don't Abuse Them
          1. Tip 1: To Be or Not To Be Variables Is the Question
          2. Tip 2: Nothing Should Be Cryptic
      2. Common Security Concerns
        1. Unintended Consequences
        2. System Calls
        3. Preventing System Call Attacks
        4. Securing File Uploads
      3. Style and Security—Logging
        1. Logging Custom Error Messages
      4. Summary
    5. E. Resources and Mailing Lists
      1. Relevant Websites
      2. Mailing Lists and Newsgroups