PHP Cookbook, 2nd Edition


book cover
PHP Cookbook, 2nd Edition
By David Sklar, Adam Trachtenberg
...............................................
Publisher: O'Reilly
Pub Date: August 2006
Print ISBN-10: 0-596-10101-5
Print ISBN-13: 978-0-59-610101-5
Pages: 810
 

Table of Contents  | Index
overview

When it comes to creating dynamic web sites, the open source PHP language is red-hot property: used on more than 20 million web sites today, PHP is now more popular than Microsoft's ASP.NET technology. With our Cookbook's unique format, you can learn how to build dynamic web applications that work on any web browser. This revised new edition makes it easy to find specific solutions for programming challenges.


PHP Cookbook has a wealth of solutions for problems that you'll face regularly. With topics that range from beginner questions to advanced web programming techniques, this guide contains practical examples -- or "recipes" -- for anyone who uses this scripting language to generate dynamic web content. Updated for PHP 5, this book provides solutions that explain how to use the new language features in detail, including the vastly improved object-oriented capabilities and the new PDO data access extension. New sections on classes and objects are included, along with new material on processing XML, building web services with PHP, and working with SOAP/REST architectures. With each recipe, the authors include a discussion that explains the logic and concepts underlying the solution.



book cover
PHP Cookbook, 2nd Edition
By David Sklar, Adam Trachtenberg
...............................................
Publisher: O'Reilly
Pub Date: August 2006
Print ISBN-10: 0-596-10101-5
Print ISBN-13: 978-0-59-610101-5
Pages: 810
 

Table of Contents  | Index

   Copyright
   Preface
        Chapter 1.  Strings
      Section 1.0.  Introduction
      Recipe 1.1.  Accessing Substrings
      Recipe 1.2.  Extracting Substrings
      Recipe 1.3.  Replacing Substrings
      Recipe 1.4.  Processing a String One Byte at a Time
      Recipe 1.5.  Reversing a String by Word or Byte
      Recipe 1.6.  Expanding and Compressing Tabs
      Recipe 1.7.  Controlling Case
      Recipe 1.8.  Interpolating Functions and Expressions Within Strings
      Recipe 1.9.  Trimming Blanks from a String
      Recipe 1.10.  Generating Comma-Separated Data
      Recipe 1.11.  Parsing Comma-Separated Data
      Recipe 1.12.  Generating Fixed-Width Field Data Records
      Recipe 1.13.  Parsing Fixed-Width Field Data Records
      Recipe 1.14.  Taking Strings Apart
      Recipe 1.15.  Wrapping Text at a Certain Line Length
      Recipe 1.16.  Storing Binary Data in Strings
      Recipe 1.17.  Program: Downloadable CSV File
        Chapter 2.  Numbers
      Section 2.0.  Introduction
      Recipe 2.1.  Checking Whether a Variable Contains a Valid Number
      Recipe 2.2.  Comparing Floating-Point Numbers
      Recipe 2.3.  Rounding Floating-Point Numbers
      Recipe 2.4.  Operating on a Series of Integers
      Recipe 2.5.  Generating Random Numbers Within a Range
      Recipe 2.6.  Generating Biased Random Numbers
      Recipe 2.7.  Taking Logarithms
      Recipe 2.8.  Calculating Exponents
      Recipe 2.9.  Formatting Numbers
      Recipe 2.10.  Formatting Monetary Values
      Recipe 2.11.  Printing Correct Plurals
      Recipe 2.12.  Calculating Trigonometric Functions
      Recipe 2.13.  Doing Trigonometry in Degrees, Not Radians
      Recipe 2.14.  Handling Very Large or Very Small Numbers
      Recipe 2.15.  Converting Between Bases
      Recipe 2.16.  Calculating Using Numbers in Bases Other Than Decimal
      Recipe 2.17.  Finding the Distance Between Two Places
        Chapter 3.  Dates and Times
      Section 3.0.  Introduction
      Recipe 3.1.  Finding the Current Date and Time
      Recipe 3.2.  Converting Time and Date Parts to an Epoch Timestamp
      Recipe 3.3.  Converting an Epoch Timestamp to Time and Date Parts
      Recipe 3.4.  Printing a Date or Time in a Specified Format
      Recipe 3.5.  Finding the Difference of Two Dates
      Recipe 3.6.  Finding the Difference of Two Dates with Julian Days
      Recipe 3.7.  Finding the Day in a Week, Month, or Year
      Recipe 3.8.  Validating a Date
      Recipe 3.9.  Parsing Dates and Times from Strings
      Recipe 3.10.  Adding to or Subtracting from a Date
      Recipe 3.11.  Calculating Time with Time Zones
      Recipe 3.12.  Accounting for Daylight Savings Time
      Recipe 3.13.  Generating a High-Precision Time
      Recipe 3.14.  Generating Time Ranges
      Recipe 3.15.  Using Non-Gregorian Calendars
      Recipe 3.16.  Using Dates Outside the Range of an Epoch Timestamp
      Recipe 3.17.  Program: Calendar
        Chapter 4.  Arrays
      Section 4.0.  Introduction
      Recipe 4.1.  Specifying an Array Not Beginning at Element 0
      Recipe 4.2.  Storing Multiple Elements Per Key in an Array
      Recipe 4.3.  Initializing an Array to a Range of Integers
      Recipe 4.4.  Iterating Through an Array
      Recipe 4.5.  Deleting Elements from an Array
      Recipe 4.6.  Changing Array Size
      Recipe 4.7.  Appending One Array to Another
      Recipe 4.8.  Turning an Array into a String
      Recipe 4.9.  Printing an Array with Commas
      Recipe 4.10.  Checking if a Key Is in an Array
      Recipe 4.11.  Checking if an Element Is in an Array
      Recipe 4.12.  Finding the Position of a Value in an Array
      Recipe 4.13.  Finding Elements That Pass a Certain Test
      Recipe 4.14.  Finding the Largest or Smallest Valued Element in an Array
      Recipe 4.15.  Reversing an Array
      Recipe 4.16.  Sorting an Array
      Recipe 4.17.  Sorting an Array by a Computable Field
      Recipe 4.18.  Sorting Multiple Arrays
      Recipe 4.19.  Sorting an Array Using a Method Instead of a Function
      Recipe 4.20.  Randomizing an Array
      Recipe 4.21.  Removing Duplicate Elements from an Array
      Recipe 4.22.  Applying a Function to Each Element in an Array
      Recipe 4.23.  Finding the Union, Intersection, or Difference of Two Arrays
      Recipe 4.24.  Making an Object Act like an Array
      Recipe 4.25.  Program: Printing a Horizontally Columned HTML Table
        Chapter 5.  Variables
      Section 5.0.  Introduction
      Recipe 5.1.  Avoiding == Versus = Confusion
      Recipe 5.2.  Establishing a Default Value
      Recipe 5.3.  Exchanging Values Without Using Temporary Variables
      Recipe 5.4.  Creating a Dynamic Variable Name
      Recipe 5.5.  Using Static Variables
      Recipe 5.6.  Sharing Variables Between Processes
      Recipe 5.7.  Encapsulating Complex Data Types in a String
      Recipe 5.8.  Dumping Variable Contents as Strings
        Chapter 6.  Functions
      Section 6.0.  Introduction
      Recipe 6.1.  Accessing Function Parameters
      Recipe 6.2.  Setting Default Values for Function Parameters
      Recipe 6.3.  Passing Values by Reference
      Recipe 6.4.  Using Named Parameters
      Recipe 6.5.  Creating Functions That Take a Variable Number of Arguments
      Recipe 6.6.  Returning Values by Reference
      Recipe 6.7.  Returning More Than One Value
      Recipe 6.8.  Skipping Selected Return Values
      Recipe 6.9.  Returning Failure
      Recipe 6.10.  Calling Variable Functions
      Recipe 6.11.  Accessing a Global Variable Inside a Function
      Recipe 6.12.  Creating Dynamic Functions
        Chapter 7.  Classes and Objects
      Section 7.0.  Introduction
      Recipe 7.1.  Instantiating Objects
      Recipe 7.2.  Defining Object Constructors
      Recipe 7.3.  Defining Object Destructors
      Recipe 7.4.  Implementing Access Control
      Recipe 7.5.  Preventing Changes to Classes and Methods
      Recipe 7.6.  Defining Object Stringification
      Recipe 7.7.  Specifying Interfaces
      Recipe 7.8.  Creating Abstract Base Classes
      Recipe 7.9.  Assigning Object References
      Recipe 7.10.  Cloning Objects
      Recipe 7.11.  Overriding Property Accesses
      Recipe 7.12.  Calling Methods on an Object Returned by Another Method
      Recipe 7.13.  Aggregating Objects
      Recipe 7.14.  Accessing Overridden Methods
      Recipe 7.15.  Using Method Polymorphism
      Recipe 7.16.  Defining Class Constants
      Recipe 7.17.  Defining Static Properties and Methods
      Recipe 7.18.  Controlling Object Serialization
      Recipe 7.19.  Introspecting Objects
      Recipe 7.20.  Checking if an Object Is an Instance of a Specific Class
      Recipe 7.21.  Autoloading Class Files upon Object Instantiation
      Recipe 7.22.  Instantiating an Object Dynamically
      Recipe 7.23.  Program: whereis
        Chapter 8.  Web Basics
      Section 8.0.  Introduction
      Recipe 8.1.  Setting Cookies
      Recipe 8.2.  Reading Cookie Values
      Recipe 8.3.  Deleting Cookies
      Recipe 8.4.  Redirecting to a Different Location
      Recipe 8.5.  Detecting Different Browsers
      Recipe 8.6.  Building a Query String
      Recipe 8.7.  Reading the Post Request Body
      Recipe 8.8.  Generating HTML Tables with Alternating Row Styles
      Recipe 8.9.  Using HTTP Basic or Digest Authentication
      Recipe 8.10.  Using Cookie Authentication
      Recipe 8.11.  Flushing Output to the Browser
      Recipe 8.12.  Buffering Output to the Browser
      Recipe 8.13.  Compressing Web Output
      Recipe 8.14.  Reading Environment Variables
      Recipe 8.15.  Setting Environment Variables
      Recipe 8.16.  Communicating Within Apache
      Recipe 8.17.  Program: Web Site Account (De)activator
      Recipe 8.18.  Program: Tiny Wiki
        Chapter 9.  Form
      Section 9.0.  Introduction
      Recipe 9.1.  Processing Form Input
      Recipe 9.2.  Validating Form Input: Required Fields
      Recipe 9.3.  Validating Form Input: Numbers
      Recipe 9.4.  Validating Form Input: Email Addresses
      Recipe 9.5.  Validating Form Input: Drop-Down Menus
      Recipe 9.6.  Validating Form Input: Radio Buttons
      Recipe 9.7.  Validating Form Input: Checkboxes
      Recipe 9.8.  Validating Form Input: Dates and Times
      Recipe 9.9.  Validating Form Input: Credit Cards
      Recipe 9.10.  Preventing Cross-Site Scripting
      Recipe 9.11.  Working with Multipage Forms
      Recipe 9.12.  Redisplaying Forms with Inline Error Messages
      Recipe 9.13.  Guarding Against Multiple Submission of the Same Form
      Recipe 9.14.  Processing Uploaded Files
      Recipe 9.15.  Preventing Global Variable Injection
      Recipe 9.16.  Handling Remote Variables with Periods in Their Names
      Recipe 9.17.  Using Form Elements with Multiple Options
      Recipe 9.18.  Creating Drop-Down Menus Based on the Current Date
        Chapter 10.  Database Access
      Section 10.0.  Introduction
      Recipe 10.1.  Using DBM Databases
      Recipe 10.2.  Using an SQLite Database
      Recipe 10.3.  Connecting to an SQL Database
      Recipe 10.4.  Querying an SQL Database
      Recipe 10.5.  Retrieving Rows Without a Loop
      Recipe 10.6.  Modifying Data in an SQL Database
      Recipe 10.7.  Repeating Queries Efficiently
      Recipe 10.8.  Finding the Number of Rows Returned by a Query
      Recipe 10.9.  Escaping Quotes
      Recipe 10.10.  Logging Debugging Information and Errors
      Recipe 10.11.  Creating Unique Identifiers
      Recipe 10.12.  Building Queries Programmatically
      Recipe 10.13.  Making Paginated Links for a Series of Records
      Recipe 10.14.  Caching Queries and Results
      Recipe 10.15.  Accessing a Database Connection Anywhere in Your Program
      Recipe 10.16.  Program: Storing a Threaded Message Board
        Chapter 11.  Sessions and Data Persistence
      Section 11.0.  Introduction
      Recipe 11.1.  Using Session Tracking
      Recipe 11.2.  Preventing Session Hijacking
      Recipe 11.3.  Preventing Session Fixation
      Recipe 11.4.  Storing Sessions in a Database
      Recipe 11.5.  Storing Sessions in Shared Memory
      Recipe 11.6.  Storing Arbitrary Data in Shared Memory
      Recipe 11.7.  Caching Calculated Results in Summary Tables
        Chapter 12.  XML
      Section 12.0.  Introduction
      Recipe 12.1.  Generating XML as a String
      Recipe 12.2.  Generating XML with the DOM
      Recipe 12.3.  Parsing Basic XML Documents
      Recipe 12.4.  Parsing Complex XML Documents
      Recipe 12.5.  Parsing Large XML Documents
      Recipe 12.6.  Extracting Information Using XPath
      Recipe 12.7.  Transforming XML with XSLT
      Recipe 12.8.  Setting XSLT Parameters from PHP
      Recipe 12.9.  Calling PHP Functions from XSLT Stylesheets
      Recipe 12.10.  Validating XML Documents
      Recipe 12.11.  Handling Content Encoding
      Recipe 12.12.  Reading RSS and Atom Feeds
      Recipe 12.13.  Writing RSS Feeds
      Recipe 12.14.  Writing Atom Feeds
        Chapter 13.  Web Automation
      Section 13.0.  Introduction
      Recipe 13.1.  Fetching a URL with the Get Method
      Recipe 13.2.  Fetching a URL with the Post Method
      Recipe 13.3.  Fetching a URL with Cookies
      Recipe 13.4.  Fetching a URL with Arbitrary Headers
      Recipe 13.5.  Fetching a URL with an Arbitrary Method
      Recipe 13.6.  Fetching a URL with a Timeout
      Recipe 13.7.  Fetching an HTTPS URL
      Recipe 13.8.  Debugging the Raw HTTP Exchange
      Recipe 13.9.  Marking Up a Web Page
      Recipe 13.10.  Cleaning Up Broken or Nonstandard HTML
      Recipe 13.11.  Extracting Links from an HTML File
      Recipe 13.12.  Converting Plain Text to HTML
      Recipe 13.13.  Converting HTML to Plain Text
      Recipe 13.14.  Removing HTML and PHP Tags
      Recipe 13.15.  Responding to an Ajax Request
      Recipe 13.16.  Integrating with JavaScript
      Recipe 13.17.  Program: Finding Stale Links
      Recipe 13.18.  Program: Finding Fresh Links
        Chapter 14.  Consuming Web Services
      Section 14.0.  Introduction
      Recipe 14.1.  Calling a REST Method
      Recipe 14.2.  Calling a SOAP Method with WSDL
      Recipe 14.3.  Calling a SOAP Method Without WSDL
      Recipe 14.4.  Debugging SOAP Requests
      Recipe 14.5.  Using Complex SOAP Types
      Recipe 14.6.  Setting SOAP Types
      Recipe 14.7.  Using SOAP Headers
      Recipe 14.8.  Using Authentication with SOAP
      Recipe 14.9.  Redefining an Endpoint
      Recipe 14.10.  Catching SOAP Faults
      Recipe 14.11.  Mapping XML Schema Data Types to PHP Classes
      Recipe 14.12.  Calling an XML-RPC Method
      Recipe 14.13.  Using Authentication with XML-RPC
        Chapter 15.  Building Web Services
      Section 15.0.  Introduction
      Recipe 15.1.  Serving a REST Method
      Recipe 15.2.  Serving a SOAP Method
      Recipe 15.3.  Accepting Arguments in a SOAP Method
      Recipe 15.4.  Generating WSDL Automatically
      Recipe 15.5.  Throwing SOAP Faults
      Recipe 15.6.  Processing a SOAP Header
      Recipe 15.7.  Generating a SOAP Header
      Recipe 15.8.  Using Authentication with SOAP
      Recipe 15.9.  Serving an XML-RPC Method
        Chapter 16.  Internet Services
      Section 16.0.  Introduction
      Recipe 16.1.  Sending Mail
      Recipe 16.2.  Sending MIME Mail
      Recipe 16.3.  Reading Mail with IMAP or POP3
      Recipe 16.4.  Posting Messages to Usenet Newsgroups
      Recipe 16.5.  Reading Usenet News Messages
      Recipe 16.6.  Getting and Putting Files with FTP
      Recipe 16.7.  Looking Up Addresses with LDAP
      Recipe 16.8.  Using LDAP for User Authentication
      Recipe 16.9.  Performing DNS Lookups
      Recipe 16.10.  Checking if a Host Is Alive
      Recipe 16.11.  Getting Information About a Domain Name
        Chapter 17.  Graphics
      Section 17.0.  Introduction
      Recipe 17.1.  Drawing Lines, Rectangles, and Polygons
      Recipe 17.2.  Drawing Arcs, Ellipses, and Circles
      Recipe 17.3.  Drawing with Patterned Lines
      Recipe 17.4.  Drawing Text
      Recipe 17.5.  Drawing Centered Text
      Recipe 17.6.  Building Dynamic Images
      Recipe 17.7.  Getting and Setting a Transparent Color
      Recipe 17.8.  Reading EXIF Data
      Recipe 17.9.  Serving Images Securely
      Recipe 17.10.  Program: Generating Bar Charts from Poll Results
        Chapter 18.  Security and Encryption
      Section 18.0.  Introduction
      Recipe 18.1.  Preventing Session Fixation
      Recipe 18.2.  Protecting Against Form Spoofing
      Recipe 18.3.  Ensuring Input Is Filtered
      Recipe 18.4.  Avoiding Cross-Site Scripting
      Recipe 18.5.  Eliminating SQL Injection
      Recipe 18.6.  Keeping Passwords Out of Your Site Files
      Recipe 18.7.  Storing Passwords
      Recipe 18.8.  Dealing with Lost Passwords
      Recipe 18.9.  Verifying Data with Hashes
      Recipe 18.10.  Encrypting and Decrypting Data
      Recipe 18.11.  Storing Encrypted Data in a File or Database
      Recipe 18.12.  Sharing Encrypted Data with Another Web Site
      Recipe 18.13.  Detecting SSL
      Recipe 18.14.  Encrypting Email with GPG
        Chapter 19.  Internationalization and Localization
      Section 19.0.  Introduction
      Recipe 19.1.  Listing Available Locales
      Recipe 19.2.  Using a Particular Locale
      Recipe 19.3.  Setting the Default Locale
      Recipe 19.4.  Localizing Text Messages
      Recipe 19.5.  Localizing Dates and Times
      Recipe 19.6.  Localizing Currency Values
      Recipe 19.7.  Localizing Images
      Recipe 19.8.  Localizing Included Files
      Recipe 19.9.  Managing Localization Resources
      Recipe 19.10.  Using gettext
      Recipe 19.11.  Setting the Character Encoding of Outgoing Data
      Recipe 19.12.  Setting the Character Encoding of Incoming Data
      Recipe 19.13.  Manipulating UTF-8 Text
        Chapter 20.  Error Handling, Debugging, and Testing
      Section 20.0.  Introduction
      Recipe 20.1.  Finding and Fixing Parse Errors
      Recipe 20.2.  Creating Your Own Exception Classes
      Recipe 20.3.  Printing a Stack Trace
      Recipe 20.4.  Reading Configuration Variables
      Recipe 20.5.  Setting Configuration Variables
      Recipe 20.6.  Hiding Error Messages from Users
      Recipe 20.7.  Tuning Error Handling
      Recipe 20.8.  Using a Custom Error Handler
      Recipe 20.9.  Logging Errors
      Recipe 20.10.  Eliminating "headers already sent" Errors
      Recipe 20.11.  Logging Debugging Information
      Recipe 20.12.  Using a Debugger Extension
      Recipe 20.13.  Writing a Unit Test
      Recipe 20.14.  Writing a Unit Test Suite
      Recipe 20.15.  Applying a Unit Test to a Web Page
      Recipe 20.16.  Setting Up a Test Environment
        Chapter 21.  Performance Tuning and Load Testing
      Section 21.0.  Introduction
      Recipe 21.1.  Timing Function Execution
      Recipe 21.2.  Timing Program Execution
      Recipe 21.3.  Profiling with a Debugger Extension
      Recipe 21.4.  Stress Testing Your Web Site
      Recipe 21.5.  Avoiding Regular Expressions
      Recipe 21.6.  Using an Accelerator
        Chapter 22.  Regular Expressions
      Recipe 22.0.  Introduction
      Recipe 22.1.  Switching from ereg to preg
      Recipe 22.2.  Matching Words
      Recipe 22.3.  Finding the nth Occurrence of a Match
      Recipe 22.4.  Choosing Greedy or Nongreedy Matches
      Recipe 22.5.  Finding All Lines in a File That Match a Pattern
      Recipe 22.6.  Capturing Text Inside HTML Tags
      Recipe 22.7.  Preventing Parentheses from Capturing Text
      Recipe 22.8.  Escaping Special Characters in a Regular Expression
      Recipe 22.9.  Reading Records with a Pattern Separator
      Recipe 22.10.  Using a PHP Function in a Regular Expression
        Chapter 23.  Files
      Recipe 23.0.  Introduction
      Recipe 23.1.  Creating or Opening a Local File
      Recipe 23.2.  Creating a Temporary File
      Recipe 23.3.  Opening a Remote File
      Recipe 23.4.  Reading from Standard Input
      Recipe 23.5.  Reading a File into a String
      Recipe 23.6.  Counting Lines, Paragraphs, or Records in a File
      Recipe 23.7.  Processing Every Word in a File
      Recipe 23.8.  Picking a Random Line from a File
      Recipe 23.9.  Randomizing All Lines in a File
      Recipe 23.10.  Processing Variable-Length Text Fields
      Recipe 23.11.  Reading Configuration Files
      Recipe 23.12.  Modifying a File in Place Without a Temporary File
      Recipe 23.13.  Flushing Output to a File
      Recipe 23.14.  Writing to Standard Output
      Recipe 23.15.  Writing to Many Filehandles Simultaneously
      Recipe 23.16.  Escaping Shell Metacharacters
      Recipe 23.17.  Passing Input to a Program
      Recipe 23.18.  Reading Standard Output from a Program
      Recipe 23.19.  Reading Standard Error from a Program
      Recipe 23.20.  Locking a File
      Recipe 23.21.  Reading and Writing Custom File Types
      Recipe 23.22.  Reading and Writing Compressed Files
        Chapter 24.  Directories
      Recipe 24.0.  Introduction
      Recipe 24.1.  Getting and Setting File Timestamps
      Recipe 24.2.  Getting File Information
      Recipe 24.3.  Changing File Permissions or Ownership
      Recipe 24.4.  Splitting a Filename into Its Component Parts
      Recipe 24.5.  Deleting a File
      Recipe 24.6.  Copying or Moving a File
      Recipe 24.7.  Processing All Files in a Directory
      Recipe 24.8.  Getting a List of Filenames Matching a Pattern
      Recipe 24.9.  Processing All Files in a Directory Recursively
      Recipe 24.10.  Making New Directories
      Recipe 24.11.  Removing a Directory and Its Contents
      Recipe 24.12.  Program: Web Server Directory Listing
      Recipe 24.13.  Program: Site Search
        Chapter 25.  Command-Line PHP
      Section 25.0.  Introduction
      Recipe 25.1.  Parsing Program Arguments
      Recipe 25.2.  Parsing Program Arguments with getopt
      Recipe 25.3.  Reading from the Keyboard
      Recipe 25.4.  Running PHP Code on Every Line of an Input File
      Recipe 25.5.  Reading Passwords
      Recipe 25.6.  Program: Command Shell
        Chapter 26.  PEAR and PECL
      Section 26.0.  Introduction
      Recipe 26.1.  Using the PEAR Installer
      Recipe 26.2.  Finding PEAR Packages
      Recipe 26.3.  Finding Information About a Package
      Recipe 26.4.  Installing PEAR Packages
      Recipe 26.5.  Upgrading PEAR Packages
      Recipe 26.6.  Uninstalling PEAR Packages
      Recipe 26.7.  Installing PECL Packages
   Colophon
   Index