Tải bản đầy đủ (.pdf) (10 trang)

Phát triển web với PHP và MySQL - p 3 pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (417.75 KB, 10 trang )

CONTENTS
Previewing the Newsletter 703
Sending the Message 704
Extending the Project 709
Next 709
29 Building Web Forums 711
The Problem 712
Solution Components 712
Solution Overview 714
Designing the Database 716
Viewing the Tree of Articles 718
Expanding and Collapsing 721
Displaying the Articles 724
Using the treenode Class 725
Viewing Individual Articles 731
Adding New Articles 734
Extensions 741
Using an Existing System 741
Next 742
30 Generating Personalized Documents in Portable
Format (PDF) 743
The Problem 744
Evaluating Document Formats 745
Paper 745
ASCII 745
HTML 745
Word Processor Formats 746
Rich Text Format 746
PostScript 747
Portable Document Format 748
Solution Components 749


Question and Answer System 749
Document Generation Software 749
Solution Overview 752
Asking the Questions 753
Grading the Answers 755
Generating an RTF Certificate 758
Generating a PDF Certificate from a Template 762
Generating a PDF Document Using PDFlib 765
A Hello World Script for PDFlib 766
Generating Our Certificate with PDFlib 770
xxi
00 7842 FM 3/6/01 3:38 PM Page xxi
PHP AND MYSQL WEB DEVELOPMENT
Problems with Headers 777
Extending the Project 778
Further Reading 778
PART VI Appendixes 779
A Installing PHP 4 and MySQL 781
Running PHP as a CGI Interpreter or Module 782
Installing Apache, PHP, and MySQL Under UNIX 783
Apache and mod_SSL 787
httpd.conf File—Snippets 790
Is SSL Working? 792
Installing Apache, PHP, and MySQL Under Windows 793
Installing MySQL Under Windows 793
Installing Apache Under Windows 795
Differences Between Apache for Windows and UNIX 798
Installing PHP for Windows 799
Installation Notes for Microsoft IIS 800
Installation Notes for Microsoft PWS 802

Other Configurations 802
B Web Resources 803
PHP Resources 804
MySQL and SQL Specific Resources 806
Apache Resources 806
Web Development 806
Index 807
xxii
00 7842 FM 3/6/01 3:38 PM Page xxii
About the Authors
Laura Thomson is a lecturer in Web programming in the Department of Computer Science at
RMIT University in Melbourne, Australia. She is also a partner in the award-winning Web
development 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 contacted at
Luke Welling is a lecturer in software engineering and e-commerce in the School of Electrical
and Computer Systems Engineering at RMIT University in Melbourne, Australia. He is also a
partner in Tangled Web Design. He holds a Bachelor of Applied Science (Computer Science)
degree and is currently completing a master’s degree in Genetic Algorithms for Communication
Network Design. In his spare time, he attempts to perfect his insomnia. Luke can be contacted
at
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. He obtained a master’s degree in Electrical Engineering from Georgia Tech in
Atlanta, Georgia in 1998. He is the author of numerous articles about Linux, Apache, PHP, and
MySQL and can be reached via email at
Chris Newman is a consultant programmer specializing in the development of dynamic

Internet applications. He has extensive commercial experience in using PHP and MySQL to
produce a wide range of applications for an international client base. A graduate of Keele
University, he lives in Stoke-on-Trent, England, where he runs Lightwood Consultancy Ltd.
More information on Lightwood Consultancy Ltd can be found at
, and Newman can be contacted at
00 7842 FM 3/6/01 3:38 PM Page xxiii
Dedication
To our Mums and Dads.
Acknowledgments
We would like to thank the team at Sams for all their hard work. In particular, we would like to
thank Shelley Johnston Markanday without whose 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 MySQL development 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 in 1998. We also
thank James Woods and all the staff at Law Partners for giving us such interesting work to test
the boundaries of PHP with.
Finally, we would like to thank our family and friends for putting up with us while we have
been antisocial for the better part of a year. Specifically, thank you for your support to our
family members: Julie, Robert, Martin, Lesley, Adam, Paul, Sandi, James, and Archer.
00 7842 FM 3/6/01 3:38 PM Page xxiv
Tell Us What You Think!
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, what areas you’d
like to see us publish in, and any other words of wisdom you’re willing to pass our way.
You can email or write me directly to let me know what you did or didn’t like about this
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 author as well as your name
and phone or email address. I will carefully review your comments and share them with the
author and editors who worked on the book.
E-mail:
Mail: Mark Taber
Associate Publisher
Sams Publishing
201 West 103rd Street
Indianapolis, IN 46290 USA
00 7842 FM 3/6/01 3:38 PM Page xxv
00 7842 FM 3/6/01 3:38 PM Page xxvi
Introduction
Welcome to PHP and MySQL Web Development. Within its pages, you will find distilled
knowledge from our experiences using PHP and MySQL, two of the hottest Web development
tools around.
In this introduction, we’ll cover
• Why you should read this book
• What you will be able to achieve using this book
• What PHP and MySQL are and why they’re great
• An overview of the new features of PHP 4
• How this book is organized
Let’s get started.
Why You Should Read This Book
This book will teach you how to create interactive Web sites from the simplest order form
through to complex secure e-commerce sites. What’s more, 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 and have done
some programming in a modern programming language before, but have not necessarily pro-

grammed for the Internet or used a relational database. If you are a beginning programmer, you
should still find this book useful, but it might take you a little longer to digest. We’ve tried not
to leave out any basic concepts, but we do cover them at speed. The typical reader of this book
is someone who wants to master PHP and MySQL for the purpose of building a large or com-
mercial Web site. You might already be working in another Web development language; if so,
this book should get you up to speed quickly.
We wrote this book because we were tired of finding books on PHP that were basically a func-
tion reference. These books are useful, but they don’t help when your boss or client has said
“Go build me a shopping cart.” We have done our best to make every example useful. Many of
the code samples can be directly used in your Web site, and many others can be used with
minor modifications.
What You Will Be Able to Achieve Using This Book
Reading this book will enable you to build real-world, dynamic Web sites. If you’ve built Web
sites using plain HTML, you will realize the limitations of this approach. Static content from a
pure HTML Web site is just that—static. It stays the same unless you physically update it. Your
users can’t interact with the site in any meaningful fashion.
01 7842 intro 3/6/01 3:33 PM Page 1
PHP AND MYSQL WEB DEVELOPMENT
Using a language such 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 chap-
ters. We’ll begin by looking at a simple online ordering system, and work our way through the
various parts of PHP and MySQL.
We will then discuss aspects of electronic commerce and security as they relate to building a real-
world Web site, and show you how to implement these aspects in PHP and MySQL.
In the final section of this book, we will talk about how to approach real-world projects, and take
you through the design, planning, and building of the following seven projects:
• User authentication and personalization
• Shopping carts
• Content management systems

• Web-based email
• Mailing list managers
• Web forums
• Document generation
Any of these projects should be usable as is, or can be modified to suit your needs. We chose them
because we believe they represent seven of the most common Web-based applications built by
programmers. If your needs are different, this book should help you along the way to achieving
your goals.
What Is PHP?
PHP is a server-side scripting language designed specifically for the Web. Within an HTML page,
you can embed PHP code that will be executed each time the page is visited. Your PHP code is
interpreted at the Web server and generates HTML or other output that the visitor will see.
PHP was conceived in 1994 and was originally the work of one man, Rasmus Lerdorf. It was
adopted by other talented people and has gone through three major rewrites to bring us the broad,
mature product we see today. As of January 2001, it was in use on nearly five million domains
worldwide, and this number is growing rapidly. You can see the current number at
http://www.
php.net/usage.php
PHP is an Open Source product. You have access to the source code. You can use it, alter it, and
redistribute it all without charge.
PHP originally stood for Personal Home Page, but was changed in line with the GNU recursive
naming convention (GNU = Gnu’s Not Unix) and now stands for PHP Hypertext Preprocessor.
The current major version of PHP is 4. This version has seen some major improvements to the
language, discussed in the next section.
2
01 7842 intro 3/6/01 3:33 PM Page 2
INTRODUCTION
The home page for PHP is available at
The home page for Zend is at
What’s New In PHP Version 4?

If you have used PHP before, you will notice a few important improvements in version 4. In this
new version
• PHP 4 is much faster than previous versions because it uses the new Zend Engine. If you
need even higher performance, you can obtain the Zend Optimizer, Zend Cache, or Zend
Compiler from .
• You have always been able to use PHP as an efficient module for the Apache server. With
this new version, you can install PHP as an ISAPI module for Microsoft’s Internet
Information Server.
• Session support is now built in. In previous versions, you needed to install the PHPlib add-
on for session control or write your own.
What Is MySQL?
MySQL (pronounced My-Ess-Que-Ell) is a very fast, robust, relational database management sys-
tem (RDBMS). A database enables you to efficiently store, search, sort, and retrieve data. The
MySQL server controls access to your data to ensure that multiple users can work with it concur-
rently, to provide fast access to it, and ensure that only authorized users can obtain access. Hence,
MySQL is a multi-user, multi-threaded server. It uses SQL (Structured Query Language), the stan-
dard database query language worldwide. MySQL has been publicly available since 1996, but has
a development history going back to 1979. It has now won the Linux Journal Readers’ Choice
Award three years running.
MySQL is now available under an Open Source license, but commercial licenses are also available
if required.
Why Use PHP and MySQL?
When setting out to build an e-commerce site, there are many different products that you could use.
You will need to choose hardware for the Web server, an operating system, Web server software, a
database management system, and a programming or scripting language.
Some of these choices will be dependent on the others. For example, not all operating systems will
run on all hardware, not all scripting languages can connect to all databases, and so on.
In this book, we do not pay much attention to your hardware, operating system, or Web server
software. We don’t need to. One of the nice features of PHP is that it is available for Microsoft
Windows, for many versions of UNIX, and with any fully-functional Web server. MySQL is

similarly versatile.
3
01 7842 intro 3/6/01 3:33 PM Page 3
PHP AND MYSQL WEB DEVELOPMENT
To demonstrate this, the examples in this book have been written and tested on two popular setups:
• Linux using the Apache Web server
• Microsoft Windows 2000 using Microsoft Internet Information Server (IIS)
Whatever hardware, operating system, and Web server you choose, we believe you should seri-
ously consider using PHP and MySQL.
Some of PHP’s Strengths
Some of PHP’s main competitors are Perl, Microsoft Active Server Pages (ASP), Java Server
Pages (JSP), and Allaire Cold Fusion.
In comparison to these products, PHP has many strengths including the following:
• High performance
• Interfaces to many different database systems
• Built-in libraries for many common Web tasks
• Low cost
• Ease of learning and use
• Portability
• Availability of source code
A more detailed discussion of these strengths follows.
Performance
PHP is very efficient. Using a single inexpensive server, you can serve millions of hits per day.
Benchmarks published by Zend Technologies () show PHP outperforming
its competition.
Database Integration
PHP has native connections available to many database systems. In addition to MySQL, you can
directly connect to PostgreSQL, mSQL, Oracle, dbm, filePro, Hyperwave, Informix, InterBase,
and Sybase databases, among others.
Using the Open Database Connectivity Standard (ODBC), you can connect to any database that

provides an ODBC driver. This includes Microsoft products, and many others.
Built-in Libraries
Because PHP was designed for use on the Web, it has many built-in functions for performing
many useful Web-related tasks. You can generate GIF images on-the-fly, connect to other net-
work services, send email, work with cookies, and generate PDF documents, all with just a few
lines of code.
4
01 7842 intro 3/6/01 3:33 PM Page 4

×