Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 1 Professional PHP Programming Jesus Castagnetto Harish Rawat Sascha Schumann Chris Scollo Deepak Veliath Wrox Press Ltd. TEAM FLY PRESENTS Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 2 Professional PHP Programming © 1999 Wrox 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 quotations 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, the information contained in this book is sold without warranty, either express or implied. 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 Wrox Press Ltd Arden House, 1102 Warwick Road, Acock's Green, Birmingham B27 6BH, UK Printed in USA ISBN 1-861002-96-3 TEAM FLY PRESENTS Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Trademark Acknowledgements Wrox has endeavored to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, Wrox cannot guarantee the accuracy of this information. Credits 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 Editor 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 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 4 About the Authors Jesus M. Castagnetto Jesus M. Castagnetto is a Ph.D. Chemist currently 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 applets/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 that 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 their 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 Group 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 president of Taurix, a software development company based in New Brunswick, New Jersey. 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. Favorite OS - Linux. When not writing books, busy making plots to kill editors in their sleep :) TEAM FLY PRESENTS Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com i Table of Contents I. Introduction 1. About this book 2. Downloading, installing, configuring PHP 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 defined. 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 Array Non-sequentially Indexed Arrays 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 expressions III. Applying PHP 11. Non-relational databases What is a database? Databases and CSV dbm - a fast, record-based, flat-file database TEAM FLY PRESENTS Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com ii Another interface to dbm - DBA Our telephone book interface Error! Bookmark not defined. 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 TEAM FLY PRESENTS Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com iii The binary compiler The optimizer New features in PHP4 PHP4 and overall portability E. Other Resources TEAM FLY PRESENTS Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com iv 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 information 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 effectively 21-8. applying stripslashes() 21-9. two helper function 21-10. using sprintf() TEAM FLY PRESENTS Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 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 pages 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 their 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 technology 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 authors 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 areas: ❑ The PHP language structure and syntax, including its data types, operators, statements and functions; ❑ The process of installing the scripting engine 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 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... application/x-httpd -php php3 php AddType application/x-httpd -php3 -source phps # for PHP4 this should be AddType application/x-httpd -php3 -source phps # if compiled the stand-alone version, uncomment the following line #Action application/x-httpd -php3 /cgi-bin /php # use Action application/x-httpd -php /cgi-bin /php for PHP 4 # if compiled as a loadable module, uncomment the following line #LoadModule php_ module... recognize files with the extension php3 to be PHP scripts and use the PHP module to interpret them In this book we use mainly the php extension, so you can set the AddType directive to php with either PHP 3 or PHP 4 The argument application/xhttpd -php3 (application/x-httpd -php for PHP 4) indicates the MIME-type Now to start the newly installed Apache server with the PHP module built into it: # /usr/local/apache/bin/apachectl... php3 _imap4r2.dll IMAP 4 functions php3 _ldap.dll LDAP functions php3 _msql1.dll MSQL 1 client php3 _msql2.dll MSQL 2 client php3 _mssql.dll MSSQL client (requires MSSQL DB-Libraries php3 _mysql.dll MySQL functions (available on PHP4 too as php_ mysql.dll ) php3 _nsmail.dll Netscape mail functions php3 _oci73.dll Oracle 7.3 functions A word on browser capability detection - PHP 3 has a feature that allows scripts to be aware... from the PHP manual is below: Library Description php3 _calendar.dll Calendar conversion functions php3 _crypt.dll Crypt functions php3 _dbase.dll DBase functions php3 _dbm.dll GDBM emulation via Berkely DB2 library php3 _filepro.dll Read-only access to filepro databases php3 _gd.dll GD Library functions for gif manipulation php3 _hyperwave.dll HyperWave functions php3 _imap4r2.dll IMAP 4 functions php3 _ldap.dll... /usr/web/server /php3 / Independently of which method you used, you will need to add statements to the Apache configuration file (usually httpd.conf), so the server will know what to do when requests for PHP scripts come: ❑ Add a PHP type, and the appropriate directives in case you compiled the stand-alone or the loadable version of PHP: AddType application/x-httpd -php3 php3 php #for PHP 4 this should... the PHP module built into it We may need to do some tweaking of the Apache configuration files before we get Apache to recognize PHP scripts and use the PHP module for interpreting them We need to edit the Apache configuration file /etc/httpd/conf/httpd.conf to add the following entry: AddType application/x-httpd -php3 php3 However for PHP 4 the entry should be: AddType application/x-httpd -php php The... need to first get the binaries from http://www .php. net/download -php. php3 and extract the files into a directory, like C:\Program Files \PHP Make sure you have all the DLLs (see previous table) for all the add-on modules that you need Copy these into C:\Program Files \PHP We need to create PHP s configuration file php3 .ini, from the sample configuration file phpdist.ini and then copy it into your Windows... // (this allows documents be stored under directories other than the web // server’s root) AddType application/x-httpd -php3 php3 Action application/x-httpd -php3 " /php3 /php. exe" // (provided that the PHP interpreter is in the php3 directory under the web // server’s root) To install PHP 4 with IIS 5 on Windows 2000 you will need to set your options using the MMC (Microsoft Management Console) Select... the Distribution You could obtain PHP distributions by: Methods ❑ HTTP ❑ FTP ❑ From an anonymous CVS server Formats ❑ Binary tar file ❑ RPMs ❑ Source code The core PHP distribution is available from the official PHP home at http://www .php. net PHP is available over FTP or HTTP from a number of mirror sites (for a site closest to you, check the list at the PHP homepage) PHP is distributed as source distributions... icon is shown when listing files in a directory: AddIcon /icons/script.gif conf sh shar csh ksh tcl php3 php phps ❑ And add a directive starting the PHP3 engine (this can also go in the php3 .ini file), and setting up the display of error messages (if any) when a script is interpreted: php3 _engine on php3 _display_errors on Finally, start (shutdown the server first if it is running already) or restart . information 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. 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. 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