Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 984 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
984
Dung lượng
10,58 MB
Nội dung
“I’ve never purchased a better programming book… This book proved to be the most informative, easiest to follow, and had the best examples of any other computer-related book I have ever purchased.The text is very easy to follow!” —Nick Landman “…the Sams book byWelling & Thomson is the only one which I have found to be indispensable.The writing is clear and straightforward but never wastes my time.The book is extremely well laid out.The chapters are the right length and chapter titles quickly take you where you want to go.” —Wright Sullivan, President,A&E Engineering, Inc., Greer South Carolina “I just wanted to tell you that I think the book PHPandMySQLWebDevelopment rocks! It’s logically structured, just the right difficulty level for me (intermediate), interesting and easy to read, and, of course, full of valuable information!” —CodE-E, Austria “There are several good introductory books on PHP, but Welling & Thomson is an excellent handbook for those who wish to build up complex and reliable systems. It’s obvious that the authors have a strong back- ground in the development of professional applications and they teach not only the language itself, but also how to use it with good software engineering practices.” —Javier Garcia, senior telecom engineer, Telefonica R&D Labs, Madrid “I picked up this book two days ago and I am half way finished. I just can’t put it down.The layout and flow is perfect. Everything is presented in such a way so that the information is very palatable. I am able to immediately grasp all the concepts. The examples have also been wonderful. I just had to take some time out to express to you how pleased I have been with this book.” —Jason B. Lancaster “This book has proven a trusty companion, with an excellent crash course in PHPand superb coverage of MySQL as used for Web applications. It also features several complete applications that are great examples of how to construct modular, scalable applications with PHP.Whether you are a PHP newbie or a veteran in search of a better desk-side reference, this one is sure to please!” —WebDynamic “The true PHP/MySQL bible, PHPandMySQLWebDevelopmentbyLukeWellingandLaura Thomson, made me realize that programming and databases are now available to the commoners. Again, I know 1/10000th of what there is to know, and already I’m enthralled.” —Tim Luoma,TnTLuoma.com “Welling and Thomson’s book is a good reference for those who want to get to grips with practical projects straight off the bat. It includes webmail, shopping cart, session control, and web-forum/weblog applications as a matter of course, and begins with a sturdy look at PHP first, moving to MySQL once the basics are covered.” —twilight30 on Slashdot 00 6728 fm 9/2/04 1:15 PM Page i “This book is absolutely excellent, to say the least…. LukeWellingandLaura Thomson give the best in-depth explana- tions I’ve come across on such things as regular expressions, classes and objects, sessions etc. I really feel this book filled in a lot of gaps for me with things I didn’t quite understand….This book jumps right into the functions and features most commonly used with PHP, and from there it continues in describing real-world projects, MySQL integration, and security issues from a proj- ect manager’s point of view. I found every bit of this book to be well organized and easy to understand.” —notepad on codewalkers.com “A top-notch reference for programmers using PHPand MySQL. Highly recommended.” —The Internet Writing Journal “This book rocks! I am an experienced programmer, so I didn’t need a lot of help with PHP syntax; after all, it’s very close to C/C++. I don’t know a thing about databases, though, so when I wanted to develop a book review engine (among other projects) I wanted a solid reference to using MySQL with PHP. I have O’Reilly’s mSQL andMySQL book, and it’s probably a better pure-SQL reference, but this book has earned a place on my reference shelf…Highly recommended.” —Paul Robichaux “One of the best programming guides I’ve ever read.” —jackofsometrades from Lahti, Finland “This is a well-written book for learn- ing how to build Internet applications with two of the most popular open-source Webdevelopment technologies….The projects are the real jewel of the book. Not only are the projects described and constructed in a logical, component-based manner, but the selection of projects represents an excellent cross-section of common components that are built into many web sites.” —Craig Cecil “The book takes an easy, step-by-step approach to introduce even the clueless programmer to the language of PHP. On top of that, I often find myself referring back to it in my Web design efforts. I’m still learning new things about PHP, but this book gave me a solid foundation from which to start and continues to help me to this day.” —Stephen Ward “This book is one of few that really touched me and made me ‘love’ it. I can’t put it in my bookshelf; I must put it in a touchable place on my working bench as I always like to refer from it. Its structure is good, wordings are simple and straight for- ward, and examples are clear and step by step. Before I read it, I knew nothing of PHPand MySQL. After reading it, I have the confidence and skill to develop any complicated Web application.” —Power Wong “This book is God…. I highly recom- mend this book to anyone who wants to jump in the deep end with database driven Web application programming. I wish more computer books were organized this way.” —Sean C Schertell 00 6728 fm 9/2/04 1:15 PM Page ii PHPandMySQLWebDevelopment Sams Publishing, 800 East 96th Street, Indianapolis, Indiana 46240 DEVELOPER’S LIBRARY LukeWellingLaura Thomson Third Edition 00 6728 fm 9/2/04 1:15 PM Page iii PHPandMySQLWebDevelopment Third Edition Copyright 2005 by Sams Publishing All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photo- copying, recording, or otherwise, without written permission from the pub- lisher. No patent liability is assumed with respect to the use of the informa- tion contained herein.Although every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions. Neither is any liability assumed for damages resulting from the use of the information contained herein. International Standard Book Number: 0-672-32672-8 Library of Congress Catalog Card Number: 2003099244 Printed in the United States of America First Printing: October 2004 07 06 05 04 4321 Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. Wa r ning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied.The information provided is on an “as is” basis.The authors and the publisher 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 CD-ROM or programs accompanying it. Bulk Sales Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information, please contact U.S. Corporate and Government Sales 1-800-382-3419 corpsales@pearsontechgroup.com For sales outside the U.S., please contact International Sales 1-317-428-3341 international@pearsoned.com Acquisitions Editor Shelley Johnston Development Editor Scott Meyers Managing Editor Charlotte Clapp Copy Editor Chuck Hutchinson Indexer Mandie Frank Proofreader Paula Lowell Technical Editors Sara Golemon Chris Newman Media Specialist Dan Scherf Design Gary Adair Page Layout Cheryl Lynch Michelle Mitchell 00 6728 fm 9/2/04 1:15 PM Page iv ❖ To our Mums and Dads ❖ 00 6728 fm 9/2/04 1:15 PM Page v Contents at a Glance Introduction 1 I Using PHP 1 PHP Crash Course 11 2 Storing and Retrieving Data 57 3 Using Arrays 79 4 String Manipulation and Regular Expressions 105 5 Reusing Code and Writing Functions 129 6 Object-Oriented PHP 157 7 Exception Handling 191 II Using MySQL 8 Designing Your Web Database 205 9 Creating Your Web Database 217 10 Working with Your MySQL Database 241 11 Accessing Your MySQL Database from the Web with PHP 265 12 Advanced MySQL Administration 285 13 Advanced MySQL Programming 307 III E-commerce and Security 14 Running an E-commerce Site 323 15 E-commerce Security Issues 337 16 Implementing Authentication with PHPandMySQL 357 17 Implementing Secure Transactions with PHPandMySQL 379 00 6728 fm 9/2/04 1:15 PM Page vi IV Advanced PHP Techniques 18 Interacting with the File System and the Server 401 19 Using Network and Protocol Functions 419 20 Managing the Date and Time 439 21 Generating Images 451 22 Using Session Control in PHP 479 23 Other Useful Features 495 V Building Practical PHPandMySQL Projects 24 Using PHPandMySQL for Large Projects 507 25 Debugging 523 26 Building User Authentication and Personalization 541 27 Building a Shopping Cart 579 28 Building a Content Management System 625 29 Building a Web-Based Email Service 657 30 Building a Mailing List Manager 695 31 Building Web Forums 751 32 Generating Personalized Documents in Portable Document Format (PDF) 783 33 Connecting to Web Services with XML and SOAP 819 VI Appendixes A Installing PHPandMySQL 867 B Web Resources 889 Index 893 00 6728 fm 9/2/04 1:15 PM Page vii Table of Contents Introduction 1 I Using PHP 1 PHP Crash Course 11 Using PHP 12 Creating a Sample Application: Bob’s Auto Parts 12 Creating the Order Form 12 Processing the Form 14 Embedding PHP in HTML 15 Use of PHP Tags 16 PHP Tag Styles 16 PHP Statements 17 Whitespace 18 Comments 18 Adding Dynamic Content 19 Calling Functions 20 Using the date() Function 20 Accessing Form Variables 21 Form Variables 21 String Concatenation 24 Va r iables and Literals 25 Understanding Identifiers 26 Creating User-Declared Variables 26 Assigning Values to Variables 26 Examining Variable Types 27 PHP’s Data Types 27 Type Strength 27 Type Casting 28 Va r iable Variables 28 Declaring and Using Constants 29 Understanding Variable Scope 29 00 6728 fm 9/2/04 1:15 PM Page viii Using Operators 30 Arithmetic Operators 31 String Operators 32 Assignment Operators 32 Comparison Operators 34 Logical Operators 36 Bitwise Operators 36 Other Operators 37 Using Operators:Working Out the Form Totals 39 Understanding Precedence and Associativity: Evaluating Expressions 40 Using Variable Functions 42 Testing and Setting Variable Types 42 Testing Variable Status 43 Reinterpreting Variables 44 Implementing Control Structures 44 Making Decisions with Conditionals 44 if Statements 44 Code Blocks 45 else Statements 45 elseif Statements 46 switch Statements 47 Comparing the Different Conditionals 49 Repeating Actions Through Iteration 49 while Loops 51 for and foreach Loops 52 do while Loops 53 Breaking Out of a Control Structure or Script 54 Employing Alternative Control Structure Syntax 54 Using declare 55 Next: Saving the Customer’s Order 55 2 Storing and Retrieving Data 57 Saving Data for Later 57 Storing and Retrieving Bob’s Orders 58 Processing Files 59 00 6728 fm 9/2/04 1:15 PM Page ix x Contents Opening a File 59 Choosing File Modes 59 Using fopen() to Open a File 60 Opening Files Through FTP or HTTP 62 Addressing Problems Opening Files 63 Writing to a File 65 Parameters for fwrite() 66 File Formats 66 Closing a File 67 Reading from a File 69 Opening a File for Reading: fopen() 70 Knowing When to Stop: feof() 71 Reading a Line at a Time: fgets(), fgetss(), and fgetcsv() 71 Reading the Whole File: readfile(), fpassthru(),and file() 72 Reading a Character: fgetc() 73 Reading an Arbitrary Length: fread() 73 Using Other Useful File Functions 74 Checking Whether a File Is There: file_exists() 74 Determining How Big a File Is: filesize() 74 Deleting a File: unlink() 74 Navigating Inside a File: rewind(), fseek(), and ftell() 74 Locking Files 76 Doing It a Better Way: Database Management Systems 77 Problems with Using Flat Files 77 How RDBMSs Solve These Problems 78 Further Reading 78 Next 78 3 Using Arrays 79 What Is an Array? 79 Numerically Indexed Arrays 80 Initializing Numerically Indexed Arrays 80 Accessing Array Contents 81 Using Loops to Access the Array 82 00 6728 fm 9/2/04 1:15 PM Page x [...]... VI Appendixes A Installing PHPandMySQL 867 Running PHP as a CGI Interpreter or Module 868 Installing Apache, PHP, andMySQL Under Unix 868 Binary Installation 868 Source Installation 868 httpd.conf File: Snippets 875 Is PHP Support Working? 876 Is SSL Working? 877 00 6728 fm 9/2/04 1:15 PM Page xxxi Contents Installing Apache, PHP, andMySQL Under Windows 878 Installing MySQL Under Windows 879 Installing... Sams Publishing, visit our Web site at www.samspublishing.com.Type the ISBN (excluding hyphens) or the title of the book in the Search box to find the book you’re looking for 01 6728 IN 9/2/04 1:24 PM Page 1 Introduction W ELCOME TO PHP AND MYSQLWEBDEVELOPMENT Within its pages, you will find distilled knowledge from our experiences using PHPand MySQL, two of the hottest webdevelopment tools around... real-world website and show you how to implement these aspects in PHPandMySQL In the final part of this book, we describe how to approach real-world projects and take you through the design, planning, and building of the following projects: User authentication and personalization Shopping carts Content-management systems Web- based email Mailing list managers Web forums PDF document generation Web services... award-winning webdevelopment firm Tangled Web Design Laura has previously worked for Telstra and the Boston Consulting Group She holds a Bachelor of Applied Science (Computer Science) degree and a Bachelor of Engineering (Computer Systems Engineering) degree with honors, and is currently completing her Ph.D in Adaptive Web Sites In her spare time, she enjoys sleeping Laura can be reached via email at laura@ tangledweb.com.au... as PHP and a database such as MySQL allows you to make your sites dynamic: to have them be customizable and contain real-time information We have deliberately focused this book on real-world applications, even in the introductory chapters.We begin by looking at a simple online ordering system and work our way through the various parts of PHPandMySQL We then discuss aspects of electronic commerce and. .. reached via email at luke@ tangledweb.com.au Both authors have attained the MySQL Core Certification offered byMySQL AB and the Zend Certified PHP Engineer offered by Zend Technologies Ltd About the Contributors Israel Denis Jr is a freelance consultant working on e-commerce projects throughout the world He specializes in integrating ERP packages such as SAP and Lawson with custom web solutions.When... dedication and patience this book would not have been possible.We would also like to thank Israel Denis Jr and Chris Newman for their valuable contributions We appreciate immensely the work done by the PHP and MySQLdevelopment teams.Their work has made our lives easier for a number of years now and continues to do so on a daily basis We thank Adrian Close at eSec for saying “You can build that in PHP back... Information About the PHP Environment 499 Finding Out What Extensions Are Loaded 499 Identifying the Script Owner 500 Finding Out When the Script Was Modified 500 Loading Extensions Dynamically 500 Temporarily Altering the Runtime Environment 500 Highlighting Source Code 501 Using PHP on the Command Line 502 Next 503 V Building Practical PHPandMySQL Projects 24 Using PHPandMySQL for Large Projects... at laura@ tangledweb.com.au LukeWelling is a senior web developer at MySQL AB, the company behind the MySQL database He has previously taught engineering and computer science at RMIT University in Melbourne, Australia and worked as a computer programmer for many years He holds a Bachelor of Applied Science (Computer Science) degree In his spare time, he attempts to perfect his insomnia Luke can be reached... Installing Apache Under Windows 882 Installing PHP for Windows 884 Installing PEAR 887 Setting Up Other Configurations 888 B Web Resources 889 PHP Resources 889 MySQLand SQL Specific Resources 891 Apache Resources 891 WebDevelopment 892 Index 893 xxxi 00 6728 fm 9/2/04 1:15 PM Page xxxii About the Authors Laura Thomson is a lecturer in the School of Computer Science and Information Technology at RMIT University . are a PHP newbie or a veteran in search of a better desk-side reference, this one is sure to please!” —WebDynamic “The true PHP/ MySQL bible, PHP and MySQL Web Development by Luke Welling and Laura. 6728 fm 9/2/04 1:15 PM Page ii PHP and MySQL Web Development Sams Publishing, 800 East 96th Street, Indianapolis, Indiana 46240 DEVELOPER’S LIBRARY Luke Welling Laura Thomson Third Edition 00. wordings are simple and straight for- ward, and examples are clear and step by step. Before I read it, I knew nothing of PHP and MySQL. After reading it, I have the confidence and skill to develop