4 String Manipulation and Regular Expressions 1075 Reusing Code and Writing Functions 133 7 Error and Exception Handling 193 II Using MySQL 8 Designing Your Web Database 207 9 Creating Y
Trang 2“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
“This book by Welling & Thomson is the
only one which I have found to be
indis-pensable.The writing is clear and
straightfor-ward 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 PHP and MySQL Web Development
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 agoand 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 sothat the information is very palatable I amable 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.”
—Tim Luoma,TnTLuoma.com
“Welling and Thomson’s book is a goodreference for those who want to get togrips with practical projects straight off thebat It includes webmail, shopping cart,session control, and web-forum/weblogapplications as a matter of course, andbegins with a sturdy look at PHP first,moving to MySQL once the basics are covered.”
—twilight30 on Slashdot
Trang 3Thomson 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 PHP and 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 and MySQL 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
two of the most popular open-source Webdevelopment technologies….The projectsare the real jewel of the book Not only arethe projects described and constructed in alogical, component-based manner, but theselection of projects represents an excellentcross-section of common components thatare built into many web sites.”
—Craig Cecil
“The book takes an easy, step-by-stepapproach to introduce even the cluelessprogrammer to the language of PHP Ontop of that, I often find myself referringback to it in my Web design efforts I’m stilllearning new things about PHP, but thisbook gave me a solid foundation fromwhich to start and continues to help me tothis day.”
—Stephen Ward
“This book is one of few that reallytouched me and made me ‘love’ it I can’tput it in my bookshelf; I must put it in atouchable place on my working bench as Ialways like to refer from it Its structure isgood, wordings are simple and straight for-ward, and examples are clear and step bystep Before I read it, I knew nothing ofPHP and MySQL After reading it, I havethe confidence and skill to develop anycomplicated Web application.”
—Power Wong
“This book is God… I highly mend this book to anyone who wants tojump in the deep end with database drivenWeb application programming I wish morecomputer books were organized this way.”
recom-—Sean C Schertell
Trang 4PHP and MySQL ®
Web Development
Fourth Edition
Trang 6PHP and MySQL ®
Web Development
Luke Welling Laura Thomson
Fourth Edition
Upper Saddle River, NJ •Boston•Indianapolis•San Francisco
Trang 7All rights reserved No part of this book shall be reproduced, stored in a
retrieval system, or transmitted by any means, electronic, mechanical,
pho-tocopying, recording, or otherwise, without written permission from the
pub-lisher No patent liability is assumed with respect to the use of the
infor-mation contained herein Although every precaution has been taken in the
preparation of this book, the publisher and authors assume no
responsibil-ity for errors or omissions Neither is any liabilresponsibil-ity assumed for damages
resulting from the use of the information contained herein.
Library of Congress Cataloging-in-Publication Data
1 PHP (Computer program language) 2 SQL (Computer program language)
3 MySQL (Electronic resource) 4 Web sites Design I Thomson,
Laura II Title
QA76.73.P224W45 2008
005.2'762 dc22
2008036492 Printed in the United States of America
First Printing: September 2008
ISBN-10: 0-672-32916-6
ISBN-13: 978-0-672-32916-6
Trademarks
All terms mentioned in this book that are known to be trademarks or
serv-ice marks have been appropriately capitalized Pearson Education, Inc.
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.
Warning 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
lia-bility nor responsilia-bility 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
Pearson Education, Inc offers excellent discounts on this book when
ordered in quantity for bulk purchases or special sales For more
informa-tion, please contact
U.S Corporate and Government Sales
Trang 8To our Mums and Dads
❖
Trang 94 String Manipulation and Regular Expressions 107
5 Reusing Code and Writing Functions 133
7 Error and Exception Handling 193
II Using MySQL
8 Designing Your Web Database 207
9 Creating Your Web Database 219
11 Accessing Your MySQL Database from the Web
III E-commerce and Security
15 E-commerce Security Issues 341
16 Web Application Security 361
17 Implementing Authentication with PHP and
Trang 10IV Advanced PHP Techniques
19 Interacting with the File System and the Server431
20 Using Network and Protocol Functions 451
23 Using Session Control in PHP 509
24 Other Useful Features 525
V Building Practical PHP and MySQL Projects
25 Using PHP and MySQL for Large Projects 535
27 Building User Authentication and Personalization569
28 Building a Shopping Cart 607
29 Building a Web-Based Email Service 651
30 Building a Mailing List Manager 687
33 Connecting to Web Services with XML and SOAP807
34 Building Web 2.0 Applications with Ajax 855
VI Appendixes
Trang 11Introduction 1
I Using PHP
1 PHP Crash Course 13
Before You Begin: Accessing PHP 14
Creating a Sample Application: Bob’s Auto Parts 14Creating the Order Form 14
Processing the Form 16
Using the date() Function 22
Accessing Form Variables 23
Short, Medium, and Long Variables 23String Concatenation 26
Variables and Literals 27
Declaring and Using Constants 31
Understanding Variable Scope 31
Using Operators 32
Arithmetic Operators 33
String Operators 34
Trang 12Working Out the Form Totals 41
Understanding Precedence and Associativity 42Using Variable Functions 44
Testing and Setting Variable Types 44
Testing Variable Status 45
Next 57
2 Storing and Retrieving Data 59
Saving Data for Later 59
Storing and Retrieving Bob’s Orders 60
Processing Files 61
Opening a File 61
Choosing File Modes 61
Using fopen() to Open a File 62
Opening Files Through FTP or HTTP 64
Trang 13Writing to a File 67Parameters for fwrite() 68File Formats 68
Closing a File 69Reading from a File 71Opening a File for Reading: fopen() 72Knowing When to Stop: feof() 73Reading a Line at a Time: fgets(), fgetss(), andfgetcsv() 73
Reading the Whole File: readfile(), fpassthru(), andfile() 74
Reading a Character: fgetc() 75Reading an Arbitrary Length: fread() 75Using Other Useful File Functions 76Checking Whether a File Is There:
file_exists() 76Determining How Big a File Is: filesize() 76Deleting a File: unlink() 76
Navigating Inside a File: rewind(), fseek(), andftell() 76
Using Loops to Access the Array 84Arrays with Different Indices 85
Initializing an Array 85Accessing the Array Elements 85Using Loops 85
Trang 14xiii Contents
Loading Arrays from Files 98
Performing Other Array Manipulations 102
Navigating Within an Array: each(), current(),
reset(), end(), next(), pos(), and prev() 102
Applying Any Function to Each Element in an
Formatting Strings for Presentation 110
Formatting Strings for Storage: addslashes() and
Trang 15Comparing Strings 119Performing String Ordering: strcmp(),strcasecmp(), and strnatcmp() 119Testing String Length with strlen() 120Matching and Replacing Substrings with StringFunctions 120
Finding Strings in Strings: strstr(), strchr(),strrchr(), and stristr() 120
Finding the Position of a Substring: strpos() andstrrpos() 121
Replacing Substrings: str_replace() andsubstr_replace() 122
Introducing Regular Expressions 123The Basics 124
Character Sets and Classes 124Repetition 126
Subexpressions 126Counted Subexpressions 126Anchoring to the Beginning or End of a String 126
Branching 127Matching Literal Special Characters 127Reviewing the Special Characters 127Putting It All Together for the Smart Form 128Finding Substrings with Regular Expressions 129Replacing Substrings with Regular Expressions 130Splitting Strings with Regular Expressions 130Further Reading 131
Next 131
5 Reusing Code and Writing Functions 133
The Advantages of Reusing Code 133Cost 134
Reliability 134Consistency 134Using require() and include() 134Filename Extensions and require() 135
Trang 16xv Contents
Using auto_prepend_file and
auto_append_file 142
Using Functions in PHP 143
Calling Functions 143
Calling an Undefined Function 145
Understanding Case and Function Names 146
Defining Your Own Functions 146
Examining Basic Function Structure 146
Naming Your Function 147
Using Parameters 148
Understanding Scope 150
Passing by Reference Versus Passing by Value 153
Using the return Keyword 154
Returning Values from Functions 155
Understanding Object-Oriented Concepts 160
Classes and Objects 160
Using Class Attributes 164
Controlling Access with private and public 166
Calling Class Operations 167
Implementing Inheritance in PHP 168
Controlling Visibility Through Inheritance with
private and protected 169
Trang 17Overriding 170Preventing Inheritance and Overriding with final172
Understanding Multiple Inheritance 173Implementing Interfaces 173
Designing Classes 174Writing the Code for Your Class 175Understanding Advanced Object-OrientedFunctionality in PHP 183
Using Per-Class Constants 184Implementing Static Methods 184Checking Class Type and Type Hinting 184Late Static Bindings 185
Cloning Objects 186Using Abstract Classes 186Overloading Methods with call() 186Using autoload() 187
Implementing Iterators and Iteration 188Converting Your Classes to Strings 190Using the Reflection API 190
Next 191
7 Error and Exception Handling 193
Exception Handling Concepts 193The Exception Class 195
User-Defined Exceptions 196Exceptions in Bob’s Auto Parts 199Exceptions and PHP’s Other Error HandlingMechanisms 202
Further Reading 203Next 203
II Using MySQL
8 Designing Your Web Database 207
Relational Database Concepts 208
Trang 18xvii Contents
Designing Your Web Database 211
Think About the Real-World Objects You Are
Modeling 211
Avoid Storing Redundant Data 212
Use Atomic Column Values 214
Choose Sensible Keys 215
Think About What You Want to Ask the
Database 215
Avoid Designs with Many Empty
Attributes 215
Summary of Table Types 216
Web Database Architecture 216
Further Reading 218
Next 218
9 Creating Your Web Database 219
Using the MySQL Monitor 220
Logging In to MySQL 221
Creating Databases and Users 222
Setting Up Users and Privileges 223
Introducing MySQL’s Privilege System 223
Principle of Least Privilege 223
User Setup:The GRANT Command 223
Types and Levels of Privileges 225
The REVOKE Command 227
Examples Using GRANT and REVOKE 227
Setting Up a User for the Web 228
Using the Right Database 229
Creating Database Tables 229
Understanding What the Other Keywords
Mean 231
Trang 19Looking at the Database with SHOW andDESCRIBE 233
Creating Indexes 234Understanding MySQL Identifiers 235Choosing Column Data Types 236Numeric Types 236
Date and Time Types 238String Types 239
Further Reading 241Next 241
10 Working with Your MySQL Database 243
What Is SQL? 243Inserting Data into the Database 244Retrieving Data from the Database 246Retrieving Data with Specific Criteria 248Retrieving Data from Multiple Tables 249Retrieving Data in a Particular Order 255Grouping and Aggregating Data 256Choosing Which Rows to Return 258Using Subqueries 258
Updating Records in the Database 261Altering Tables After Creation 261Deleting Records from the Database 264Dropping Tables 264
Dropping a Whole Database 264Further Reading 265
Choosing a Database to Use 274
Trang 20xix Contents
Querying the Database 274
Retrieving the Query Results 275
Disconnecting from the Database 276
Putting New Information in the Database 276
Using Prepared Statements 280
Using Other PHP-Database Interfaces 282
Using a Generic Database Interface: PEAR
MDB2 282
Further Reading 285
Next 285
12 Advanced MySQL Administration 287
Understanding the Privilege System in Detail 287
The user Table 289
The db and host Tables 290
The tables_priv, columns_priv, and procs priv
Making Your MySQL Database Secure 294
MySQL from the Operating System’s Point of
View 294
Passwords 295
User Privileges 295
Web Issues 296
Getting More Information About Databases 296
Getting Information with SHOW 296
Getting Information About Columns with
Trang 21Using Indexes 305Using Default Values 305Other Tips 305
Backing Up Your MySQL Database 305Restoring Your MySQL Database 306Implementing Replication 306Setting Up the Master 307Performing the Initial Data Transfer 307Setting Up the Slave or Slaves 308Further Reading 309
Next 309
13 Advanced MySQL Programming 311
The LOAD DATA INFILE Statement 311Storage Engines 312
Transactions 313Understanding Transaction Definitions 313Using Transactions with InnoDB 314Foreign Keys 315
Stored Procedures 316Basic Example 316Local Variables 319Cursors and Control Structures 319Further Reading 323
Next 323
III E-commerce and Security
14 Running an E-commerce Site 327
Deciding What You Want to Achieve 327Considering the Types of Commercial Websites 327Publishing Information Using Online
Brochures 328Taking Orders for Goods or Services 331
Trang 22xxi Contents
Providing Services and Digital Goods 334
Adding Value to Goods or Services 335
Cutting Costs 335
Understanding Risks and Threats 336
Crackers 337
Failure to Attract Sufficient Business 337
Computer Hardware Failure 337
Power, Communication, Network, or Shipping
Failures 338
Extensive Competition 338
Software Errors 338
Evolving Governmental Policies and Taxes 339
System Capacity Limits 339
Choosing a Strategy 339
Next 339
15 E-commerce Security Issues 341
How Important Is Your Information? 342
Security Threats 342
Exposure of Confidential Data 343
Loss or Destruction of Data 344
Modification of Data 345
Denial of Service 346
Errors in Software 347
Repudiation 348
Usability, Performance, Cost, and Security 349
Creating a Security Policy 349
Authentication Principles 350
Encryption Basics 351
Private Key Encryption 353
Public Key Encryption 353
Digital Signatures 354
Digital Certificates 355
Secure Web Servers 356
Auditing and Logging 357
Trang 23Firewalls 357Data Backups 358Backing Up General Files 358Backing Up and Restoring Your MySQLDatabase 358
Physical Security 359Next 359
16 Web Application Security 361
Strategies for Dealing with Security 361Start with the Right Mindset 362Balancing Security and Usability 362Monitoring Security 363
Our Basic Approach 363Identifying the Threats We Face 363Access to or Modification of Sensitive Data 363Loss or Destruction of Data 364
Denial of Service 364Malicious Code Injection 365Compromised Server 365Understanding Who We’re Dealing With 365Crackers 366
Unwitting Users of Infected Machines 366Disgruntled Employees 366
Hardware Thieves 366Ourselves 366Securing Your Code 367Filtering User Input 367Escaping Output 371Code Organization 374What Goes in Your Code 374File System Considerations 375Code Stability and Bugs 376Execution Quotes and exec 377Securing Your Web Server and PHP 378Keep Software Up-to-Date 378
Trang 24xxiii Contents
Web Server Configuration 380
Commercially Hosted Web Applications 382
Database Server Security 383
Users and the Permissions System 383
Sending Data to the Server 384
Connecting to the Server 384
Running the Server 385
Protecting the Network 385
Install Firewalls 386
Use a DMZ 386
Prepare for DoS and DDoS Attacks 387
Computer and Operating System Security 387
Keep the Operating System Up-to-Date 387
Run Only What Is Necessary 388
Physically Secure the Server 388
Protecting Multiple Pages 399
Using Basic Authentication 399
Using Basic Authentication in PHP 400
Using Basic Authentication with Apache’s htaccess
Trang 2518 Implementing Secure Transactions with PHP and MySQL 409
Providing Secure Transactions 409The User’s Machine 410The Internet 411Your System 412Using Secure Sockets Layer (SSL) 413Screening User Input 417
Providing Secure Storage 417Storing Credit Card Numbers 419Using Encryption in PHP 419Installing GPG 420Testing GPG 422Further Reading 427Next 428
IV Advanced PHP Techniques
19 Interacting with the File System and the Server 431
Uploading Files 431HTML for File Upload 433Writing the PHP to Deal with the File 434Avoiding Common Upload Problems 438Using Directory Functions 439
Reading from Directories 439Getting Information About the CurrentDirectory 442
Creating and Deleting Directories 443Interacting with the File System 443Getting File Information 444Changing File Properties 446Creating, Deleting, and Moving Files 447Using Program Execution Functions 447
Trang 26xxv Contents
Interacting with the Environment: getenv() and
putenv() 450
Further Reading 450
Next 450
20 Using Network and Protocol Functions 451
Examining Available Protocols 451
Sending and Reading Email 452
Using Data from Other Websites 452
Using Network Lookup Functions 455
Backing Up or Mirroring a File 459
Using FTP to Back Up or Mirror a File 459
21 Managing the Date and Time 469
Getting the Date and Time from PHP 469
Using the date() Function 469
Dealing with Unix Timestamps 471
Using the getdate() Function 473
Validating Dates with checkdate() 474
Trang 2722 Generating Images 483
Setting Up Image Support in PHP 484Understanding Image Formats 484JPEG 485
PNG 485WBMP 485GIF 485Creating Images 486Creating a Canvas Image 487Drawing or Printing Text on the Image 487Outputting the Final Graphic 489
Cleaning Up 490Using Automatically Generated Images in Other Pages 490
Using Text and Fonts to Create Images 491Setting Up the Base Canvas 495Fitting the Text onto the Button 495Positioning the Text 498
Writing the Text onto the Button 499Finishing Up 499
Drawing Figures and Graphing Data 499Using Other Image Functions 507Further Reading 507
Next 508
23 Using Session Control in PHP 509
What Is Session Control? 509Understanding Basic Session Functionality 509What Is a Cookie? 510
Setting Cookies from PHP 510Using Cookies with Sessions 511Storing the Session ID 511Implementing Simple Sessions 512Starting a Session 512Registering Session Variables 513
Trang 28xxvii Contents
Using Session Variables 513
Unsetting Variables and Destroying the
Session 513
Creating a Simple Session Example 514
Configuring Session Control 516
Implementing Authentication with Session
Control 517
Further Reading 524
Next 524
24 Other Useful Features 525
Evaluating Strings: eval() 525
Terminating Execution: die() and exit() 526
Serializing Variables and Objects 526
Getting Information About the PHP
Environment 528
Finding Out What Extensions Are Loaded 528
Identifying the Script Owner 529
Finding Out When the Script Was
Modified 529
Temporarily Altering the Runtime Environment 529
Highlighting Source Code 530
Using PHP on the Command Line 531
Trang 29Using a Standard Directory Structure 542Documenting and Sharing In-House Functions 542
Implementing Version Control 542Choosing a Development Environment 544Documenting Your Projects 544
Prototyping 545Separating Logic and Content 546Optimizing Code 546
Using Simple Optimizations 547Using Zend Products 547Testing 548
Further Reading 549Next 549
26 Debugging 551
Programming Errors 551Syntax Errors 552Runtime Errors 553Logic Errors 558Variable Debugging Aid 559Error Reporting Levels 562Altering the Error Reporting Settings 563Triggering Your Own Errors 564
Handling Errors Gracefully 565Next 567
27 Building User Authentication and
Personalization 569
Solution Components 569User Identification and Personalization 570Storing Bookmarks 571
Recommending Bookmarks 571Solution Overview 571
Implementing the Database 573
Trang 30xxix Contents
Implementing the Basic Site 574
Implementing User Authentication 577
Registering Users 577
Logging In 584
Logging Out 587
Changing Passwords 588
Resetting Forgotten Passwords 591
Implementing Bookmark Storage and Retrieval 596
Building an Online Catalog 608
Tracking Users’ Purchases While They
Shop 608
Implementing a Payment System 608
Building an Administration Interface 609
Solution Overview 609
Implementing the Database 612
Implementing the Online Catalog 615
Listing Categories 617
Listing Books in a Category 620
Showing Book Details 622
Implementing the Shopping Cart 623
Using the show_cart.php Script 623
Viewing the Cart 627
Adding Items to the Cart 630
Saving the Updated Cart 631
Printing a Header Bar Summary 632
Checking Out 633
Trang 31Implementing Payment 639Implementing an Administration Interface 641Extending the Project 650
Using an Existing System 650Next 650
29 Building a Web-Based Email Service 651
Solution Components 651Mail Protocols: POP3 Versus IMAP 651POP3 and IMAP Support in PHP 652Solution Overview 654
Setting Up the Database 655Examining the Script Architecture 657Logging In and Out 663
Setting Up Accounts 666Creating a New Account 668Modifying an Existing Account 670Deleting an Account 670
Reading Mail 671Selecting an Account 671Viewing Mailbox Contents 674Reading a Mail Message 677Viewing Message Headers 680Deleting Mail 681
Sending Mail 682Sending a New Message 682Replying To or Forwarding Mail 684Extending the Project 686
Next 686
30 Building a Mailing List Manager 687
Solution Components 687Setting Up a Database of Lists and Subscribers688
Uploading Newsletters 688Sending Mail with Attachments 689
Trang 32xxxi Contents
Solution Overview 689
Setting Up the Database 692
Defining the Script Architecture 694
Viewing List Information 713
Viewing List Archives 716
Subscribing and Unsubscribing 717
Changing Account Settings 719
Changing Passwords 719
Logging Out 721
Implementing Administrative Functions 721
Creating a New List 722
Uploading a New Newsletter 724
Handling Multiple File Upload 727
Previewing the Newsletter 732
Sending the Message 733
Extending the Project 740
Next 740
31 Building Web Forums 741
Understanding the Process 741
Solution Components 742
Solution Overview 743
Designing the Database 744
Viewing the Tree of Articles 747
Expanding and Collapsing 749
Displaying the Articles 752
Using the treenode Class 753
Viewing Individual Articles 760
Adding New Articles 762
Adding Extensions 769
Trang 33Using an Existing System 770Next 770
32 Generating Personalized PDF
Documents 771
Project Overview 771Evaluating Document Formats 772Solution Components 776
Question and Answer System 776Document Generation Software 776Solution Overview 778
Asking the Questions 780Grading the Answers 782Generating an RTF Certificate 784Generating a PDF Certificate from a Template 788
Generating a PDF Document Using PDFlib 792
A Hello World Script for PDFlib 792Generating a Certificate with PDFlib 796Handling Problems with Headers 804
Extending the Project 805Next 805
33 Connecting to Web Services with XML and SOAP 807
Project Overview:Working with XML and WebServices 807
Understanding XML 808Understanding Web Services 811Solution Components 813
Using Amazon’s Web Services Interfaces 813Parsing XML: REST Responses 814Using SOAP with PHP 814Caching 815
Trang 34xxxiii Contents
Solution Overview 815
Core Application 820
Showing Books in a Category 826
Getting an AmazonResultSet Class 828
Using REST to Make a Request and Retrieve a
Result 838
Using SOAP to Make a Request and Retrieve a
Result 845
Caching the Data from a Request 846
Building the Shopping Cart 849
Checking Out to Amazon 852
Installing the Project Code 853
Extending the Project 854
The XMLHTTPRequest Object 860
Communicating with the Server 862
Working with the Server Response 864
Putting It All Together 866
Adding Ajax Elements to Earlier Projects 870
Adding Ajax Elements to PHPbookmark 870
For More Information 884
Trang 35Learning More about the Document ObjectModel (DOM) 884
JavaScript Libraries for Ajax Applications 884Ajax Developer Websites 885
Appendixes
A Installing PHP and MySQL 889
Installing Apache, PHP, and MySQL Under Unix 890Binary Installation 890
Source Installation 891httpd.conf File: Snippets 896
Is PHP Support Working? 897
Is SSL Working? 898Installing Apache, PHP, and MySQL Under Windows 899
Installing MySQL Under Windows 900Installing Apache Under Windows 901Installing PHP for Windows 903Installing PEAR 905
Setting Up Other Configurations 906
B Web Resources 907
PHP Resources 907MySQL and SQL Specific Resources 909Apache Resources 909
Web Development 910
Index 911
Trang 36Lead Authors
Laura Thomsonis a senior software engineer at Mozilla Corporation She was
former-ly a principal at both OmniTI and Tangled Web Design, and she has worked for RMITUniversity 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 In her spare time she enjoys riding horses, arguingabout free and open source software, and sleeping
Luke Wellingis a web architect at OmniTI and regularly speaks on open source andweb development topics at conferences such as OSCON, ZendCon, MySQLUC,PHPCon, OSDC, and LinuxTag Prior to joining OmniTI, he worked for the web ana-lytics company Hitwise.com, at the database vendor MySQL AB, and as an independentconsultant at Tangled Web Design He has taught computer science at RMIT University
in Melbourne, Australia, and holds a Bachelor of Applied Science (Computer Science)degree In his spare time, he attempts to perfect his insomnia
Contributing Authors
Julie C Meloniis the technical director for i2i Interactive (www.i2ii.com), a dia company located in Los Altos, California She has 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 has authored numerous books and articles
multime-on web-based programming languages and database topics, including the bestselling Sams
Teach Yourself PHP, MySQL, and Apache All in One.
Adam DeFieldsis a consultant specializing in web application development, projectmanagement, and instructional design He lives in Grand Rapids, Michigan where heruns Emanation Systems, LLC, (www.emanationsystemsllc.com) a company he founded
in 2002 He has been involved with web development projects using several differenttechnologies, but has developed a strong preference toward PHP/MySQL-based projects
Marc Wandschneideris a freelance software developer, author, and speaker who travelsthe globe working on interesting projects In recent years, a lot of his attention hasbeen focused on writing robust and scalable web applications, and in 2005 he wrote a
book called Core Web Application Programming with PHP and MySQL He was was
previ-ously the main developer of the SWiK (http://swik.net) open source community site.Marc currently lives in Beijing where he spends his time mangling the Chinese languageand programming
Trang 37We would like to thank the team at Pearson for all their hard work In particular, wewould like to thank Shelley Johnston, without whose dedication and patience the firstthree editions of this book would not have been possible, and Mark Taber, who has takenover for the fourth edition.
We appreciate immensely the work done by the PHP and MySQL developmentteams.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 in 1998
He said we would like PHP, and it seems he was right
Finally, we would like to thank our family and friends for putting up with us while
we have been repeatedly antisocial while working on books Specifically, thank you foryour support to our family members: Julie, Robert, Martin, Lesley, Adam, Paul, Archer,and Barton
Trang 38We Want to Hear from You!
As the reader of this book, you are our most important critic and commentator.We value
your opinion and want to know what we’re doing right, what we could do better, whatareas you’d like to see us publish in, and any other words of wisdom you’re willing topass our way
You can email or write me directly to let me know what you did or didn’t like aboutthis book—as well as what we can do to make our books stronger
Please note that I cannot help you with technical problems related to the topic of this book, and that due to the high volume of mail I receive, I might not be able to reply to every message.
When you write, please be sure to include this book’s title and authors as well as yourname and phone or email address I will carefully review your comments and share themwith the authors and editors who worked on the book
Email: feedback@developers-library.info
Mail: Mark Taber
Associate PublisherPearson Education, Inc
800 East 96th StreetIndianapolis, IN 46240 USA
Reader Services
Visit our website and register this book at informit.com/register for convenient access toany updates, downloads, or errata that might be available for this book
Trang 40WELCOME TOPHP AND M Y SQL W EB D EVELOPMENT Within its pages, you will finddistilled knowledge from our experiences using PHP and MySQL, two of the hottestweb development tools around
In this introduction, we cover
n Why you should read this book
n What you will be able to achieve using this book
n What PHP and MySQL are and why they’re great
n What’s changed in the latest versions of PHP and MySQL
n How this book is organized
Let’s get started
Why You Should Read This Book
This book will teach you how to create interactive websites from the simplest orderform through to complex, secure e-commerce sites or interactive Web 2.0 sites.What’smore, you’ll learn how to do it using open source technologies
This book is aimed at readers who already know at least the basics of HTML andhave done some programming in a modern programming language before but have notnecessarily programmed for the Internet or used a relational database If you are a begin-ning programmer, you should still find this book useful, but digesting it might take a lit-tle longer.We’ve tried not to leave out any basic concepts, but we do cover them atspeed.The typical readers of this book want to master PHP and MySQL for the purpose
of building a large or commercial website.You might already be working in another webdevelopment language; if so, this book should get you up to speed quickly
We wrote the first edition of this book because we were tired of finding PHP booksthat were basically function references.These books are useful, but they don’t help whenyour boss or client has said, “Go build me a shopping cart.” In this book, we have doneour best to make every example useful.You can use many of the code samples directly inyour website, and you can use many others with only minor modifications