<<

. 3
( 132 .)



>>

What do we need to prevent? . . . . . . . . . . . . . . . . . . . . . . . . 402
The Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Code Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
The object-oriented approach . . . . . . . . . . . . . . . . . . . . . . . . 408
Accessing the file system . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Uploading files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Code Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Objects in theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Sample script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Contents xxi

Chapter 13 Problem-Tracking System . . . . . . . . . . . . . . . . . . . . . . 441
Determining the Scope and Goals of the Application . . . . . 441
What do you need? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
What do you need to prevent? . . . . . . . . . . . . . . . . . . . . . . . 444
Designing the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Code Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Code Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Reusable functions from /book/tracking/functions.php . . . . . . 453
Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
Chapter 14 Shopping Cart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Determining the Scope and Goals of the Application . . . . 477
What do you need? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
What do you need to prevent? . . . . . . . . . . . . . . . . . . . . . . . 479
The Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
Configuration Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
Configuring for encryption and security . . . . . . . . . . . . . . . . 480
Configuring Apache for credit-card authorization . . . . . . . . . . 482
Configuring for session handling . . . . . . . . . . . . . . . . . . . . . 483
Code Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
Session functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
Dealing with the credit-card processor . . . . . . . . . . . . . . . . . . 486
Code Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
Chapter 15 XML Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
Scope and Goals of Application . . . . . . . . . . . . . . . . . . . . . 506
Code Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
An introduction to parsers . . . . . . . . . . . . . . . . . . . . . . . . . . 508
Using Simplexml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
Code Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
Laying the groundwork . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
Chapter 16 SOAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Overview of SOAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
The SOAP envelope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
The SOAP body . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
A typical request/response pair . . . . . . . . . . . . . . . . . . . . . . . 522
Code Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
The essence of NuSOAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
A simple NuSOAP service call . . . . . . . . . . . . . . . . . . . . . . . 526
xxii Contents

Determining the Goals of the Application . . . . . . . . . . . . . 527
Code Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
The Barnes & Noble application . . . . . . . . . . . . . . . . . . . . . . 528
The Babelfish application . . . . . . . . . . . . . . . . . . . . . . . . . . 532
Writing a SOAP server application . . . . . . . . . . . . . . . . . . . . 534
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535
Chapter 17 Project Management . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
Determining the Goals of the Application . . . . . . . . . . . . . 537
Necessary pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
Designing the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
User types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
Application users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
Project and task status . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
Project-user mappings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Code Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
Logging in and establishing a session . . . . . . . . . . . . . . . . . . 544
Showing active projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
Creating a new project . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Uploading a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Viewing a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Adding a user . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Code Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Session management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
Viewing projects™ status . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
Uploading a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552
Displaying the contents of a file . . . . . . . . . . . . . . . . . . . . . . 553
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553

Part V Appendixes

Appendix A What™s on the CD-ROM . . . . . . . . . . . . . . . . . . . . 557
Appendix B HTML Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
Appendix C Brief Guide to MySQL/PHP Installation and
Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . 571
Appendix D MySQL Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . 583
Appendix E MySQL User Administration . . . . . . . . . . . . . . . . 597
Appendix F PHP Function Reference . . . . . . . . . . . . . . . . . . . 607
Appendix G Regular Expressions Overview . . . . . . . . . . . . . . . 659
Appendix H Helpful User-Defined Functions . . . . . . . . . . . . . . 669
Contents xxiii

Appendix I PHP and MySQL Resources . . . . . . . . . . . . . . . . . 691
Appendix J MySQL Function Reference . . . . . . . . . . . . . . . . . 697

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735

End-User License Agreement . . . . . . . . . . . . . . . . . . . . 765
Introduction
Soon we will head off on a fabulous journey, a journey on which we will explore
the ins and outs of MySQL and PHP database applications in great detail. It™s going
to be a fun trip; we just know it.
Okay, maybe we™re being a bit optimistic. If you™re anything like us, points of
this particular journey will be a lot more tedious than they are exciting. Let™s face
facts: Application development isn™t always the most exciting thing in the world.
And as with any other venture that involves programming, some very frustrating
times are sure to be ahead, whether because of a syntax error you can™t find or a
piece of code that won™t do what you think it ought to do. But despite all that, here
you are, and we think there is a very good reason for that.
Web applications are the present and the future. No matter your background,
whether you have a history with Visual Basic or COBOL, or maybe just some HTML
and JavaScript, your r©sum© is only going to improve with some Web application
development experience. We don™t think there™s a better combination of tools to
have under your belt than PHP and MySQL. The numbers bear us out. PHP and
MySQL are becoming increasingly popular, and the demand for people who can use
these tools will only increase.
A bit later in this introduction we go into a more detailed explanation of why
you should use PHP and MySQL. However, before we can get to that, we want take
a bit of time to go over the architecture of Web applications. Once we™ve done this,
we can explain in detail why PHP and MySQL should be the centerpieces of your
application-development environment. Once we™ve sold you on these tools, we pre-
sent a very quick and grossly under-coded application. As you look over this appli-
cation, you can see the basic syntax and principles behind PHP and MySQL.


As we proceed with the book, we assume that you have read and under-
stand everything presented in this introduction.




Basic Architecture
At the most basic level, the Web works off a client/server architecture. Simply
stated, that means that both a central server and a client application are responsi-
ble for some amount of processing. This setup differs from that of a program such
as Microsoft Word, which operates just fine without any help from a server. Those
of you who used older VAX machines might remember the days of dumb terminals,
which had no processing power whatsoever. Depending on where you work today,
xxv
xxvi Introduction

perhaps in a university or a bank, you might still use applications that are in no
way dependent on the client. In other words, applications in which all the work is
done on the central computer.

The client
In this book, you™ll learn how to use MySQL and PHP to create applications that
make use of a single client: the Web browser. This is not the only possibility for
Internet-based applications. For very sophisticated applications that require more
client-side processing or that need to maintain state (we talk about maintaining
state later in the introduction) a Java applet may be necessary. But unless you™re
coding something like a real-time chat program, client-side Java is completely
unnecessary.
So the only client you need to be concerned with is the Web browser. The appli-
cations need to generate HTML to be rendered in the browser. As you probably
already know, the primary language of browsers is the Hypertext Markup
Language, or HTML. HTML provides a set of tags that describe how a Web page
should look. If you are new to the concept of HTML, get on the Web and read one
of the many tutorials out there. It shouldn™t take long to learn the basics. Some of
the best include:

— National Center for Supercomputer Applications (http://archive.
ncsa.uiuc.edu/General/Internet/WWW/HTMLPrimer.html)

— Davesite (http://www.davesite.com/webstation/html/)

— Webmonkey (http://hotwired.lycos.com/webmonkey/teachingtool/)

Of course, most browsers accept more than HTML. All kinds of plug-ins, includ-
ing RealPlayer, Flash, and Shockwave, are available, and most browsers also have
some level of support for JavaScript. Some can even work with XML. But, like most
Web developers, we are taking a lowest-common-denominator approach in this
book. We™re going to create applications that can be read in any browser. We don™t
use JavaScript, XML, or anything else that could prevent some users from render-
ing the pages we serve. HTML it is.

The server
Almost all of the work of Web applications takes place on the server. A specific
application, called a Web server, is responsible for communicating with the
browser. A relational-database server stores whatever information the application
requires. Finally, you need a language to broker requests between the Web server
and the database server; it is also used to perform programmatic tasks on the infor-
mation that comes to and from the Web server. Figure I-1 represents this system.
Introduction xxvii


Relational
Database
(MySQL, Oracle, MS
SQL)




Middleware
Web Server PHP, ColdFusion
(Apache, IIS) ASP, JSP




Internet




Web Browser
(Internet Explore
Netscape)


Figure I-1: Architecture of Web applications


But, of course, none of this is possible without an operating system. The Web
server, programming language, and database server you use must work well with
your operating system.

THE OPERATING SYSTEM
Many operating systems are out there. Windows 98/2000/CE/XP and Macintosh OS
are probably the most popular. But that™s hardly the end of it. Circumstances might
have forced you to work with some obscure OS for the past few years. You might
even be under the impression that your OS is the best thing going. That™s fine. But
if you™re planning on spending a lot of time on the Web and on running applica-
tions, you™re best off getting to know either Windows NT/2000 or some version of
Unix. These two account for well over 90 percent of all the servers on the Web. It is
probably easier for you to learn a little NT/2000/XP or Unix than it is to convince
everybody else that the AS/400 is the way to go.
xxviii Introduction


Apple™s Mac OS X is based on a FreeBSD/Unix foundation and gives you
everything you expect from a Unix OS.




Which should you use? Well, this question is a complex one, and the answer for
many will be based partially on philosophical approach. In case you™re unaware of
it, let™s take a moment to talk about the broad topics in this philosophical debate.
If you don™t know what we are talking about, here are the basics. PHP and
MySQL belong to a class of software known as open source. This means that the
source code for the heart of the applications is available to anyone who wants to
see it. These applications make use of an open-source development model, which
enables anyone who is interested to participate in the development of the project.
In the case of PHP, coders all over the world participate in the development of the
language and see no immediate pay for their substantial work. Most of the people
who participate are passionate about good software and code for the enjoyment of
seeing people like you and me develop with their tools.
This method of development has been around for some time, but it has gained
prominence as Linux has become increasingly popular. More often than not, open-
source software is free of charge. You can download the application, install it, and
use it without getting permission from anyone or paying a dime to anyone.
Suffice it to say that Microsoft, Oracle, and other traditional software companies
do not make use of this method of development.
If you are not an open-source adherent, there are excellent reasons for choosing
NT/2000/XP. Usually, the thing that steers people towards NT/2000/XP is inertia. If
you or your company has been developing with Microsoft products for years, it is
probably going to be easier to stay within that environment than to make a change.
If you have a team of people who know Visual Basic, you are probably going to

<<

. 3
( 132 .)



>>