The PHP Anthology 101 Essential Tips, Tricks, and Hacks, 2Nd Edition, Published by Sitepoint
Total Page:16
File Type:pdf, Size:1020Kb
'REYSCALE #-9+ 0ANTONE ALL SOURCE CODE AVAILABLE FOR DOWNLOAD Black 50% 0 100, 53, O, CMYK Orange 021 C PANTONE 0ANTONE SAVE TIME AND FRUSTRATION WITH Black 100% CMYK 100, 45, 0, 37 2955 C PANTONE #-9+ THIS COMPREHENSIVE COLLECTION OF READY-TO-USE PHP 5 SOLUTIONS! 'REYSCALE The PHP Anthology: 101 Essential Tips, Tricks & Hacks, PHP ‘ALL STAR TEAM’ PHP 2nd Edition is a collection of powerful PHP 5 solutions to the most common programming problems. benbalbo.com THE PHP BEN BALBO PANTONE Orange 021 C PANTONE 2955 C Five world-class developers guide you through the CMYK O, 53, 100, 0 CMYK 100, 45, 0, 37 Black 50% Black 100% capabilities of PHP using countless examples of best- weierophinney.net/matthew/ ANTHOLOGY practice programming. All solutions are fully explained and MATTHEW WEIER 101 ESSENTIAL TIPS, TRICKS & HACKS the ready-to-use code is available for download. O’PHINNEY BY DAVEY SHAFIK This is a must-have companion for any PHP coder looking to khankennels.com/blog/ MATTHEW WEIER O’PHINNEY LIGAYA LIGAYA TURMELLE dive into more complex PHP 5 solutions. TURMELLE HARRY FUECKS Manage errors gracefully. BEN BALBO pixelated-dreams.com 101 ESSENTIAL TIPS, TRICKS & HACKS Build functional forms, tables, and SEO-friendly URLs. DAVEY 2ND EDITION Reduce load time with client- and server-side caching. SHAFIK ANTHOLOGY Produce and utilize web services with XML. Secure your site using access control systems. phppatterns.com HARRY phppatterns.com Easily work with files, emails, and images. FUECKS DAVEY SHAFIK THE PHP And much more… SITEPOINT BOOKS WEB PROGRAMMING ISBN: 978-0-9758419-9-0 Advocate best practice techniques Lead you through practical examples Provide working code for your web site Make learning easy and fun SOLUTIONS TO THE MOST COMMON PROGRAMMING PROBLEMS USD $39.95 CAD $51.95 Visit us on the Web at sitepoint.com or for sales and support email [email protected] SHAFIK, FUECKS ET AL. coverphpant2.indd 1 6/27/2008 1:45:25 PM The PHP Anthology: 101 Essential Tips, Tricks and Hacks, 2nd Edition (Chapters 2, 10, and 11) Thank you for downloading these sample chapters of The PHP Anthology 101 Essential Tips, Tricks, and Hacks, 2nd Edition, published by SitePoint. This excerpt includes the Summary of Contents, Information about the Author, Editors and SitePoint, Table of Contents, Preface, three chapters from the book, and the index. We hope you find this information useful in evaluating this book. For more information, visit sitepoint.com Summary of Contents of this Excerpt Preface..............................................................................................................................xi .. 2. Using Databases with PDO...................................................................................39 . 10. Access Control.....................................................................................................269 . 11. Caching..................................................................................................................363 . Index..............................................................................................................................505 .. Summary of Additional Book Contents 1. Introduction..............................................................................................................39 3. Strings.........................................................................................................................77 . 4. Dates and Times.......................................................................................................95 . 5. Forms, Tables, and Pretty URLs.........................................................................115 . 6. Working with Files................................................................................................147 . 7. Email...........................................................................................................................179 8. Images........................................................................................................................197 9. Error Handling.........................................................................................................237 12. XML and Web Services......................................................................................395 . 13. Best Practices........................................................................................................435 A. PHP Configuration...............................................................................................473 . B. Hosting Provider Checklist.................................................................................483 C. Security Checklist.................................................................................................489 . D. Working with PEAR...............................................................................................497 THE PHP ANTHOLOGY 101 ESSENTIAL TIPS, TRICKS & HACKS BY DAVEY SHAFIK MATTHEW WEIER O’PHINNEY LIGAYA TURMELLE HARRY FUECKS BEN BALBO 2ND EDITION iv The PHP Anthology: 101 Essential Tips, Tricks & Hacks by Davey Shafik, Matthew Weier O’Phinney, Ligaya Turmelle, Harry Fuecks, and Ben Balbo Copyright © 2007 SitePoint Pty. Ltd. Expert Reviewer: Jason Sweat Editor: Georgina Laidlaw Managing Editor: Simon Mackie Editor: Hilary Reynolds Technical Editor: Andrew Tetlaw Index Editor: Fred Brown Technical Director: Kevin Yank Cover Design: Alex Walker Printing History: First Edition: December, 2003 Second Edition: October, 2007 Notice of Rights All rights reserved. No part of this book may be reproduced, stored in a retrieval system or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Notice of Liability The author and publisher have made every effort to ensure the accuracy of the information herein. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors and SitePoint Pty. Ltd., nor its dealers or distributors will be held liable for any damages to be caused either directly or indirectly by the instructions contained in this book, or by the software or hardware products described herein. Trademark Notice Rather than indicating every occurrence of a trademarked name as such, this book uses the names only in an editorial fashion and to the benefit of the trademark owner with no intention of infringement of the trademark. Published by SitePoint Pty. Ltd. 424 Smith Street Collingwood VIC Australia 3066 Web: www.sitepoint.com Email: [email protected] ISBN 978-0-9758419-9-0 Printed and bound in the United States of America v Ben Balbo Ben Balbo was born in Germany, grew up in the UK, lives in Melbourne, and likes Guinness. While he isn’t drinking Guinness (which is most of the time in Melbourne, as it just doesn’t taste the same), he earns a living as a PHP developer and trainer, security consultant, and Open Source developer. He has been known to talk in public about web development-related topics, which comes as part of the package of being on the committees of both the Melbourne PHP User Group and Open Source Developers’ Club. Although he wouldn’t admit this, he participates at this level only in order to go to restaurants or pubs after the meetings. Harry Fuecks Harry Fuecks1 is a technical writer, programmer, and system engineer. He has worked in corporate IT since 1994, having completed a Bachelor’s degree in Physics. He first came across PHP in 1999, while putting together a small intranet. Today, he’s the lead developer of a corporate extranet, where PHP plays an important role in delivering a unified platform for numerous back office systems. In his off hours he writes technical articles for SitePoint and runs phpPatterns,2 a site exploring PHP application design. Originally from the United Kingdom, he now lives in Switzerland. Harry is the proud father of a beautiful baby girl who keeps him busy all day (and night!). Davey Shafik Davey Shafik is a full-time PHP developer with ten years’ experience in PHP and related technologies. An avid magazine writer, book author, and speaker, Davey keeps his mind sharp by trying to tackle problems from a unique perspective from his home in Central Florida where he lives with five cats and more computers. Ligaya Turmelle Ligaya Turmelle is a full-time goddess, occasional PHP programmer, and obsessive world traveler. Actively involved with the PHP community as a founding Principal of phpwomen.org, administrator at codewalkers.com, roving reporter for the Developer Zone on Zend.com, and PHP blogger and long-time busybody of #phpc on freenode, she hopes to one day actually meet the people she talks to. When not sitting at her computer staring at the screen, Ligaya can usually be found either playing golf, scuba diving, snorkeling, kayaking, hiking, or just playing with the dogs outside. Ligaya Turmelle is a Zend Certified Engineer. 1 Harry Fuecks photo credit: Bruno Gerber http://www.flickr.com/photos/beegee74/231137320/ 2 http://www.phppatterns.com/ vi Matthew Weier O’Phinney Matthew Weier O’Phinney is a full-time father of two and spends his free time developing in PHP. He is a PEAR developer, core contributor to Zend Framework, and all-around PHP 5 proponent—though PHP 6 cannot come soon enough for him. About the Expert Reviewer Jason Sweat has used PHP since 2001, where he was searching for a free—as in beer—substi tute for IIS/ASP to create an