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

Wrox press professional PHP programming

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 (17.86 MB, 858 trang )

1



Professional
PHP
Programming








Jesus Castagnetto
Harish Rawat
Sascha Schumann
Chris Scollo
Deepak Veliath



















Wrox Press Ltd. 
























































TEAM FLY PRESENTS
2

Professional PHP Programming


© 1999 Wrox Press

All rights reserved. No part of this book may be reproduced, stored in a retrieval system
or transmitted in any form or by any means, without the prior written permission of the
publisher, except in the case of brief quotations embodied in critical articles or reviews.

The authors and publisher have made every effort in the preparation of this book to ensure
the accuracy of the information. However, the information contained in this book is sold
without warranty, either express or implied. Neither the authors, Wrox Press nor its
dealers or distributors will be held liable for any damages caused or alleged to be caused
either directly or indirectly by this book.
























Published by Wrox Press Ltd
Arden House, 1102 Warwick Road, Acock's Green, Birmingham B27 6BH, UK
Printed in USA
ISBN 1-861002-96-3

























































TEAM FLY PRESENTS
Trademark Acknowledgements

Wrox has endeavored to provide trademark information about all the companies and
products mentioned in this book by the appropriate use of capitals. However, Wrox cannot
guarantee the accuracy of this information.



Credits



Authors Technical Reviewers
Jesus Castagnetto Matt Allen
Harish Rawat Vivek Awasthi
Sascha Schumann Gianluca Baldo
Chris Scollo Robert Baskerville

Deepak Veliath Mathijs Brands
Andy Jeffries
Additional Material Kristian Kohntopp
Mathijs Brands Manuel Lemos
Rod Kreisler Samuel Liddcott
Brian Moon Brad Marsh
Mark Musone Neil Matthews
Julian Skinner Sebastian Moerike-Krauz
Mark Musone
Editors Paul Schreiber
Robert FE Shaw Rick Stones
Soheb Siddiqi Adrian Sill
Julian Skinner Andrew Stopford
Adrian Teasdale
Development Editor
Mark Wilcox
Richard Collins
Design / Layout
Managing Editor Tom Bartlett
Paul Cooper Mark Burdett
Will Fallon
Project Manager Jon Jones
Tony Berry John McNulty

Index Cover Design
Martin Brooks Chris Morris

























































TEAM FLY PRESENTS
4
About the Authors
Jesus M. Castagnetto
Jesus M. Castagnetto is a Ph.D. Chemist currently working at The Scripps Research
Institute as a Postdoctoral Research Associate in the Metalloprotein Structure and Design
Group ( where he is developing the
Metalloprotein Database and Browser ( /> using a
combination of in-house programs, Java applets/servlets, SQL databases, and PHP3

server-side scripting. Got his PhD from New York University for research in
computational, synthetic, and physico-chemical studies of small molecule ligand-metal
complexes. Nowadays, he tackles systems that are a "little" bigger: Metalloproteins.
Bioinformatics and computational modeling of chemical systems are two of his main
areas of interest. In his spare time, (yeah, right!) he hacks awk, PHP, plays way too much
with his PalmPilot, and tries not to hurt anybody while doing Shorinji Kempo, or
swinging his sword wildly in Iaijutsu class.
Harish Rawat
Harish Rawat is a Software Developer at the Oracle Corporation. He has seven years of
experience in systems programming. His technical areas of interest include XML, Java,
and Network protocols.

I would like to thank Arnab and Pankaj for their invaluable suggestions in designing the
Shopping Cart Application. I would also like to thank people at Oracle India Development
Center for their encouragement and support throughout the writing process.
Sascha Schumann
Sascha Schumann is a member of the PHP Group and the Apache
Software Foundation. He currently studies at Gymnasium Letmathe. Sascha is the
architect of many PHP modules (including but not limited to: DBA, Session management,
mcrypt, mhash), has connected PHP with AOLserver, and provides general PHP support
and custom PHP extensions to organizations employing PHP. Sascha also maintains
mhash, which is an open source software for creating cryptological digests (so called
hashes).
Christopher Scollo
Christopher Scollo is the co-founder and president of Taurix, a software development
company based in New Brunswick, New Jersey. When not developing web applications,
teaching web courses, or drowning in web periodicals, he hikes and eats. (Potatoes are a
passion.) In addition to programming languages, he also enjoys human languages and
speaks English, German, French, and Italian to varying degrees. He lives in Princeton,
New Jersey with his wife, Nicole Bator.


Deepak Veliath
Deepak Veliath is currently working for the Oracle Corporation in Bangalore, India. He
has been following PHP for a year now. Favorite OS - Linux. When not writing books,
busy making plots to kill editors in their sleep :)


























































TEAM FLY PRESENTS

i
Table of Contents
I. Introduction
1. About this book
2. Downloading, installing, configuring PHP
3. Programming in a Web environment
II. The Language
4. Variables and data types
5. Operators
6. Expressions and Statements
if else elseif
switch
while and do while
for Error! Bookmark not defined.
require and include
exit Error! Bookmark not defined.
7. Functions
How They Work
Passing Arguments
Variable Scope and Lifetime
Nested Functions and Recursion
Assigning Functions to Variables
8. Arrays
Starting Simply
Looping Through an Array
A Sequentially Indexed Array
Non-sequentially Indexed Arrays

A Few Words About
next() and prev()
array_walk()
Associative Arrays
Multi-Dimensional Arrays
Sorting Functions
Using Arrays with Form Elements
9. Objects and OOP
Object-Oriented Programming
Defining a Class
Instantiating the Class
Constructors
Inheritance
10. String manipulation and regular expressions
III. Applying PHP
11. Non-relational databases
What is a database?
Databases and CSV
dbm - a fast, record-based, flat-file database
























































TEAM FLY PRESENTS

ii
Another interface to dbm - DBA
Our telephone book interface
Error! Bookmark not defined.
12. SQL DBMS engines
13. Using LDAP
14. Serving XML documents
15. Graphic content creation
16. PHP connectivity
Utilizing TCP
The whois function
The Frontend
Extending the Frontend
Is my domain still available?
A web client

17. Sending E-mail
18. Cookies
What are cookies?
Cookies in PHP
Getting started
Deleting a cookie
Common pitfalls
What is your name?
19. Error handling and debugging
20. Security issues
21. Magic with quotes
What are magic quotes?
Defining the scope
Magic quotes explained
Sybase extension
Applying magic_quotes
Two helper functions
An alternative approach
Summary
22. Templates
IV. Sample Applications
23. A shopping cart application
24. An OO site
V. Advanced Topics
25. Version migration
26. When should I avoid regular expression functions
VI. Appendix
A. Reference
B. Open Source Software
C. PHP in non-UNIX environments

D. Notes on PHP4/Zend
Basic concepts























































TEAM FLY PRESENTS

iii
The binary compiler

The optimizer
New features in PHP4
PHP4 and overall portability
E. Other Resources























































TEAM FLY PRESENTS


iv
List of Examples
11-1. edit_form() function
16-1. Initiating a TCP connection
16-2. Exchanging messages with a SMTP server
16-3. whois_request() function
16-4. Simple whois frontend (HTML code)
16-5. Simple whois frontend (PHP code)
16-6. Source code of the simple whois front-end
16-7. Allowing server choice
16-8. Changed PHP code
16-9. a function to tell the TLD
16-10. using an array to store information about whois server
16-11. is_domain_available
16-12. http class framework
16-13. Full http class
16-14. Using the http class
18-1. counting the hits per visitor
18-2. setcookie fails due to sent data before it
18-3. specifying the expire time
18-4. Deleting the cookie
username
18-5. deleting and setting a new cookie
18-6. Identifying a visitor
21-1.
addslashes()
21-2. output
21-3. output with enabled
magic_quotes_sybase
21-4. The ugly way

21-5. automating the conversion process
21-6. using
get_magic_quotes_gpc
21-7. using
magic_quotes_runtime effectively
21-8. applying
stripslashes()
21-9. two helper function
21-10. using
sprintf()

























































TEAM FLY PRESENTS
Introduction
Over the past years, the Internet has gone from the preserve of academics to the cutting edge of business.
A large part of this has been driven by the growth of the Web, with its graphical browsers and high
media profile. The change from static HTML pages to dynamic, user interactive presentations has been
achieved largely by the introduction of scripting technologies.

Working with the usual markup language of a web page, scripting languages enable clients to demand
specific information from their servers, and their servers, in turn, to receive important user input in order
to process and display data on demand.

In the forefront of this advancement has been the Open Source community, dedicated in providing web-
based solutions purely for the love of the technology itself. Open source is not only about free software
(though not everything is free anyway), but it is about, as the name suggests, being open about the source
of the code.

Having free access to the source means that authors are forced to keep to standards. If these are not
maintained, the deviation is labeled a bug, and if the author doesn't fix it, someone else will. Large
numbers of independent programmers being able to understand what a program does, and ensuring that
standards are maintained, prevents the author from being able to exploit the user, as happens in the
commercial software world.
What Does This Book Cover?
PHP, the subject of this book, is an open source server-side scripting language that has taken web-based
development to a new level of sophistication.


In this book we will show you what PHP is, how it simplifies server-side scripting and adds to the
functionality of your web pages.

In particular, we'll be looking at the following areas:

❑ The PHP language structure and syntax, including its data types, operators, statements and
functions;
❑ The process of installing the scripting engine onyour server;
❑ The myriad of PHP modules that greatly enhance its capabilities;
❑ Server Side Programming
❑ Practical case study examples of PHP in action.
























































TEAM FLY PRESENTS
Who Should Read This Book?
This book is for anyone who has to implement web-based applications that go beyond simple static
pages. If you are attempting to build a database-driven site, or you are new to the whole "dynamic web
content generation paradigm", or just want to make some nifty server-side scripts, read this book. If you
are an experienced developer that has used until now only product ‘ABC’ that works for the ‘Kewl-OS’
platform, but feel the need to build solutions that are more portable and that can be developed in a short
period of time, read this book.

We assume that you have some basic knowledge of web technologies. You should be comfortable with
writing HTML, have experience with writing programs in any of the many scripting or programming
languages, and have some understanding on how a web-based distributed information system works. We
will assume also that you have at least conceptual familiarity with databases, although a good knowledge
of these systems will surely make it easier to comprehend some of the material in the book (in particular
the examples in the databases and the shopping cart application chapters).
Technology Requirements
For server-side programming you will need a web server on your machine. This can be IIS on a Windows
platform, or something like Apache, or Xitami, for other operating systems. Apache dominates the web
server scene, currently powering 60% of the Web.

In order to use the server-side scripting tool you will need to install and configure the PHP language
interpreter on your server. We will be stepping through this process early in the book.

For the client, you have a free choice. PHP is effective on both Internet Explorer and Netscape
Communicator, as well as any of the other web browsers that are available.

If you want to be able to run all the code, you should also have access to a relational database. Your
choice for this is quite wide and explained later in the book. We have chosen to use MySQL throughout
the book.

For programming itself, all you need is a good text editor, like NotePad, gnotepad or eMacs. Scripting
pages are simply text files, often written embedded within the markup language that generates the web
page.
Conventions Used in This Book
We have used a number of different styles of text and layout in the book to help differentiate between the
different kinds of information. Here are examples of the styles we use and an explanation of what they
mean:

Advice, hints, and background information comes indented and italicized, like this.
Important information comes in boxes like this.
Bullets are also indented, and appear with a little box marking each new bullet point, like this:

❑ Important Words are in a bold type font
























































TEAM FLY PRESENTS
❑ Words that appear on the screen in menus like the File or Window are in a similar font to the
one that you see on screen
❑ Keys that you press on the keyboard, like Ctrl and Enter, are in italics
❑ Code has several fonts. If it's a word that we're talking about in the text, for example when
discussing the For Next loop, it's in a bold font. If it's a block of code that you can type in
as a program and run, then it's also in a gray box:

<?php
{phpinfo();}
?>

❑ Sometimes you'll see code in a mixture of styles, like this:

<?php
require 'functions.php';

// Check whether the user is already authenticated or not
if (!authenticateUser( $cookie_user, $cookie_passwd)){

header("Location:http://$HTTP_HOST/$DOCROOT/default.htm");
exit();
}

?>

The code with a white background is code we've already looked at and that we don't wish to examine
further.

These formats are designed to make sure that you know what it is you're looking at. We hope they make
life easier.
Tell Us What You Think
We've worked hard on this book to make it useful. We've tried to understand what you're willing to
exchange your hard-earned money for, and we've tried to make the book live up to your expectations.

Please let us know what you think about this book. Tell us what we did wrong, and what we did right.
This isn't just marketing flannel: we really do huddle around the email to find out what you think. If you
don't believe it, then send us a note. We'll answer, and we'll take whatever you say on board for future
editions. The easiest way is to use email:



You can also find more details about Wrox Press on our web site. There, you'll find the code from our
latest books, sneak previews of forthcoming titles, and information about the authors and editors. You
can order Wrox titles directly from the site, or find out where your nearest local bookstore with Wrox
titles is located.
























































TEAM FLY PRESENTS
Customer Support
If you find a mistake, please have a look at the errata page for this book on our web site first. If you can't
find an answer there, tell us about the problem and we'll do everything we can to answer promptly!
Appendix H outlines how you can submit an errata in much greater detail. Just send us an email:



or fill in the form on our web site:


























































TEAM FLY PRESENTS
1
What Is PHP?
The World Wide Web has changed very fast in so many ways. Sometimes it seems like yesterday that a
little known markup language with a strange name HTML (Hyper Text Markup Language) was used by

some physicists to link scientific documents at a group of CERN servers. It was wondrous to read some
text somewhere in the world with just a simple program, and what is more information in the document
could magically transport you to another one with related information.

And this spread relatively quickly to other sciences. Text-only interfaces were the norm, and simplicity
of accessing information content the most important part of the equation. Text documents with a small
set of tags and a simple server setup was all you needed to inform your colleagues and share the
knowledge, independently of whether the organic chemist at the other end was using his trusty Mac, or
the theoretician was using her Unix box, or the impoverished graduate was using a second or third
hand PC running very flaky TCP/IP software.

Nowadays we expect more, much more than this. We expect a web site with lots of information, and a
good presentation, but we do not want to be distracted by a difficult interface. The information should
be easy to find, and it should be current. A clean and dynamic web site is a great asset for the user and
for the information provider. Long gone (fortunately) are the days of garish-looking web sites with
blinky thingies, lots of animated images that usually were hiding a shallow content depth. We want
information, we want it 5 minutes ago and we want it in the way we like it.

A modern web site is not just a web server; it also includes a way of storing data and querying (a SQL
database perhaps), a way of processing the requests from the user and creating documents with the
appropriate information. Many are the options open to the web developer, but not all of them as open
and general as others. We should not only consider the immediate task at hand of creating a site with
dynamic content, we need to be sure that we can still be providing the said content independently of
the changes in hardware or software technology.

We want to try and insure ourselves against future technology changes, dramatically reduce our license
costs, keep our hardware budget under control, and yet be portable to different web servers and
operating systems. We also want some assurance that we can do something about that killer bug we
just think we found in our web server or scripting environment, be able to understand (if we want to)
























































TEAM FLY PRESENTS
how the scripting works, and be able to modify the behavior of our web server or scripting host to meat
some particularly unusual need. Open source products will be your best assurance that your application
that works now in the "Super-Turbo Hexium IX" machine of today, will work in the "Nanotech Cube
Aleph" of tomorrow (I am exaggerating just a wee bit).
Enter PHP
PHP (acronym for: PHP Hypertext Preprocessor), is a server-side embedded scripting language. This

means that it works within an HTML document to confer to it the capacity of generating content on
demand. You can convert your site into a web application, not just a collection of static pages with
information that may not get updated quite so often, which may be alright for a "personal" web site
(yes, we all have made such a beast), but not for one that is going to be used for business or for
education.

You may be asking "But, why PHP? There are so many other options like ASP, Cold Fusion, Perl,
Java, Python, even good old shell/awk/sed scripts?", and the answer will be: simplicity, an almost
natural way of using databases and platform independence.

And did I mention it was open source?

Of course general scripting or programming languages like Perl, Python, etc. have also platform
independence, and are open source. They are great languages, and sometimes an overkill for what you
need, like using a concrete mixer to make scrambled eggs. PHP was designed to work on the web, and
in this ambit it excels; connecting and querying a database is a simple task that can be handled in 2 or
3 lines of code. The PHP scripting engine is well optimized for the response times needed on web
applications, it can even be part of the web server itself improving the throughput even more.

If it were only a matter of improving the speed of the scripts, then PHP will be one of many solutions.
But there is more to the PHP equation than that. There is the simplicity and robustness of the language
and the scripting engine. There is the connectivity to an ever increasing number of database servers,
the shorter development cycles and the ease (encouraged by the syntaxes and constructs) of creating
modular and reusable components.

You can perform tasks as simple as creating a feedback form that sends an e-mail to the web
maintainer, to a whole database driven document management system (like Midgard,
to helpdesk or bug tracking systems (like Keystone,
to a shopping cart application (like
FishCartSQL, to what would be considered "middle-ware"

packages without the need for extra languages or frameworks, and whole libraries for quick and
flexible development (PHPLIB,

Then there is the support from a widely distributed and cooperative community, with several source
repositories (like PHP Code Exchange, or Berber's WeberDev
many sites with tutorials (PHPBuilder,
PHPWizard, WebMonkey,
etc.) and thriving (high volume) mailing lists.

And did I mention that it is open source?

There’s no more waiting until the next release for a feature to be added or a bug to get fixed. Just take
the source, make your modifications and there you are, instant customization and complete control. No
more guessing at whether a particular function or feature is insecure, the code does not lie. And who
























































TEAM FLY PRESENTS
knows, maybe your modification gets to be so popular that others may want to use it (hey! instant
fame). And you cannot beat the total price for a development environment using the combination of
Linux, Apache, MySQL and PHP, not only cheaper than other more proprietary environments, but also
more stable and robust. As Eric Raymond said "given enough eyes, all bugs are shallow".
It All Began
Some time in 1994 when Rasmus Lerdorf put together a bunch of Perl scripts to track down who was
looking at his resume. Little by little, people started to get interested in the scripts, and they were later
released as a package "Personal Home Page" tools (the first meaning of PHP). In view of the interest,
he wrote a scripting engine and incorporated another tool to parse input from HTML forms: FI, Form
Interpreter, thus creating what was called variously PHP/FI or PHP2. This was done around mid 1995.

Soon, people started to use these tools to do more complicated things, and the development changed
from just one person, to a group of core developers in charge of the project and its organization. This
was the beginning of PHP3. This group of developers (Rasmus Lerdorf, Andi Gutmans, Zeev Suraski,
Stig Bakken, Shane Caraveo, and Jim Winstead), improved and extended the scripting engine and
added a simple API that will allow other programmers the liberty to add more functionality to the
language by writing modules for it. The language's syntax was also refined, with constructs that will be
familiar for people coming from object oriented or procedural languages. If you know C, C++ or Java,
or have done even some shell/awk scripting, or written a Pascal or VBasic program, learning the basic
PHP constructs will be a breeze.


The PHP language features the usual complement of control structures, operators, variable types,
function declarations and class/object declarations that we have been accustomed to expect from any
compiled or interpreted language, and yet it also has features of its own. For example, in C you employ
pointers, in other scripting languages this can be cumbersome or even not possible, but in PHP this is
just one use of variable variables (discussed in detail later in the book), as the code below shows:

$peru = array("domain"=>"pe", "capital"=>"Lima");
$japan = array("domain"=>"jp", "capital"=>"Tokyo");

function show ( $country ) {
echo "Internet domain = ".${$country}["domain"]."\n";
echo "Capital city = ".${$country}["capital"]."\n";
}

show ("peru");

// Prints:
// Internet domain = pe
// Capital city = Lima

show ("japan");

// Prints:
// Internet domain = jp
// Capital city = Tokyo

The trick is in the
${$country}[] call, which in turn is equivalent to using $peru[] or
$japan[]. But that is not all, what if you want to make a function that uses another one for
comparing a couple of items. An idea similar to the one shown above allows us to pass different

comparison functions in the main function parameters:























































TEAM FLY PRESENTS

function bigger($x, $y, $comp_func) {
if ( $comp_func($x, $y) ) {
$out = "Item ".$x." is bigger than ".$y;

} else {
$out = "Item ".$x." is not bigger than ".$y;
}
return $out;
}

bigger(2, 1, "num_comp");
// will use the function num_comp() to compare the numbers

bigger("epsilon", "gamma", "greek_comp");
// will compare the strings as names of greek alphabet characters

In this way making general handling routines is simpler, and no, there is no typo above, it is
$comp_func and not $$comp_func, think a little about it and you will see why.

Don’t worry if much of the above doesn’t make sense at this moment. These examples are just a taste
of what is to come in the rest of the book and will be fully explained.
And the Current Situation is
PHP version 4 (PHP4), based on the Zend engine (details at This
scripting engine has been designed from the ground up to be easily embeddable in different
applications. PHP4 is the first application using the Zend engine, but it could also be included in other
packages, for example in MySQL (which could be a good way to enable stored procedures in that
database).

There is already a beta version of PHP4 (beta 3 when this book was written). I will recommend a visit
to the Zend web site for more information; particularly tantalizing is the possibility of using COM and
perhaps CORBA with this engine.

It is also easy to note a trend towards more and more sites using PHP for their scripting needs. The
statistics (available at the main PHP site's usage page,

courtesy of Netcraft, show a continuous increase in the total
number of domains and IPs using PHP as an internal Apache module (about 1,000,000 virtual servers).
There is also a good number of sites using PHP as a stand-alone module and these will not show in the
surveys that Netcraft conducts.

Notable is also that mod_PHP (as the corresponding Apache module is called) is the most popular
module for the most popular web server in the Internet (E-Soft Inc.'s web survey, http://www.e-
softinc.com/survey/). Even a cautious forecast will predict a steady increase of the usage of
PHP, even more when the final PHP4 version appears with all the promise that the Zend engine holds.
Book Style and Organization
The book will emphasize clarity over conceptual profundity and practical real-world examples over
abstract examples (such as "Hello World" and "$foobar = 1"). Our aim is to present code that is useful
(with little modification) to the reader, not intricate technical discussions. These examples will try to
be as web browser-neutral as possible, which is a good strategy for any robust web application. Use of
























































TEAM FLY PRESENTS
platform specific client browser features will be avoided, e.g. if JavaScript is generated, this will be
constructed so it will work well independently of whether the user runs Internet Explorer under
Windows or Netscape Navigator on a Solaris box.

Although PHP development requires the use of other technologies such as HTML, SQL, and HTTP
servers, this book will not attempt to be a full resource on those subject areas. Technologies other than
PHP will be addressed only in the context of their use and interaction with PHP. For example, we will
not discuss at length the basis of XML and its specification. We will, however, demonstrate how to use
MySQL and PHP together to build a web database application.

We divide this book into a number of sections. The first section introduces the basics of the language
with clear and "real life" examples, that is we tried to avoid making "Hello World"-like scripts, after
all you want to develop solutions for a web application. The second section will contain chapters
showing more complex use of PHP functions (databases, image creation, etc.). The third section will
deal with fully discussed applications (such as a shopping cart application). And finally we are
including a section with appendices containing a general reference on the language, as well as
discussions on the open source concept and similar topics.
In Closing
This book will cover the core language of PHP, including issues such as installation and configuration,
and demonstrations of the practical use of the language. The focus is on common business needs, such
as database application development and e-commerce. Explicit mention will be made of the pros and

cons of certain approaches, and the trade-offs involved. In the end, our aim is to provide you with a
new and powerful tool, so that you, the web application developer, will be able to create a better
project in a shorter period of time.

If we at least convince you to consider PHP as a viable alternative for your projects, we will be happy.
And even more, if you decide not only to think about using it, but actually go ahead and use it for real
and then participate in the community of all the other PHP developers, that my friend will make us feel
warm all over (as the saying goes). So, sit back, grab some coffee and enjoy.
Useful Websites
Here is a preliminary list of websites you may like to visit to keep up-to-date with developments in
PHP, Apache and MySQL. A fuller reference list is included in the appendices at the end of this book.

❑ PHP site:
❑ PHPBuilder site:
❑ Apache Project site:
❑ MySQL site:
























































TEAM FLY PRESENTS
2
Installing and Configuring PHP
In this chapter we shall explore the ways and means to get us equipped with a working version of
PHP. We will configure PHP so that it is able to utilize MySQL, which we will be using as our
preferred choice of database product. We will also add two of the many extensions that PHP supports:
Boutell's GD library (for image creation and manipulation) and Expat (for XML parser support).

PHP comes with a myriad of options, both to build the distribution and also to configure an
installation. PHP supports several APIs and interfaces to other programming tools. The sheer number
of these tools is daunting, not to speak of the configuration possibilities for each of these. Before we
get involved in the detail of installing PHP, it would be worthwhile to consider what we would expect
to do with PHP in the near future. Depending on this we need to include only those particular modules
that are germane to the kind of stuff we plan to do with the installation.

The advantage with choosing only the modules that we need is obviously a smaller binary and the
performance benefits associated with it. We could always load the less frequently used modules
explicitly, but if we use this strategy with frequently used ones the installation mostly takes a
performance hit.


In the first section we shall take a look at the installation part with small digressions on common
pitfalls while installing and how to circumvent them. In the latter section we shall look at configuring
PHP to suit our expected usage pattern.

During the course of this chapter we shall be looking at:

❑ PHP’s scope and dependency on other software. Why is this dependency there?
❑ Getting us to quickly installing a working version of PHP. We could then go on and change
the configuration to suit our needs.
❑ Various options in terms of platforms and configurations for installing PHP.
❑ Exploring the various mechanisms of configuring PHP – build-time settings, configuration
files, etc.
























































TEAM FLY PRESENTS
❑ Configuration of PHP with respect to database options, mail options, security and a few
general options.
Installation
PHP is supported on quite a few platforms, many of them UNIX-like and of course on Microsoft’s
operating systems supporting the Win32 environment. Since PHP cannot do much without a web
server, when we talk of installing PHP on a platform, we also need to take into consideration the web
server that we plan to use.
Platforms and Web Servers
On the UNIX front, Apache is the web server of choice for most installations. PHP is rated as the
second most popular Apache module, where it is found to perform very well. PHP can be compiled as
a standalone interpreter, like any other CGI scripting language or it can be compiled as an Apache
module. Apache is also available on the Windows platform and PHP is supported for this combination.
FHTTPd is an open source, UNIX-only web server for which PHP can be configured as a module.

PHP distributions for Microsoft’s web server solutions IIS 3 up to IIS 5, PWS on Windows95/98 and
NT are available. Omni HTTPd for Windows is another supported web server among others for which
results may vary.

There are a number of other web servers that can be used with PHP, like Xitami for instance, but it
must also be remembered that Apache is the only web server for which PHP can be compiled as a
module – the rest are CGI interpreter installations.
PHP Modules
PHP has support, by way of APIs and interfaces to a vast chest of tools and platforms. Most of these

tools or add-on modules are available as libraries (DLLs in Windows parlance). We shall see some
common tools supported by PHP:

❑ A large number of databases are supported for connectivity by PHP. As of PHP 3, these
include Adabas, dBase, Empress, FilePro, Informix, InterBase, mSQL, MySQL, Oracle,
PostgreSQLQL, Solid, Sybase, Velocis and several flavors of the Unix dbm,. Any other
database that supports ODBC (Open Database Connectivity) can be accessed using PHP’s
ODBC support, e.g. IBM’s DB2 database.
❑ LDAP (Light-weight Directory Access Protocol) is another supported protocol. PHP provides
APIs for writing LDAP client programs. LDAP is a protocol used to access directory related
information such as address books, white pages etc.
❑ XML (eXtended Markup Language) touted as the future language of the web is also
supported. XML (among others) separates content or the information that a web page holds
from the presentation of the page. WDDX (Web Distributed Data eXchange) a technology
derived from XML is also supported.
❑ Mail protocols such as IMAP (Interactive Mail Access Protocol) and SMTP (Simple Mail
Transport Protocol) are supported. IMAP is a protocol used for mail retrieval and SMTP is
used for routing mail on the Internet.
























































TEAM FLY PRESENTS
❑ Image functions are supported in the sense that, using the PHP API we could generate
images dynamically, responding to user input.
❑ PDF (Portable Document Format) promoted by Adobe for distributing documents on the web
is also supported apart from support for PostScript, another document format that is also a
printer-control language. You could create PDF and PostScript documents using the
appropriate API.
❑ SNMP V3 (Simple Network Management Protocol Version 3) is also supported so that
manageability over the network can be achieved. You would need to install the UCD SNMP
library for this (

Some of these modules are part of the core PHP distribution itself and may need no other software to
get them going. Some of them may not be so, in that they need other libraries to work. The list above
is by no means exhaustive and we shall be looking at a few other modules later in the chapter.
CGI Interpreter vs. Apache Module
As mentioned earlier, PHP can be compiled to be a standalone CGI interpreter or as an Apache
module. With PHP configured to be a CGI interpreter, every time a PHP script is to be interpreted, the
web server spawns an instance of the PHP interpreter, which interprets the script. This obviously

causes some performance degradation.



When PHP is compiled as an Apache module, as the diagram above shows, it runs in the same address
space as that of the web server process itself and hence provides a significant performance
improvement over traditional CGI interpreters that are separate processes. Certain features like
persistent database connections (discussed later in this chapter) are available only in the Apache
module version. Some security concerns arise when we go in for the CGI version. We shall see them
























































TEAM FLY PRESENTS
later when we take a look at configuring for security.

However the CGI interpreter version of PHP allows users to run different PHP scripts under different
user-ids, whereas in the case of the Apache module version, the script is run as the Apache user
(nobody is the default user). Also, the CGI version can be installed on web servers by users who
don’t have access rights to install PHP as a web server module. The other advantage is that it is much
harder for malicious or poorly written scripts to crash a web server while using the interpreter.
Obtaining the Distribution
You could obtain PHP distributions by:
Methods
❑ HTTP
❑ FTP
❑ From an anonymous CVS server
Formats
❑ Binary tar file
❑ RPMs
❑ Source code.

The core PHP distribution is available from the official PHP home at . PHP
is available over FTP or HTTP from a number of mirror sites (for a site closest to you, check the list
at the PHP homepage).

PHP is distributed as source distributions and also as compiled binaries. The compiled binaries are
available as RPM (RedHat Package Manager) distributions for Linux installations that support the
RPM format, such as RedHat and SuSE. Compiled binaries are also available for Windows platforms.
For a list of mirror sites that carry binary distributions for 32-bit Windows, go to the Download

section at the home site.

RPM distributions are not restricted to just binaries. There are source RPMs that package PHP sources
too. The RPM binary distributions, though very simple to install, suffer from the limitation that no
build-time configuration can be done.

We could get the distribution via anonymous CVS (Concurrent Versioning System) from the PHP
site’s CVS servers, . We need not have a CVS client to be installed on our
machine for this, as there are web-interfaces to the CVS server. The sources available from CVS are
the latest and in a state of flux as they are development sources; it may or may not be stable, hence for
a first-timer, this may not be the best option. More information on the CVS is available at
.

The best bet for an installation (with sufficient flexibility in terms of deciding what modules we need)
is to pull the sources either by FTP or HTTP and build the distribution from the source. This is not a
very involved process, considering the autoconf scripts which take care of most of the trouble.
While this is the case for Unix and similar systems, the pre-compiled binaries are the best bet for
























































TEAM FLY PRESENTS
Windows users. However those of us who would like to compile for Windows should have a version
6.0 VC++ compiler at hand.

As mentioned earlier, PHP allows access to several other tools or add-on modules, most of which are
accessed by PHP in the form of libraries. A partial list of these tools/modules and their locations on
the web is given below. Many of the listed tools and the corresponding libraries are available as pre-
compiled binaries for both Windows (as DLLs) and UNIX. Many of them in fact are available as
source distributions allowing us to compile them, if we choose to do so.
Aspell
Aspell is an open source spell checker and the Aspell library is available from

Berkeley DB
The Berkeley DB is a supported Database backend for PHP. It is usually used in environments where
the database requirements are not sophisticated. It offers simplicity and speed for elementary
applications. The distribution for both Windows and UNIX is available from

Dmalloc
The Dmalloc (debug memory allocation) library, which provides debugging facilities, is available

from .
Freetype fonts
If we need to use TrueType fonts in images that we generate using PHP, we can use the solution
available from FreeType. This provides the libttf library that is used by PHP. It must however be
noted that not all components associated with this is open source; some may involve payment. For
details refer to the FreeType homepage at
GD Library
To use most of the graphic functionality available with PHP we need the GD library. Thomas Boutell
wrote the library and currently UNIX and Windows versions for this are available; both of these are
maintained at
Though PHP 3 uses the GD library for most of its image functionality, it has not
been updated to support the PNG (Portable Network Graphics) format. It only
has support for the earlier GIF format. Since version 1.6, GD discontinued
support for GIF due to copyright issues; we must either use the older versions
of library or wait until PHP supports the PNG format. Unfortunately, even up
to the Beta 3 release of PHP 4, PNG support is still missing.
IMAP
The IMAP library is available from The IMAP
protocol is used for retrieving and saving mail on remote servers. We could write server-side
programs using PHP’s IMAP to build applications as complex web-based mail services.
























































TEAM FLY PRESENTS
LDAP
Several LDAP distributions exist, with the University of Michigan LDAP server (for UNIX) among
the earliest. The latest distribution is available at
The OpenLDAP project was started in the
recent past with the aim of taking up from where the UMich left off.

Source distributions of the OpenLDAP server are available from the OpenLDAP home
. These distributions are of the open software genre. The OpenLDAP
project intends to do for the Umich LDAP server what Apache did for the NCSA server. Netscape has
its SDK for LDAP available from

mSQL
The mSQL database distribution is available from for UNIX
platforms and distributions for Windows are available from the MSQL PC Home page,
mSQL is a simple, uncomplicated database for instances where we

don’t need a heavy-weight commercial solution, but good performance.
MySQL
MySQL is available from . It is quite similar to mSQL since it had been
designed to overcome some limitations of mSQL. It offers an open source solution to simple database
needs that does not call for stuff like transaction processing, which is more into the domain of
traditional heavy-iron databases. MySQL does pretty well in terms of searching databases to service
web-based queries. However MySQL is not entirely freeware. The licensing policy for MySQL is
evolving and you should refer to the homepage (mentioned above) for more information on licensing.
mcrypt
The mcrypt library supports numerous commercial and open source block based encryption schemes.
It is available from
mhash
mhash is a thread-safe hash library, implemented in C, and provides a uniform interface to a large
number of hash algorithms. The mhash library is available from
PDF
The library for manipulating PDF documents is available from The
Protable Document Format introduced by Adobe is one of the preferred means of distributing
documents electronically.
SNMP
The library available from provides support for
SNMP, the Simple Network Management Protocol on UNIX. On Windows NT, PHP uses the native
SNMP interface provided by Windows.
Sybase-CT
The Sybase-CT libraries are available from the PHP site itself.
























































TEAM FLY PRESENTS
T1
The T1 library (t1lib) is written in C which implements functions for generating bitmaps from
Adobe Type 1 fonts. It is available from r-uni-
bochum.de/ini/PEOPLE/rmz/t1lib/t1lib.html
XML
The expat XML (eXtended Markup Language) parser by James Clark for both UNIX and the Win32
API is available from
Zlib
The zlib library used for compression is available from



This is by no means a complete list - this is just to give a flavor of the support available. Please look
at the add-on modules table in the latter part of the chapter.
Building and Installing on UNIX
We shall take a look at how to build the PHP distribution on UNIX and install it. The installation on
UNIX-like systems is pretty straightforward. We run the autoconf scripts, which generate the
necessary Make files, and then we carry out a make and install.

Let us see how we do this on a RedHat 6.1 (Linux 2.2.12) box. If we have some header files that are
specific to a particular add-on module and residing at a non-standard location that we wish PHP to
look for in, we can specify the directory containing them by the CPPFLAGS variable before we start
with configuration. Using the Bash-shell, this would look like:

$ CPPFLAGS=-I/home/php/myinclude/; export CPPFLAGS

Similarly to get PHP to look at libraries specific to our setup, we need to set the LDFLAGS variable:

$ LDFLAGS=-I/home/php/mylibs; export LDFLAGS
Building the CGI Interpreter Version of PHP
We shall build the CGI interpreter version of PHP first. This is the easiest version to build since it is
the default. We assume that we have the source distribution of PHP3.

We need to uncompress the distribution and extract the files first.

$ gzip –cd php-3.0.x.tar.gz | tar xvf -

We change directory into wherever the distribution was unpacked and run the configure script which
will generate the necessary Make files. We can pass options to the configure script, to tell the script
the composition of modules that we need for our installation and also certain configuration
parameters. We shall look at each of these options soon.


$ ./configure –-with-ldap –-other-options

Here the –-with-ldap option is an example of how we can specify at build-time what add-on























































TEAM FLY PRESENTS

×