You should begin by reviewing your HTML skills. Soon enough, you’re going to be
writing programs that write Web pages, so you need to be very secure with your
HTML coding. If you usually write all your Web pages with a plain text editor, you
should be fine. If you tend to rely on higher-end tools like Microsoft FrontPage or
Macromedia Dreamweaver, you should put those tools aside for a while and
make sure you can write solid HTML by hand. You should know how to build stan-
dard Web pages using modern standards (HTML 4.0 or XHTML), including form
elements and cascading style sheets (CSS). If you need a refresher, please see
Appendix A, which is stored on the CD that accompanies this book.
Programming on the Web Server
The Internet is all about various computers communicating with each other. The
prevailing model of the Internet is the notion of clients and servers. You can
understand this better by imagining a drive-through restaurant. As you drive to
the little speaker, a barely intelligible voice asks for your order. You ask for your
cholestoburger supreme and the teenager packages your food. You drive up,
exchange money for the meal, and drive away. Meanwhile, the teenager waits for
another customer to appear. The Internet works much like this model. Large per-
manent computers called
Web servers
host Web pages and other information.
They are much like the drive-through restaurant. Users drive up to the Web
server using a Web browser. The data is exchanged and the user can read the
information on the Web browser.
What’s interesting about this model is the interaction doesn’t have to stop there.
Since the client (user’s) machine is a computer, it can be given instructions. Com-
monly, the JavaScript language stores special instructions in a Web page. These
instructions (like the HTML code itself) don’t mean anything on the server. Once
the page gets to the client machine, the browser interprets the HTML code and
any other JavaScript instructions.
While much of the work is passed to the client, there are some disadvantages to
this client-side approach. Programs designed to work inside a Web browser are
usually greatly restricted in the kinds of things they can do. A client-side Web
program usually cannot access the user’s printer or disk drives. This limitation
alone prevents such programs from doing much of the most useful work of the
Internet, such as database connectivity and user tracking.
3
C
h
a
p
t
e
r
1
E
x
p
l
o
r
i
n
g
t
h
e
P
H
P
E
n
v
i
r
o
n
m
e
n
t
The server is also a computer; it’s possible to write programs designed to operate on
the server rather than the client. This arrangement has a number of advantages:
• Server-side programs run on powerful Web server computers.
• The server can freely work with files and databases.
• The code returned to the user is plain HTML, which can be displayed on
any Web browser.
Installing PHP and Apache
PHP is only interesting when it runs on a computer configured as a Web server.
One way or another, you need access to a computer with at least three compo-
nents on it: PHP, a Web server (such as Apache or Microsoft IIS), and some sort of
database management system (usually MySQL ).
Using an Existing Server
If you’re lucky, you already may have access to a Web server that has PHP and
some other useful programs installed. Once you start building professional Web
sites, investigate using a commercial Web server so you don’t have to deal with
the headaches of server administration and security yourself (unless, of course,
you really like that kind of thing). The advantage of using a prebuilt server is pri-
marily avoiding the entire messy process of setting up your own server. The dis-
advantage is you’re stuck with the configuration that your server administrator
decides upon.
Ultimately, you need to have access to some sort of Web server so people can see
your programs. It might make sense to do all your programming directly on the
server you’ll use to disseminate your work. If you already have a Web site stored
on a server, check to see if your server offers PHP support. A surprising number
of services offer PHP/MySQL support for little or no extra money.
There are some free PHP servers around, but they don’t tend to stay up for long
and they usually have some sort of advertisements or other strings attached. Still,
it’s possible to find a free host that will support PHP. To find a suitable hosting
service, do a Google search on
free PHP hosting
. You have many choices if you’re
willing to pay a monthly fee for service. With a little digging, you can easily find
full-featured services for less than $10 a month. If you want to be able to do all
the projects in this book, look for a server that supports PHP 5.0 and MySQL. It is
also useful if the service supports phpMyAdmin, a database management system
described in chapter 9, “Using MySQL to Create Databases.”
4
P
H
P
5
/M
y
S
Q
L
P
r
o
g
r
a
m
m
i
n
g
f
o
r
t
h
e
A
b
s
o
l
u
t
e
B
e
g
i
n
n
e
r
Installing Your Own Development Environment
Even if you have access to an online Web server, you may want to build a practice
server for development. This approach has many advantages:
• You can control exactly how the server you install is configured. You can
tune it so all the options you want are turned on, and things you don’t
need are disabled. (I describe how to do this later in this chapter in the
section called “Telling Apache about PHP.”)
• You can test your programs without exposing them to the entire world.
When you install a local server, you usually do not expose it to anyone but
yourself. That way people won’t snoop around your work until you’re
ready to expose it.
• It’s easier to configure development environments to work with local
servers than to work with remote ones.
• You don’t have to be connected to the Web while you work. This is especially
important if you don’t have a high-speed connection.
However, installing a Web server (and its related programs) is not as simple as
installing commercial applications. There are a
lot
of variables and many things
that can go wrong. However, with patience, you should be able to do it.
You need several components to build your own PHP development system. PHP
development is often done with either a system called
LAMP
(Linux, Apache,
MySQL, and PHP) or
WAMP
(Windows, Apache, MySQL, and PHP).
If you’re running Linux, there’s a good chance everything is already installed
on your system and you need only configure and turn things on. For that reason, I’m
presuming for this discussion that you’re working on a Windows XP system.
Please look at the various Help documents that come with the software compo-
nents for assistance installing with other operating systems.
To get your system up and running, you need the following components.
A Web Server
The Web server is software that allows a computer to host Web pages. The most
popular Web server as of this writing is
Apache
, an open-source offering which
runs on Windows, Linux, and just about every other operating system. The Web
server lets you write and test programs running from your local computer exactly
the same way they will be seen on the Internet.
TRICK
5
C
h
a
p
t
e
r
1
E
x
p
l
o
r
i
n
g
t
h
e
P
H
P
E
n
v
i
r
o
n
m
e
n
t
The PHP Environment
The PHP environment is a series of programs and library files. These programs
are unusual because the user never runs them directly. Instead, a user requests a
PHP program from a Web server and the server calls upon PHP to process the
instructions in the file. PHP then returns HTML code, which the user sees in the
browser. This book was written using PHP 5.0, although most of the code works
well on earlier versions of PHP.
A Database Environment
Interacting with databases is one of PHP’s most powerful uses. For that reason,
you need at least one database engine installed with your system. For this book,
you use MySQL and SQLite. I cover the installation and use of these packages
more fully in chapter 9, because you won’t need them until then.
An Editor
Have some sort of editor to manipulate your code. You can use Notepad, but you
probably want something more substantial. A number of freeware and commer-
cial PHP editors are available. For this book, I used emacs (a powerful UNIX-based
text editor that can be somewhat mystifying for beginners) and Maguma Studio.
The latter is a commercial editor with a very impressive free version.
Installing Apache
According to the industry-standard Netcraft (www.netcraft.com) survey, 67 per-
cent of the world’s servers are running Apache as of March 2004. You might
already have Microsoft’s Internet Information Server (IIS) installed on your
machine. If so, you can use it, but you have to read the documentation to see how
to make sure IIS communicates with PHP. (The installation notes that come with
PHP explain how to run PHP on an IIS Web server.)
I know of several people who have had good luck running PHP on the IIS server,
but I’ve had problems. Things got especially messy when I tried to run both IIS and
Apache on the same system. IIS would tend to shut down Apache with little warn-
ing. If there’s not an urgent need to run IIS, I’d stick with Apache on my PHP server.
In my office, we actually run PHP on a Linux server and run IIS with .NET on its
own Windows-based server.
The Apache Web server is extremely stable and relatively easy to use once it’s
installed. The code for this book was tested on Apache 1.3.23, 1.3.29, and 2.0.
TRAP
6
P
H
P
5
/M
y
S
Q
L
P
r
o
g
r
a
m
m
i
n
g
f
o
r
t
h
e
A
b
s
o
l
u
t
e
B
e
g
i
n
n
e
r
Installing Apache Files
Apache is available on the CD that accompanies this book. You can also go directly
to the Apache Web site to load a more recent version or get installation help.
Install Apache to your system as the first step of building your WAMP develop-
ment environment. If you have any trouble, read the excellent documentation at
/>When prompted for a domain name, use localhost for a local installation. This
allows access to this practice server from your local machine only. Once you
know things are working well, you can enter a different domain name.
Testing Your Server
Now see if Apache is installed correctly.
1. Use a file manager to look for the
Apache directory.
2. Find a program called
apache.exe.
If you don’t find it there, look in the
bin directory.
3. Run
apache.exe.
A DOS window starts.
While configuring your system,
do not close this DOS window!
If you do, Apache
will close down and work incorrectly. After testing this console version of
Apache, you run it as a service, which runs in the background. (I’ll explain that
shortly.) If you’ve installed Apache 2.0, it automatically installs as a service.
4. Fire up your Web browser and type in one of the following URLs:
http://localhost/ or http://127.0.0.1/.
Apache doesn’t show you much when it’s running, but it adds a powerful
new capacity to your computer. The 127.0.0.1 address actually works better
for me, because IE sometimes “helpfully” takes me to a search engine
when I type in
localhost. Either address indicates the main page of the
machine you’re currently on. If you’ve turned Apache on correctly, you
should see a page that looks something like Figure 1.2.
When Apache is up and running, you can reach it through the
localhost address.
Of course, if your computer has a domain name registered and if you’ve turned
off any firewalls, others can reach it as well.
TRAP
TRICK
7
C
h
a
p
t
e
r
1
E
x
p
l
o
r
i
n
g
t
h
e
P
H
P
E
n
v
i
r
o
n
m
e
n
t