PHP Fast & Easy Web Development, 2nd Edition Julie C Meloni Copyright © 2002 by Premier Press All rights reserved No part of this book may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system without written permission from Premier Press, except for the inclusion of brief quotations in a review The Premier Press logo, top edge printing, and related trade dress are trademarks of Premier Press, Inc and may not be used without written permission All other trademarks are the property of their respective owners Publisher: Stacy L Hiquet Marketing Manager: Heather Buzzingham Managing Editor: Sandy Doell Acquisitions Editor: Todd Jensen Project Editor: Kim V Benbow Editorial Assistant: Margaret Bauer Marketing Coordinator: Kelly Poffenbarger Technical Reviewer: Greg Perry Copy Editor: Kim V Benbow Interior Layout: Scribe Tribe Cover Design: Mike Tanamachi Indexer: Johnna VanHoose Dinse PHP is copyrighted by The PHP Group, and is released under the PHP License More information on the licensing terms is available in the license file within the PHP distribution or by visiting http://www.php.net/license/ Microsoft, Windows, and Internet Explorer are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries Netscape is a registered trademark of Netscape Communications Corporation in the U.S and other countries All other trademarks are the property of their respective owners Important: Premier Press cannot provide software support Please contact the appropriate software manufacturer's technical support line or Web site for assistance Premier Press and the author have attempted throughout this book to distinguish proprietary trademarks from descriptive terms by following the capitalization style used by the manufacturer Information contained in this book has been obtained by Premier Press from sources believed to be reliable However, because of the possibility of human or mechanical error by our sources, Premier Press, or others, the Publisher does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from use of such information Readers should be particularly aware of the fact that the Internet is an ever-changing entity Some facts may have changed since this book went to press ISBN: 1-931841-87-X Library of Congress Catalog Card Number: 2002104489 Printed in the United States of America 02 03 04 05 RI 10 9 8 7 6 5 4 3 2 1 Acknowledgments Thanks as always to the PHP Group, Zend Technologies, the Apache Software Foundation, and MySQL AB for creating and maintaining such wonderful and accessible products for all users Thanks to every single PHP user and developer because without you, I wouldn't have anything to write about Great thanks to the all the editors who worked with me on both editions of this book! Enormous thanks to everyone at i2i Interactive, for their never-ending support and encouragement About the Author JULIE MELONI is the technical director for i2i Interactive, a multimedia company located in Campbell, CA (that's just down the street from San Jose) She's been developing Web-based applications since the Web first saw the light of day and remembers the excitement surrounding the first GUI Web browser She is the author of several books and articles on Web-based programming languages and database topics, and you can find translations of her work in several languages, including Chinese, Italian, Portuguese, Polish, and even Serbian PHP Fast & Easy Web Development, 2nd Edition ISBN:193184187x by Julie C Meloni Premier Press © 2002 (481 pages) Getting Web developers up to speed with PHP Table of Contents Back Cover Table of Contents PHP Fast & Easy Web Development, 2nd Edition Introduction Part I - Getting Started Chapter 1 Chapter 2 Chapter 3 - Installing and Configuring MySQL - Installing Apache - Installing PHP Part II - The Absolute Basics of Coding in PHP Chapter 4 Chapter 5 Chapter 6 - Mixing PHP and HTML - Introducing Variables and Operators - Using Variables Part III - Start with Simple Stuff Chapter 7 Chapter 8 Chapter 9 Chapter 10 - Displaying Dynamic Content - Sending E-Mail - Using Your File System - Uploading Files to Your Web Site Part IV - Getting to Know Your Database Chapter 11 - Establishing a Connection Chapter 12 - Creating a Database Table Chapter 13 - Inserting Data into a Table Chapter 14 - Selecting and Displaying Data Part V - User Authentication and Tracking Chapter 15 - Database-Driven User Authentication Chapter 16 - Using Cookies Chapter 17 - Session Basics Part VI - Creating Your Own Contact Management System Chapter 18 Chapter 19 Chapter 20 Chapter 21 Chapter 22 - Planning Your System - Adding Contacts - Modifying Contacts - Deleting Contacts - Working with Contacts Part VII - Additional Administration Scripts Chapter 23 - Managing a Simple Mailing List Chapter 24 - Creating Custom Logs and Reports Part VIII - Appendixes Appendix A - Additional Configuration Options Appendix B - Basic PHP Language Reference Appendix C - Writing Your Own Functions Appendix D - Basic MySQL Reference Appendix E - Database Normalization Appendix F - Getting Help Index List of Tables PHP Fast & Easy Web Development, 2nd Edition ISBN:193184187x by Julie C Meloni Premier Press © 2002 (481 pages) Getting Web developers up to speed with PHP Table of Contents Back Cover Back Cover Don’t spend your time wading through manuals to learn PHP Spend it doing what you do best—creating Web pages! Get the skills you need to install PHP, create a database, and upload files to the Web Combining easy-to-understand instructions with visual examples, PHP Fast & Easy Web Development, 2nd Edition offers a unique, hands-on approach to PHP that you won’t find anywhere else Less time, less effort, more results! Screen by screen, use this comprehensive PHP guide to: Master the basics of coding in PHP Create a Contact Management System Create custom logs and reports Authenticate and track users Display dynamic content About the Author Julie Meloni is the technical director for i2i Interactive, a multimedia company located in Campbell, CA She’s been developing Web-based applications since the Web first saw the light of day and remembers the excitement surrounding the first GUI Web browser She is the author of several books and articles on Webbased programming languages and database topics Introduction In the two years since the first edition of PHP Fast & Easy Web Development was published, I've received a significant amount of positive feedback regarding the teaching style of this book I was originally skeptical when, about a month after the release of my first book, PHP Essentials, my editors decided that a Fast & Easy version was warranted The Fast & Easy style is a step-by-step, learn-byexample path to learning a new programming language—with pictures included! Unlike the long, text-filled chapters upon chapters of most programming books, the Fast & Easy style did indeed appeal to users who are new to PHP, and especially to programming in general This second edition takes into account some of the feedback from the first edition, but holds true to the original structure and learning path The first three chapters are dedicated to getting Apache, MySQL, and PHP up and running on your Windows or Linux machine You may be surprised at how simple it is, and how quickly you'll be up and running A few additional chapters have been added to provide additional projects for practicing your new-found skills; and some appendices of additional database and programming information also round out this new edition This book remains a solid introduction for the beginning user, who learns best by example After completing this book, you will have a strong foundation in the basics before approaching advanced levels of application design Before jumping into all that, take a moment to familiarize yourself with PHP and its history What Is PHP? Its official name is PHP: Hypertext Preprocessor, and it is a serverside scripting language When your Web browser accesses a URL, it is making a request to a Web server When you request a PHP page, something like http://www.yourcompany.com/home.php, the Web server wakes up the PHP parsing engine and says, "Hey! You've got to do something before I send a result back to this person's Web browser." Then, the PHP parsing engine runs through the PHP code found in home.php, and returns the resulting output This output is passed back to the Web server as part of the HTML code in the document, which in turn is passed on to your browser, which displays it to you A Brief History of PHP In 1994, an incredibly forward-thinking man named Rasmus Lerdorf developed a set of tools that used a parsing engine to interpret a few macros here and there They were not extravagant: a guest book, a counter, and some other "home page" elements that were cool when the Web was in its infancy He eventually combined these tools with a form interpretation (FI) package he had written, added some database support, and released what was known as PHP/FI Then, in the spirit of Open Source software development, developers all over the world began contributing to PHP/FI By 1997, more than 50,000 Web sites were using PHP/FI to accomplish different tasks— connecting to a database, displaying dynamic content, and so on At that point, the development process really started becoming a team effort With primary assistance from developers Zeev Suraski and Andi Gutmans, the version 3.0 parser was created The final release of PHP 3.0 occurred in June of 1998, when it was upgraded to include support for multiple platforms (it's not just for Linux anymore!) and Web servers, numerous databases, and SNMP (Simple Network Management Protocol) and IMAP (Internet Message Access Protocol) Then, the birth of PHP 4.0 occurred No small version change, PHP 4.0 marked a complete rethinking of the PHP core and a rewrite of the internals of the scripting language itself The PHP development team and Zend Technologies produced a remarkable product with nearly a 50-fold performance improvement over version 3.0, with a long list of new and useful features So where are we now? Over 7 million Web servers worldwide run the 4.x version of PHP PHP 4.x has been in use for over two years, with the current version being 4.2.1 PHP 4.x can work with just about any combination of Web server, operating system, and database you can think up Companies such as Zend Technologies have released products to optimize and accelerate PHP, as well as "studio" environments for developing and testing PHP code PHP usage has exploded as an enterprise application development solution—it's not just for home pages and small projects any more! What Does PHP Do? PHP does anything you want, except sit on its head and spin Actually, with a little on-the-fly image manipulation and Dynamic HTML, it could probably do that, too According to the PHP manual, "The goal of the language is to allow Web developers to write dynamically generated pages quickly." Here are some common uses of PHP: List of Tables Chapter 4: Mixing PHP and HTML Table 4.1: Basic PHP Start and End Tags Chapter 5: Introducing Variables and Operators Table 5.1: Assignment Operators Table 5.2: Arithmetic Operators Table 5.3: Comparison Operators Chapter 9: Using Your File System Table 9.1: Modes Used with fopen() Chapter 12: Creating a Database Table Table 12.1: Some MySQL Data Types Table 12.2: Fields for my_music Chapter 16: Using Cookies Table 16.1: Common Times Chapter 18: Planning Your System Table 18.1: Fields for my_contacts Chapter 22: Working with Contacts Table 22.1: Date Formatting Example Chapter 23: Managing a Simple Mailing List Table 23.1: Fields for Subscribers Chapter 24: Creating Custom Logs and Reports Table 24.1: Apache Log File Settings Appendix A: Additional Configuration Options Table A.1: Windows Extensions Table A.2: Configuration Options Appendix B: Basic PHP Language Reference Table B.1: Basic PHP Start and End Tags Table B.2: Arithmetic Operators Table B.3: Assignment Operators Table B.4: Comparison Operators Table B.5: Increment/Decrement Operators Table B.6: Logical Operators Table B.7: date() Function Formats Table B.8: fopen() Function Modes Table B.9: sprintf() Function Formatting Directives Appendix E: Database Normalization Table E.1: Students and Courses Table Table E.2: The students Table Table E.3: The students_courses Table Table E.4: The courses Table Table E.5: The New students_courses Table Table E.6: The courses Table Table E.7: The instructors Table Table E.8: Original my_contacts Table Table E.9: Fields for address Table Table E.10: Fields for phone Table Table E.11: Fields for email Table Table E.12: New my_contacts Table PHP Fast & Easy Web Development, 2nd Edition ISBN:193184187x by Julie C Meloni Premier Press © 2002 (481 pages) Getting Web developers up to speed with PHP Table of Contents Back Cover Table of Contents PHP Fast & Easy Web Development, 2nd Edition Introduction Part I - Getting Started Chapter 1 Chapter 2 Chapter 3 - Installing and Configuring MySQL - Installing Apache - Installing PHP Part II - The Absolute Basics of Coding in PHP Chapter 4 Chapter 5 Chapter 6 - Mixing PHP and HTML - Introducing Variables and Operators - Using Variables Part III - Start with Simple Stuff Chapter 7 Chapter 8 Chapter 9 Chapter 10 - Displaying Dynamic Content - Sending E-Mail - Using Your File System - Uploading Files to Your Web Site Part IV - Getting to Know Your Database Chapter 11 - Establishing a Connection Chapter 12 - Creating a Database Table Chapter 13 - Inserting Data into a Table Chapter 14 - Selecting and Displaying Data Part V - User Authentication and Tracking Chapter 15 - Database-Driven User Authentication Chapter 16 - Using Cookies Chapter 17 - Session Basics Part VI - Creating Your Own Contact Management System Chapter 18 Chapter 19 Chapter 20 Chapter 21 Chapter 22 - Planning Your System - Adding Contacts - Modifying Contacts - Deleting Contacts - Working with Contacts Part VII - Additional Administration Scripts Chapter 23 - Managing a Simple Mailing List Chapter 24 - Creating Custom Logs and Reports Part VIII - Appendixes Appendix A - Additional Configuration Options Appendix B - Basic PHP Language Reference Appendix C - Writing Your Own Functions Appendix D - Basic MySQL Reference Appendix E - Database Normalization Appendix F - Getting Help Index List of Tables ... by Julie C Meloni Premier Press © 2002 (481 pages) Getting Web developers up to speed with PHP Table of Contents Back Cover Table of Contents PHP Fast & Easy Web Development, 2nd Edition Introduction... Index List of Tables PHP Fast & Easy Web Development, 2nd Edition ISBN:193184187x by Julie C Meloni Premier Press © 2002 (481 pages) Getting Web developers up to speed with PHP Table of Contents... languages and database topics, and you can find translations of her work in several languages, including Chinese, Italian, Portuguese, Polish, and even Serbian PHP Fast & Easy Web Development, 2nd Edition