file, httpd.conf, which you will find in a directory called conf in the Apache install
directory. Add the following lines to this file:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
This ensures that the PHP interpreter will parse files that end with the .php
extension. Any files with the .phps extension will be output as PHP source. That is,
the source code will be converted to HTML and color-coded. This can be useful for
debugging your scripts.
If you want to offer to your users PHP pages with extensions more familiar to them,
you can choose any extension you want. You can even ensure that files with
the .html extension are treated as PHP files with the following:
AddType application/x-httpd-php .html
Note that treating files with the .html extension as PHP scripts could slow down your
site, because every page with this extension will be parsed by the PHP interpreter
before it is served to the user.
If PHP has been preinstalled and you have no access to the Apache configuration
files, you may be able to change the extensions that will determine which files will
be treated as PHP executables by including an AddType directive in a file
called .htaccess. After you have created this file, the directive will affect the
enclosing directory, as well as any subdirectories. This technique will only work if
the AllowOverride directive for the enclosing directory is set to either FileInfo or All.
Although the filename .htaccess is the default for an access control file, it may have
been changed. Check the AccessFileName directive in httpd.conf to find out. Even if
you don't have root access, you should be able to read the Apache configuration
files.
An .htaccess file can be an excellent way of customizing your server space if you do
not have access to the root account. An additional way of controlling the behavior of
PHP, even as a non-root user, is the php.ini file.
php.ini
After you have compiled or installed PHP, you can still change its behavior with a file
called php.ini. On UNIX systems, the default location for this file is /usr/local/lib;
on a Windows system, the default location is the Windows directory. A php.ini file
in the current working directory will override one in the default location, so you can
change the behavior of PHP on a per-directory basis.
You should find a sample php.ini file in your distribution directory, which contains
factory settings. Factory settings will be used if no php.ini file is used.
The default settings should be adequate for most of the examples in this book,
although you can read about some amendments you might like to make in Hour 22,
"Debugging."
Directives in the php.ini file take the form of a directive and a value separated by an
equals sign. Whitespace is ignored.
If PHP has been preinstalled on your system, you might want to check some of the
settings in php.ini. Remember, if you are not allowed to alter this document, you
can create one in your script's directory that can override the default. You can also
set an environmental variable PHPRC that designates a php.ini file.
You can change your php.ini settings at any time, though if you are running PHP as
an Apache module, you should restart the server for the changes to take effect.
short_open_tag
The short_open_tag directive determines whether you can begin a block of PHP
code with the symbols <? and close it with ?>. If this has been disabled, you will see
one of the following:
short_open_tag = Off
short_open_tag = False
short_open_tag = No
To enable the directive you can use one of the following:
short_open_tag = On
short_open_tag = True
short_open_tag = Yes
You can read more about PHP open and close tags in Hour 3, "A First Script."
Error Reporting Directives
To diagnose bugs in your code, you should enable the directive that allows error
messages to be written to the browser. This is on by default:
display_errors = On
You can also set the level of error reporting. We will cover the options available for
the error_reporting directive in more depth in Hour 22. For now, however, you
should set this to the following:
error_reporting = E_ALL & ~ E_NOTICE
This will report all errors but not notices that warn about potential problems with
your code. Notices can interfere with some PHP techniques. This setting is the
default.
Variable Directives
PHP makes certain variables available to you as a result of a GET request, a POST
request, or a cookie. You can influence this in the php.ini file.
The track_vars directive creates associative arrays containing elements generated
as a result of an HTTP request. This is allowed by default:
track_vars = On
The register_globals directive determines whether values resulting from an HTTP
request should be made available as global variables. Many scripts in this book will
require the following setting to be enabled:
register_globals = On
Help!
Help is always at hand on the Internet, particularly for problems concerning open
source software. Wait a moment before you hit the send button, however. No
matter how intractable your installation, configuration, or programming problem
might seem, chances are you are not alone. Someone will have already answered
your question.
When you hit a brick wall, your first recourse should be to the official PHP site at
<http://www.php.net/>, particularly the annotated manual at
<http://www.php.net/manual>.
If you still can't find your answer, don't forget that the PHP site is searchable. The
advice you are seeking may be lurking in a press release or a Frequently Asked
Questions file. Another excellent and searchable resource is the PHP Knowledge
Base at <http://www.faqts.com/knowledge-base/index.phtml>.
Still no luck? You can find links to searchable mailing list archives at
<http://www.php.net/mailsearch.php3>. These archives represent a huge
information resource with contributions from many of the great and the good in the
PHP community. Spend some time trying out a few keyword combinations.
If you are still convinced that your problem has not been addressed, you may well
be doing the PHP community a service by exposing it.
You can join the PHP mailing lists at <http://www.php.net/support.php3>.
Although these lists are often high volume, you can learn a lot from them. If you are
serious about PHP scripting, you should certainly subscribe at least to a digest list.
Once subscribed to the list that matches your concerns, you might consider posting
your problem.
When you post a question it is often a good idea to include as much information as
possible (without writing a novel). The following items often are pertinent:
Your operating system
The version of PHP you are running or installing
The configure options you chose
Any output from the configure or make commands that preceded an installation
failure
A reasonably complete example of the code that is causing you problems
Why all these cautions about posting a question to a mailing list? First, developing
research skills will stand you in good stead. A good researcher can generally solve a
problem quickly and efficiently. Asking a naive question of a technical list often
involves a wait rewarded only by a message or two referring you to the archives
where you should have begun your search for answers.
Second, remember that a mailing list is not analogous to a technical support call
center. No one is paid to answer your questions. Despite this, you have access to an
impressive resource of talent and knowledge, including that of some of the creators
of PHP itself. A good question and its answer will be archived to help other coders.
Asking a question that has been answered several times just adds more noise.
Having said this, don't be afraid to post a problem to the list. PHP developers are a
civilized and helpful breed, and by bringing a problem to the attention of the
community, you might be helping others to solve the same problem.
Summary
PHP4 is open source software. It is also open in the sense that it does not demandthat you use a particular server, operating system, or database.
In this hour, you learned where to locate PHP and other open source software that
can help you host and serve Web sites. You learned how to compile PHP as an
Apache module on Linux. If you download a PHP binary for another platform, your
distribution will contain step-by-step instructions. You learned some of the
configure options that can change the features that your binary will support. You
learned about php.ini and some of the directive it contains. Finally, you learned
about sources of support. You should now be ready to come to grips with the
language itself.
No comments:
Post a Comment