To create the configuration file:
1. Begin a new PHP script in your text editor or IDE (Script 2.1). <?php # Script 2.1 - config.inc.php continues on page 53
Script 2.1 The configuration file is the key back-end script. It defines site-wide constants and dictates how errors are handled.
4 * File name: config.inc.php
5 * Created by: Larry E. Ullman of DMC Insights, Inc.
6 * Contact: LarryUllman@DMCInsights.com, http://www.dmcinsights.com
7 * Last modified: November 8, 2006
9 * Configuration file does the following things:
10 * - Has site settings in one location.
11 * - Stores URLs and URIs as constants.
12 * - Sets how errors will be handled.
18 // Errors are emailed here.
19 $contact_email = 'address@example.com';
21 // Determine whether we're working on a local server
23 if (stristr($_SERVER['HTTP_HOST'], 'local') II (substr($_SERVER['HTTP_HOST'], 0, 7) == '192.168')) {
29 // Determine location of files and the URL of the site:
30 // Allow for development on different servers.
33 // Always debug when running locally:
36 // Define the constants:
38 define ('BASE_URL', 'http://localhost/directory/');
43 define ('BASE_URI', '/path/to/live/html/folder/');
44 define ('BASE_URL', 'http://www.example.com/');
50 * Most important setting...
51 * The $debug variable is used to set error management.
52 * To debug a specific page, add this to the index.php page:
55 require_once('./includes/config.inc.php');
m DO
(script continues on next page)
Script 2.1 continued t/» GO
|
6 00 i Script | ||
|
57 |
* To debug the entire site, do | |
|
58 | ||
|
59 |
$debug = TRUE; | |
|
60 | ||
|
61 |
* before this next conditional. | |
|
62 |
*/ | |
|
63 | ||
|
64 |
// Assume debugging is off. | |
|
65 |
if (!isset($debug)) { | |
|
66 |
$debug = FALSE; | |
|
67 |
} | |
|
68 | ||
|
69 |
# ***** SETTINGS ***** # | |
|
70 |
# ******************** # | |
|
71 | ||
|
72 | ||
|
73 |
# **************************** # | |
|
74 |
# ***** ERROR MANAGEMENT ***** # | |
|
75 | ||
|
76 |
// Create the error handler. | |
|
77 |
function my_error_handler ($e_number, $e_message, $e_file, $e_line, 3 |
e_vars) { |
|
78 | ||
|
79 |
global $debug, $contact_email; | |
|
80 | ||
|
81 |
// Build the error message. | |
|
82 |
$message = "An error occurred in script '$e file' on line $e line |
\n<br />$e_message\n<br |
|
/>"; | ||
|
83 | ||
|
84 |
// Add the date and time. | |
|
85 |
$message .= "Date/Time: " . date('n-j-Y H:i:s') . "\n<br />"; | |
|
86 | ||
|
87 |
// Append $e_vars to the $message. | |
|
88 |
$message .= "<pre>" . print_r ($e_vars, 1) . "</pre>\n<br />"; | |
|
89 | ||
|
90 |
if ($debug) { // Show the error. | |
|
91 | ||
|
92 |
echo '<p class="error">' . $message . '</p>'; | |
|
93 | ||
|
94 |
} else { | |
|
95 | ||
|
96 |
// Log the error: | |
|
97 |
error_log ($message, 1, $contact_email); // Send email. | |
|
98 | ||
|
99 |
// Only print an error message if the error isn't a notice or |
strict. |
|
100 |
if ( ($e_number != E_NOTICE) && ($e_number < 2048)) { | |
|
101 |
echo '<p class="error">A system error occurred. We apologize for the | |
|
inconvenience.</p>'; | ||
|
102 |
} | |
|
103 | ||
|
104 |
} // End of $debug IF. | |
|
105 | ||
|
106 |
} // End of my_error_handler() definition. | |
|
107 | ||
|
108 |
// Use my error handler: | |
|
109 |
set_error_handler ('my_error_handler'); | |
|
110 | ||
|
111 |
# ***** ERROR MANAGEMENT ***** # | |
|
112 |
# **************************** # | |
|
113 | ||
|
114 |
?> | |
2. Add some comments discussing the nature and purpose of this page.
* File name: config.inc.php
* Created by: Larry E. Ullman of ^ DMC Insights, Inc.
* Contact:
^ LarryUllman@DMCInsights.com, ^ http://www.dmcinsights.com
* Last modified: November 8, 2006
* Configuration file does the ^ following things:
* - Has site settings in one ^ location.
* - Stores URLs and URIs as ^ constants.
Because the configuration file is a common file, it ought to be the best-documented script in a site.
3. Set the email address to be used for errors.
$contact_email = ^ 'address@example.com';
For live sites, I prefer to be emailed when errors occur. So I declare a variable with the "to" email address. This may be my address while developing a site or a client's once the site goes live.
4. Determine if the script is running on the live server or a test server.
^ (substr($_SERVER['HTTP_HOST'], ^ 0, 7) == '192.168')) {
I almost always develop on a local server and then upload the completed site to the live server. The two environments will have server-specific settings, so the configuration file ought to confirm which is the current environment. To see if the site is running locally, I check two conditions against $_SERVER['HTTP_HOST']. If that variable contains the word local (as in http://localhost or http://power-book.local) or if the IP address begins with 192.168 (indicating a local network), then a $local variable is set as TRUE. Otherwise, it is FALSE.
5. Set the server-specific constants. if ($local) {
$debug = TRUE; define ('BASE_URI', ^ '/path/to/html/folder/');
define ('BASE_URL', ^ 'http://localhost/directory/');
define ('DB', ^ '/path/to/mysql.inc.php'); } else {
define ('BASE_URI', ^ '/path/to/live/html/folder/');
define ('BASE_URL', ^ 'http://www.example.com/'); define ('DB',
continues on next page
I always use these three constants in my Web applications. The BASE_URI is the absolute path to where the site's root folder is on the server (Figure 2.2). This constant makes it easy to use absolute URLs when any script includes a file. The BASE_URL constant is the hostname and directory (if applicable). On a test server, that might be just http://local.host/ ch02/. Finally, the DB constant is the absolute path to the file that contains the database connectivity information. For security purposes, it's best to keep this stored outside of the Web directory. Note that each constant is represented twice: once for a test server and once for the live server. If this is a test server ($local is TRUE), I also turn on debugging, which will mean more shortly.
6. Set the debugging level. if (!isset($debug)) { $debug = FALSE;
I use a $debug variable to indicate how errors should be handled. If the site is being run locally, $debug will be TRUE. To debug a live site, a page would need to use the line $debug = TRUE;
prior to including the configuration file. In all other cases, debugging is turned off.
- Figure 2.2 The root folder is where the site's index page may be found. Within the root folder, other subfolders—such as images, includes, and modules-would be found.
7. Begin a function for handling errors. function my_error_handler ($e_number, ^ $e_message, $e_file, $e_line, ^ $e_vars) {
global $debug, $contact_email; PHP allows you to define your own function for handling errors, rather than using the built-in behavior. For more information on this process or the syntax, see the PHP manual or my PHP and MySQL for Dynamic Web Sites: Visual QuickStart Guide book (Peachpit Press, 2005). Two global variables will be used in this function.
8. Build up the error message.
$message = "An error occurred in ^ script '$e_file' on line $e_line: ^ \n<br />$e_message\n<br />"; $message .= "Date/Time: " . date('n-^ j-Y H:i:s') . "\n<br />"; $message .= "<pre>" . print_r ^ ($e_vars, 1) . "</pre>\n<br />"; For debugging purposes, the error message should be as informative as possible. To start, it will include the name of the file where the error occurred and on what line. Then the date and time of the error are appended to the message. Finally, every existing variable is added. This can be a lot of data (Figure 2.3), but that's a good thing when you need to find and fix a problem.
continues on next page
|
aoo |
Site Home Page CD | ||
|
[HTTP_USER_AGENT) => Mozilla/5.0 (Macintosh; Uj Intel Mac OS X; en-US; rv | |||
|
[PATH) => /bins/sbins/usr/bin:/usr/sbins/usr/libexec:/System/Library/Core | |||
|
[REMOTEADDR] => 127.0.0.1 | |||
|
|REMOTE_PORT] => 57316 | |||
|
(SCBltTPILEMAME] => /UBerB/larryullman/Sites/ch02/index.php | |||
|
[SCRIPT_URI] => http://powerbook.Iocal/ch02/index.php | |||
|
[ SCRIPT_URI.] => /ch02/index .php | |||
|
[SERVER_ADDRj => 127.0.0.1 | |||
|
[SERVER_ADM1U] => [no address given] | |||
|
[SERVER NAME] => powerbook.local | |||
|
[£ERVER_PORT] => 90 | |||
|
[SERVERSIGNATURE] => | |||
|
Apache/I.3. |
33 Server at powerbook.local Port 80 | ||
|
ISERVER_SOFTWARE) »> Apache/1.3.33 (Darwin) PHt/5.l.S | |||
|
[GATEWAY!NTERPACE] => CGI/1.1 | |||
|
[SERVER_PROTOCOL| => HTTP/1.1 | |||
|
|REQUEST_METHOD) => GET | |||
|
[QUERYSTRING| => | |||
|
[REQUESTURl] => /ch02/index.php | |||
|
[SCR1PTNAME] => /ch02/index.php | |||
|
[PATH_TRANSLATED) => /Users/larryullman/Sites/ch02/index.php | |||
|
(PHP_SELF) => /ch02/index.php | |||
|
) |
[RE(JUEST_TIME] => 1163123666 | ||
|
t_email) -> addresaeexample.com | |||
|
I local |
=> 1 | ||
|
,debug, |
=> 1 | ||
|
[page] | |||
|
) |
itle) => Site Home Page |
; | |
|
© □ | |||
Figure 2.3 This is just some of the data reported when an error occurs.
m DO
Figure 2.3 This is just some of the data reported when an error occurs.
9. If debugging is turned on, print the error. if ($debug) {
echo '<p class="error">' . ^ $message . '</p>'; If debugging is turned on, then the full message will appear in the Web browser (Figure 2.4). This is great when developing a site but a huge security flaw on a live site. You can also edit this code to fit into your site's design.
10. If debugging is turned off, send the message in an email and print a default message.
if ( ($e_number != E_NOTICE) && ^ ($e_number < 2048)) {
echo '<p class= ^ "error">A system error occurred. ^ We apologize for the
For a live site, the detailed error message should not be shown (unless debugging is temporarily enabled for that page) but should be emailed instead. The error_log() function will do this, if provided with the number 1 as its second argument. But the user probably needs to know that something didn't go right, so a generic message is displayed (Figure 2.5). If the error happens to be a notice or a strict error (having a value of 2048), no message should be printed, as the error is likely not interfering with the operation of the page.
- Figure 2.4 How errors appear when debugging a page.
Figure 2.5 On a live site, errors are handled more modestly (and securely).
Figure 2.5 On a live site, errors are handled more modestly (and securely).
11. Complete the function, tell PHP to use this error handler, and complete the page.
} // End of my_error_handler() ^ definition. set_error_handler
12. Save the file as config.inc.php and place it in your Web directory (in an includes subfolder).
Figure 2.2 shows the directory layout I'll use for this site.
Post a comment