Web Application Development with PHP 4.0 00 9971 FM 6/16/00 7:24 AM Page Ii
Total Page:16
File Type:pdf, Size:1020Kb
00 9971 FM 6/16/00 7:24 AM Page i Web Application Development with PHP 4.0 00 9971 FM 6/16/00 7:24 AM Page ii Other Books by New Riders Publishing MySQL GTK+/Gnome Application Paul DuBois, 0-7357-0921-1 Development Havoc Pennington, 0-7357-0078-8 A UML Pattern Language Paul Evitts, 1-57870-118-X DCE/RPC over SMB: Samba and Windows NT Domain Internals Constructing Superior Software Luke Leighton, 1-57870-150-3 Paul Clements, 1-57870-147-3 Linux Firewalls Python Essential Reference Robert Ziegler, 0-7357-0900-9 David Beazley, 0-7357-0901-7 Linux Essential Reference KDE Application Development Ed Petron, 0-7357-0852-5 Uwe Thiem, 1-57870-201-1 Linux System Administration Developing Linux Applications with Jim Dennis, M. Carling, et al, GTK+ and GDK 1-556205-934-3 Eric Harlow, 0-7357-0021-4 00 9971 FM 6/16/00 7:24 AM Page iii Web Application Development with PHP 4.0 Tobias Ratschiller Till Gerken With contributions by Zend Technologies, LTD 201 West 103rd Street, Zeev Suraski Indianapolis, Indiana 46290 Andi Gutmans 00 9971 FM 6/16/00 7:24 AM Page iv Web Application Development with PHP 4.0 By:Tobias Ratschiller and Till Gerken Copyright © 2000 by New Riders Publishing Publisher FIRST EDITION: July, 2000 David Dwyer All rights reserved. No part of this book may be reproduced Executive Editor or transmitted in any form or by any means, electronic or Al Valvano mechanical, including photocopying, recording, or by any information storage and retrieval system, without written Managing Editor permission from the publisher, except for the inclusion of Gina Brown brief quotations in a review. Product Marketing International Standard Book Number: 0-7357-0997-1 Manager Library of Congress Catalog Card Number: 00-100402 Stephanie Layton 04 03 02 01 00 7 6 5 4 3 2 1 Publicity Manager Interpretation of the printing code:The rightmost double- Susan Petro digit number is the year of the book’s printing; the right- Acquisitions Editors most single-digit number is the number of the book’s Ann Quinn printing. For example, the printing code 00-1 shows that the Alan Bower first printing of the book occurred in 2000. Stacey Beheler Composed in Bembo and MCPdigital by New Riders Publishing Nancy Maragioglio Printed in the United States of America Editor Robin Drake Trademarks Indexer All terms mentioned in this book that are known to be Cheryl Lenser trademarks or service marks have been appropriately capital- ized. New Riders Publishing cannot attest to the accuracy of Manufacturing this information. Use of a term in this book should not be Coordinator regarded as affecting the validity of any trademark or service Chris Moos mark.Windows and Microsoft are registered trademarks of Book Designer Microsoft Corporation. Louisa Klucznik Warning and Disclaimer Cover Designer Aren Howell This book is designed to provide information about PHP. Every effort has been made to make this book as complete Composition and as accurate as possible, but no warranty or fitness is Amy Parker implied. Proofreader The information is provided on an as-is basis.The authors Jessica McCarty and New Riders Publishing shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book or from the use of the discs or programs that may accompany it. 00 9971 FM 6/16/00 7:24 AM Page v Contents Foreword xiii Introduction xv Target Audience xvi Prerequisites xvii Organization of This Book xvii Conventions Used in This Book xviii I Advanced PHP 1 1 Development Concepts 3 PHP for Me? 3 The Importance of Planning 4 Coding Conventions 6 Using Files to Group Functions 24 Writing Documentation 25 An API Design Example 26 Summary 30 2 Advanced Syntax 31 PHP Syntax 31 Defining Constants 32 Array Functions 33 PHP and OOP 41 Linked Lists 53 Associative Arrays 63 Polymorphism and Self-Modifying Code 78 Summary 87 3 Application Design: A Real-Life Example 89 Project Overview 90 Comparing Technologies 91 IRC Network Basics 95 v 00 9971 FM 6/16/00 7:24 AM Page vi vi Contents Fitting the Application into the Network 97 Interfacing the Network 99 Administration and Security 117 Implementation 119 Summary 119 II Web Applications 121 4 Web Application Concepts 123 HTTP and Sessions 123 Security Considerations 140 Why Usability Matters 158 Summary 166 References 166 5 Basic Web Application Strategies 167 The PHP Normal Form 168 Project Layout 175 CVS: Concurrent Versions System 178 Three-Tier Applications 191 Summary 198 6 Database Access with PHP 199 PHPLib:The PHP Base Library 200 Database Abstraction 202 Authentication 213 Summary 224 7 Cutting-Edge Applications 225 Knowledge Repositories 226 PHP and XML 238 Interchanging Data with WDDX 270 Summary 276 00 9971 FM 6/16/00 7:24 AM Page vii Contents vii 8 Case Studies 277 BizChek.com 277 SixCMS 281 MarketPlayer.com 285 Summary 290 References 290 III Beyond PHP 291 9 Extending PHP 4.0: Hacking the Core of PHP 293 Overview 294 What Is Zend? and What Is PHP? 294 Extension Possibilities 295 Source Layout 297 PHP’s Automatic Build System 301 Creating Extensions 303 Compiling Modules 304 Using Extensions 306 Troubleshooting 308 Source Discussion 308 Accepting Arguments 317 Creating Variables 329 Objects 338 Resources 339 Macros for Automatic Global Variable Creation 340 Duplicating Variable Contents:The Copy Constructor 342 Returning Values 343 Printing Information 345 Startup and Shutdown Functions 348 Calling User Functions 349 Where to Go from Here 353 Reference: Some Configuration Macros 354 00 9971 FM 6/16/00 7:24 AM Page viii About the Authors Tobias Ratschiller is a new media consultant based in Italy.With extensive knowledge of software development, database design, and content-management sys- tems, he specializes in the creation of large-scale, dynamic Web sites. He has provided consulting and implementation services for some of the world’s largest Web sites and has contributed to several books and articles on PHP.He teaches at seminars through- out Europe and is a frequent speaker at leading conferences. Till Gerken is a freelance developer and consultant for various companies, focusing on the creation of Web applications for Internet-based services. His background ranges from using C/C++, Pascal, and x86 Assembler to create such high-performance multimedia systems as 3D engines and real-time sound mixers, to using PHP and its associated tools to create medium- to large-scale, dynamic Web sites. viii 00 9971 FM 6/16/00 7:24 AM Page ix About the Technical Reviewer Graeme Merrall contributed his considerable hands-on expertise to the entire development process for Web Application Development with PHP 4.0.As the book was being written, he reviewed all the material for technical content, organization, and flow. His feedback was critical to ensuring that Web Application Development with PHP 4.0 fits our readers’ need for the highest quality technical information. Graeme originally graduated in 1993 with a degree in biochemistry. During his university studies, he discovered the Internet while it was still very much in its infancy.This led him away from biochemistry into employment with an ISP and later with a leading Web design firm in New Zealand, where he developed his skills in PHP and ASP. As well as programming, Graeme has written for the local newspaper in his former home town in New Zealand and has produced several tutorials and articles on PHP for Wired Digital’s Web Monkey. Born and raised in New Zealand, Graeme currently resides in Sydney, where he runs his own consultancy, specializing in e-commerce and business integration with the Internet. In his spare time, he enjoys modern literature, music, and crocodile wrestling. About Zend Technologies, LTD. The Zend Engine is the basic scripting engine that drives PHP.Owned by Zend Technologies, LTD, the engine is licensed to PHP for free use under the Q Public license.The Zend engine brings to PHP performance, reliability, and an easy-to-use scripting interface. The history of the Zend Engine began four years ago when the company founders, Zeev Suraski and Andi Gutmans, joined the core development team of PHP and wrote the scripting engine of PHP,which is installed on over a million hosts today. Now, with the introduction of PHP 4.0, the Zend Engine has matured into a versatile scripting engine, and Andi Gutmans and Zeev Suraski are engaged in devel- oping a host of products for enhancing PHP’s performance and commercial value. ix 00 9971 FM 6/16/00 7:24 AM Page x Acknowledgments We’d like to say “thank you” to the staff at New Riders:You’ve probably had a hard time with us, and we appreciate your friendliness and professional handling. Robin Drake, our editor, deserves special thanks for her patience with us.Thanks as well to our technical editor, Graeme Merrall, and acquisitions editor,Ann Quinn. The following people have helped us during various stages of this book, and we’d like to thank them as well:Alan Bower, Nancy Maragioglio, Jakob Nielsen, Kristian Koehntopp, Zeev Suraski,Andi Gutmans, Leon Atkinson,Alexander Aulbach, Uwe Steinmann, Boaz Yahav, and Rafi Ton.We’d also like to thank the authors of our case studies. Finally, thanks to SoftQuad for providing their excellent XMetaL XML editor for use in writing and editing the text. Acknowledgments from Tobias The person who deserves the most thanks is Till Gerken, of course, who was a great coauthor.We’ve spent thousands (or so) of hours on IRC, reviewing chapters, writing and fixing code—and having fun.