Profe ssional PHP Programming Jesus Castagnetto Haris h Rawat Sascha Schumann Chris Scollo Deepa k Veliath Wrox Press Ltd. 1 TEAM FLY PRESENTS Professional PH P Programming © 1999 W rox Press 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 quotat ions embodied in critical articles or reviews. The authors and publisher have made every effort in the preparation of this book to ensure the accuracy of the information. However, t he information contained in this book is sold without warranty, either express or implie d. Neither the authors, Wrox Press nor its dealers or distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book. Published by W rox Press Ltd Arden House, 1102 Warwick Road, Acock's Green, Birmingham B27 6BH, UK Printed in USA ISBN 1-86 1002-96-3 2 TEAM FLY PRESENTS Trademark Acknowledgements Wrox has endeavored to provide trademark information about all the companies and products mentioned in this book by the ap propriate use of capitals. However, Wrox cannot guarantee the accuracy of this information. C redits Authors Technical Reviewers Jesus Castagnetto Matt Allen Harish Rawat Vivek Awasthi Sascha Schumann Gianluca Baldo Chris Scollo Robert Baskerville Deepak Veliath Mathijs Brands Andy Jeffries Additional Material Kristian Kohntopp Mathijs Brands Manuel Lemos Rod Kreisler Samuel Liddcott Brian Moon Brad Marsh Mark Musone Neil Matthews Julian Skinner Sebastian Moerike-Krauz Mark Musone Editors Paul Schreiber Robert FE Shaw Rick Stones Soheb Siddiqi Adrian Sill Julian Skinner Andrew Stopford Adrian Teasdale Development Edit or Mark Wilcox Richard Collins Design / Layout Managing Editor Tom Bartlett Paul Cooper Mark Burdett Will Fallon Project Manager Jon Jones Tony Berry John McNulty Index Cover Design Martin Brooks Chris Morris TEAM FLY PRESENTS About the Authors Jesus M. Castagnetto Jesus M. Castagnetto is a Ph.D. Chemist curr ently working at The Scripps Research Institute as a Postdoctoral Research Associate in the Metalloprotein Structure and Design Group (http://www.scripps.edu/research/metallo/), where he is developing the Metalloprotein Database and Browser (http:/ /metallo.scripps.edu/0) using a combination of in-house programs, Java appl ets/servlets, SQL databases, and PHP3 server-side scripting. Got his PhD from New York University for research in computational, synthetic, and physico-chemical studies of small molecule ligand-metal complexes. Nowadays, he tackles systems th at are a "little" bigger: Metalloproteins. Bioinformatics and computational modeling of chemical systems are two of his main areas of interest. In his spare time, (yeah, right!) he hacks awk, PHP, plays way too much with his PalmPilot, and tries not to hurt anybody while doing Shorinji Kempo, or swinging his sword wildly in Iaijutsu class. Harish Rawat Harish Rawat is a Software Developer at the Oracle Corporation. He has seven years of experience in systems programming. His technical areas of interest include XML, Java, and Network protocols. I would like to thank Arnab and Pankaj for th eir invaluable suggestions in designing the Shopping Cart Application. I would also like to thank people at Oracle India Development Center for their encouragement and support throughout the writing process. Sascha Schumann Sascha Schumann is a member of the PHP G roup and the Apache Software Foundation. He currently studies at Gymnasium Letmathe. Sascha is the architect of many PHP modules (including but not limited to: DBA, Session management, mcrypt, mhash), has connected PHP with AOLserver, and provides general PHP support and custom PHP extensions to organizations employing PHP. Sascha also maintains mhash, which is an open source software for creating cryptological digests (so called hashes). Christopher Scollo Christopher Scollo is the co-founder and pres ident of Taurix, a software development company based in New Brunswick, New Jers ey. When not developing web applications, teaching web courses, or drowning in web periodicals, he hikes and eats. (Potatoes are a passion.) In addition to programming languages, he also enjoys human languages and speaks English, German, French, and Italian to varying degrees. He lives in Princeton, New Jersey with his wife, Nicole Bator. Deepak Veliath Deepak Veliath is currently working for the Oracle Corporation in Bangalore, India. He has been following PHP for a year now. Favo rite OS - Linux. When not writing books, busy making plots to kill editors in their slee p :) 4 TEAM FLY PRESENTS Table of Contents I. Introduction 1. About this book 2. Downloading, installing, configuring P HP 3. Programming in a Web environment II. The Language 4. Variables and data types 5. Operators 6. Expressions and Statements if ... else ... elseif switch while and do ... while for Error! Bookmark not defin ed. require and include exit Error! Bookmark not defined. 7. Functions How They Work Passing Arguments Variable Scope and Lifetime Nested Functions and Recursion Assigning Functions to Variables 8. Arrays Starting Simply Looping Through an Array A Sequentially Indexed Arra y Non-sequentially Indexed Ar rays A Few Words About next() and prev() array_walk() Associative Arrays Multi-Dimensional Arrays Sorting Functions Using Arrays with Form Elements 9. Objects and OOP Object-Oriented Programming Defining a Class Instantiating the Class Constructors Inheritance 10. String manipulation and regular expr essions III. Applying PHP 11. Non-relational databases What is a database? Databases and CSV dbm - a fast, record-based, flat-file database i TEAM FLY PRESENTS Another interface to dbm - DBA Our telephone book interface Error! Bookmark not defin ed. 12. SQL DBMS engines 13. Using LDAP 14. Serving XML documents 15. Graphic content creation 16. PHP connectivity Utilizing TCP The whois function The Frontend Extending the Frontend Is my domain still available? A web client 17. Sending E-mail 18. Cookies What are cookies? Cookies in PHP Getting started Deleting a cookie Common pitfalls What is your name? 19. Error handling and debugging 20. Security issues 21. Magic with quotes What are magic quotes? Defining the scope Magic quotes explained Sybase extension Applying magic_quotes Two helper functions An alternative approach Summary 22. Templates IV. Sample Applications 23. A shopping cart application 24. An OO site V. Advanced Topics 25. Version migration 26. When should I avoid regular expression functions VI. Appendix A. Reference B. Open Source Software C. PHP in non-UNIX environments D. Notes on PHP4/Zend Basic concepts ii TEAM FLY PRESENTS The binary compiler The optimizer New features in PHP4 PHP4 and overall portability E. Other Resources iii TEAM FLY PRESENTS List of Examples 11-1. edit_form() function 16-1. Initiating a TCP connection 16-2. Exchanging messages with a SMTP server 16-3. whois_request() function 16-4. Simple whois frontend (HTML code) 16-5. Simple whois frontend (PHP code) 16-6. Source code of the simple whois front-end 16-7. Allowing server choice 16-8. Changed PHP code 16-9. a function to tell the TLD 16-10. using an array to store informatio n about whois server 16-11. is_domain_available 16-12. http class framework 16-13. Full http class 16-14. Using the http class 18-1. counting the hits per visitor 18-2. setcookie fails due to sent data before it 18-3. specifying the expire time 18-4. Deleting the cookie username 18-5. deleting and setting a new cookie 18-6. Identifying a visitor 21-1. addslashes() 21-2. output 21-3. output with enabled magic_quotes_sybase 21-4. The ugly way 21-5. automating the conversion process 21-6. using get_magic_quotes_gpc 21-7. using magic_quotes_runtime e ffectively 21-8. applying stripslashes() 21-9. two helper function 21-10. using sprintf() iv TEAM FLY PRESENTS Introduction Over the past years, the Internet has gone from the preserve of academics to the cutting edge of business. A large part of this has been driven by the growth of the Web, with its graphical browsers and high media profile. The change from static HTML pa ges to dynamic, user interactive presentations has been achieved largely by the introduction of scripting technologies. Working with the usual markup language of a web page, scripting languages enable clients to demand specific information from their servers, and thei r servers, in turn, to receive important user input in order to process and display data on demand. In the forefront of this advancement has been the Open Source community, dedicated in providing web- based solutions purely for the love of the techno logy itself. Open source is not only about free software (though not everything is free anyway), but it is about, as the name suggests, being open about the source of the code. Having free access to the source means that auth ors are forced to keep to standards. If these are not maintained, the deviation is labeled a bug, and if the author doesn't fix it, someone else will. Large numbers of independent programmers being able to understand what a program does, and ensuring that standards are maintained, prevents the author from being able to exploit the user, as happens in the commercial software world. What Does This Book Cover? PHP, the subject of this book, is an open source server-side scripting language that has taken web-based development to a new level of sophistication. In this book we will show you what PHP is, how it simplifies server-side scripting and adds to the functionality of your web pages. In particular, we'll be looking at the following a reas: ❑ The PHP language structure and syntax, including its data types, operators, statements and functions; ❑ The process of installing the scripting e ngine onyour server; ❑ The myriad of PHP modules that greatly enhance its capabilities; ❑ Server Side Programming ❑ Practical case study examples of PHP in action. TEAM FLY PRESENTS
Description: