Chapter 2: Understanding and Avoiding Security Risks
Identifying the Sources of Risk
Minimizing User-Input Risks
Not Revealing Sensitive Information
Summary
Chapter 3: PHP Best Practices
Best Practices for Naming Variables and Functions
Best Practices for Function/Method
Best Practices for Database
Best Practices for User Interface
Best Practices for Documentation
Best Practices for Web Security
Best Practices for Source Configuration Management
Summary
Part II
Chapter 4: Architecture of an Intranet Application
Understanding Intranet Requirements
Building an Intranet Application Framework
Creating a Database Abstraction Class
Creating an Error Handler Class
Creating a Built-In Debugger Class
Creating an Abstract Application Class
Creating a Sample Application
Summary
Chapter 5: Central Authentication System
How the System Works
Creating an Authentication Class
Creating the Central Login Application
Creating the Central Logout Application
Creating the Central Authentication Database
Testing Central Login and Logout
Making Persistent Logins in Web Server Farms
Summary
Chapter 6: Central User Management System
Identifying the Functionality Requirements
Creating a User Class
User Interface Templates
Creating a User Administration Application
Creating a User Password Application
Creating a Forgotten-Password Recovery Application
Summary
Chapter 7: Intranet System
Identifying Functionality Requirements
Designing the Database
Designing and Implementing the Intranet Classes
Setting Up Application Configuration Files
Setting Up the Application Templates
Intranet Home Application
Installing Intranet Applications from the CD- ROM
Testing the Intranet Home Application
Summary
Chapter 8: Intranet Simple Document Publisher
Identifying the Functionality Requirements
The Prerequisites
Designing the Database
The Intranet Document Application Classes
Setting up Application Configuration Files
Setting Up the Application Templates
The Document Publisher Application
Installing Intranet Document Application
Testing Intranet Document Application
Summary
Chapter 9: Intranet Contact Manager
Functionality Requirements
Understanding Prerequisites
The Database
The Intranet Contact Manager Application Classes
The Application Configuration Files
The Application Templates
The Contact Category Manager Application
The Contact Manager Application
Installing Intranet Contract Manager
Testing Contract Manager
Summary
Chapter 10: Intranet Calendar Manager
Identifying Functionality Requirements
Understanding Prerequisites
Designing the Database
The Intranet Calendar Application Event Class
The Application Configuration Files
The Application Templates
The Calendar Manager Application
The Calendar Event Manager Application
Installing the Event Calendar on Your Intranet
Testing the Event Calendar
Summary
Chapter 11: Internet Resource Manager
Functionality Requirements
Understanding the Prerequisites
Designing the Database
Designing and Implementing the Internet Resource Manager Application Classes
Creating Application Configuration Files
Creating Application Templates
Creating a Category Manager Application
Creating a Resource Manager Application
Creating a Resource Tracking Application
Creating a Search Manager Application
Installing an IRM on Your Intranet
Testing IRM
Security Concerns
Summary
Chapter 12: Online Help System
Functionality Requirements
Understanding the Prerequisites
Designing and Implementing the Help Application Classes
Creating Application Configuration Files
Creating Application Templates
Creating the Help Indexing Application
Creating the Help Application
Installing Help Applications
Testing the Help System
Security Considerations
Summary
Part III
Chapter 13: Tell-a-Friend System
Functionality Requirements
Understanding Prerequisites
Designing the Database
Designing and Implementing the Tell- a- Friend Application Classes
Creating Application Configuration Files
Creating Application Templates
Creating the Tell-a-Friend Main Menu Manager Application
Creating a Tell-a-Friend Form Manager Application
Creating a Tell-a-Friend Message Manager Application
Creating a Tell-a-Friend Form Processor Application
Creating a Tell-a-Friend Subscriber Application
Creating a Tell-a-Friend Reporter Application
Installing a Tell-a-Friend System
Testing the Tell-a-Friend System
Security Considerations
Summary
Chapter 14: E-mail Survey System
Functionality Requirements
Architecture of the Survey System
Designing the Database
Designing and Implementing the Survey Classes
Designing and Implementing the Survey Applications
Developing Survey Execution Manager
Setting Up the Central Survey Configuration File
Setting Up the Interface Template Files
Testing the Survey System
Security Considerations
Summary
Chapter 15: E-campaign System
Features of an E-campaign System
Architecting an E-campaign System
Designing an E-campaign Database
Understanding Customer Database Requirements
Designing E-campaign Classes
Creating Common Configuration and Resource Files
Creating Interface Template Files
Creating an E-campaign User Interface Application
Creating a List Manager Application
Creating a URL Manager Application
Creating a Message Manager Application
Creating a Campaign Manager Application
Creating a Campaign Execution Application
Creating a URL Tracking and Redirection Application
Creating an Unsubscription Tracking Application
Creating a Campaign Reporting Application
Testing the E-Campaign System
Security Considerations
Summary
Part IV
Chapter 16: Command-Line PHP Utilities
Working with the Command-Line Interpreter
Building a Simple Reminder Tool
Building a Geo Location Finder Tool for IP
Building a Hard Disk Usage Monitoring Utility
Building a CPU Load Monitoring Utility
Summary
Chapter 17: Apache Virtual Host Maker
Understanding an Apache Virtual Host
Defining Configuration Tasks
Creating a Configuration Script
Developing makesite
Installing makesite on Your System
Testing makesite
Summary
Chapter 18: BIND Domain Manager
Features of makezone
Creating the Configuration File
Understanding makezone
Installing makezone
Testing makezone
Summary
Part V
Chapter 19: Web Forms Manager
Functionality Requirements
Understanding Prerequisites
Designing the Database
Designing and Implementing the Web Forms Manager Application Classes
Creating the Application Configuration Files
Creating Application Templates
Creating the Web Forms Submission Manager Application
Creating the Web Forms Reporter Application
Creating the CSV Data Exporter Application
Installing the Web Forms Manager
Testing the Web Forms Manager
Security Considerations
Summary
Chapter 20: Web Site Tools
Functionality Requirements
Understanding Prerequisites
Designing the Database
Designing and Implementing the Voting Tool Application Class
Creating the Application Configuration Files
Creating the Application Templates
Creating the Vote Application
Installing the Voting Tool
Testing the Voting Tool
Summary
Part VI
Chapter 21: Speeding Up PHP Applications
Benchmarking Your PHP Application
Buffering Your PHP Application Output
Compressing Your PHP Application Output
Caching Your PHP Applications
Summary
Chapter 22: Securing PHP Applications
Controlling Access to Your PHP Applications
Securely Uploading Files
Using Safe Database Access
Recommended php.ini Settings for a Production Environment
Limiting File System Access for PHP Scripts
Running PHP Applications in Safe Mode
Summary
Part VII
Appendix A: What's on the CD-ROM
System Requirements
What's on the CD
Troubleshooting
Appendix B: PHP Primer
Object-Oriented PHP
Appendix C: MySQL Primer
Using MySQL from the Command- Line
Using phpMyAdmin to Manage MySQL Database
Appendix D: Linux Primer
Installing and Configuring Apache 2.0
Installing and Configuring MySQL Server
Installing and Configuring PHP for Apache 2.0
Common File/Directory Commands
Index
Wiley Publishing, Inc. End-User License Agreement
Nội dung
Listing 5-1 (Continued) $this->status = TRUE; $this->user_id = $row->USER_ID; } else { $this->status = FALSE; } } $dbi->disconnect(); return $this->status; } function getUID() { return $this->user_id; } } ?> The following are the functions in this class: ◆ Authentication(): This is the constructor method, which sets the default values of the authentication object. First it sets the status variable to FALSE to signify that authentication is not successful yet. The e-mail vari- able is set to the e-mail address supplied as part of the parameter. (The authentication system uses e-mail address as the username and, therefore, it is a required item in the user-supplied credential.) The password para- meter is stored in the password variable. The function also sets the auth_tbl and db_url variables to authentica- tion table name and the fully qualified database name of the central authentication database. ◆ authenticate(): This function performs the authentication. It retrieves active UID and PASSWORD fields for the given e-mail address. If the user account has been deactivated (ACTIVE = 0), then the method returns default authentication status (FALSE), and if the user account is active and the encrypted version of the given password matches the stored crypto- graphic password, then the method returns TRUE status, which indicates successful authentication. 126 Part II: Developing Intranet Solutions 08 549669 ch05.qxd 4/4/03 9:24 AM Page 126 Now using this class (class.Authentication.php) and our existing application framework, let’s create central login and logout applications. Creating the Central Login Application The purpose of the login application is to present a username and password entry interface using an HTML template, and then to authenticate the user. If the user is successfully authenticated by the class.Authentication.php object, the login application creates the session data necessary to let other applica- tions know that the user is already authenticated and has valid credentials. If the user doesn’t supply valid credentials, the login application should allow the user to try a few times (say three times) and, if she fails after retrying for a config- urable number of times, then she is taken to an HTML page showing a warning about potential abuse of the system. This is to stop non-users from abusing the system. Valid users who have forgotten their passwords can run another login helper application to send new passwords via e-mail.This helper application is discussed in Chapter 6. Listing 5-2 shows the login application login.php, which implements these features. Listing 5-2: login.php <?php require_once “login.conf”; require_once “login.errors”; /* Session variables must be defined before session_start() method is called */ $count = 0; class loginApp extends PHPApplication { Continued Chapter 5: Central Authentication System 127 08 549669 ch05.qxd 4/4/03 9:24 AM Page 127 Listing 5-2 (Continued) function run() { global $MIN_USERNAME_SIZE, $MIN_PASSWORD_SIZE, $MAX_ATTEMPTS; global $WARNING_URL, $APP_MENU; $email = $this->getRequestField(‘email’); $password = $this->getRequestField(‘password’) ; $url = $this->getRequestField(‘url’); $emailLen = strlen($email); $passwdLen = strlen($password); $this->debug(“Login attempts : “ . $this- >getSessionField(‘SESSION_ATTEMPTS’)); if ($this->is_authenticated()) { // return to caller HTTP_REFERRER $this->debug(“User already authenticated.”); $this->debug(“Redirecting to $url.”); $url = (isset($url)) ? $url : $this->getServer(); header(“Location: $url”); } else if (strlen($email) < $MIN_USERNAME_SIZE || strlen($password) < $MIN_PASSWORD_SIZE) { // display the login interface $this->debug(“Invalid Email or password.”); $this->display_login(); $_SESSION[“SESSION_ATTEMPTS”] = $this- >getSessionField(“SESSION_ATTEMPTS”) + 1; } else { // Prepare the email with domain name if (!strpos($email, ‘’)) { $hostname = explode(‘.’, $_SERVER[‘SERVER_NAME’]); if (sizeof($hostname) > 1) { $email .= ‘’ . $hostname[1] . ‘.’ . $hostname[2]; } } 128 Part II: Developing Intranet Solutions 08 549669 ch05.qxd 4/4/03 9:24 AM Page 128 // authenticate user $this->debug(“Authenticate user: $email with password $password”); if ($this->authenticate($email, $password)) { $this->debug(“User is successfully authenticated.”); $_SESSION[“SESSION_USERNAME”] = $email; $_SESSION[“SESSION_PASSWORD”] = $password; $_SESSION[“SESSION_USER_ID”] = $this->getUID(); if (empty($url)) { $url = $APP_MENU; } // Log user activity $thisUser = new User($this->dbi, $this->getUID()); $thisUser->logActivity(LOGIN); $this->debug(“Location $url”); header(“Location: $url”); $this->debug(“Redirect user to caller application at url = $url.”); } else { $this->debug(“User failed authentication.”); $this->display_login(); $_SESSION[“SESSION_ATTEMPTS”] = $this- >getSessionField(“SESSION_ATTEMPTS”) + 1; } } } function warn() { global $WARNING_URL; $this->debug(“Came to warn the user $WARNING_URL”); header(“Location: $WARNING_URL”); } function display_login() { Continued Chapter 5: Central Authentication System 129 08 549669 ch05.qxd 4/4/03 9:24 AM Page 129 Listing 5-2 (Continued) global $TEMPLATE_DIR; global $LOGIN_TEMPLATE; global $MAX_ATTEMPTS; global $REL_TEMPLATE_DIR; global $email, $url; global $PHP_SELF, $FORGOTTEN_PASSWORD_APP; $url = $this->getRequestField(‘url’); if ($this->getSessionField(“SESSION_ATTEMPTS”) > $MAX_ATTEMPTS) { $this->warn(); } $this->debug(“Display login dialog box”); $template = new Template($TEMPLATE_DIR); $template->set_file(‘fh’, $LOGIN_TEMPLATE); $template->set_block(‘fh’, “mainBlock”); $template->set_var(‘SELF_PATH’, $PHP_SELF); $template->set_var(‘ATTEMPT’, $this- >getSessionField(“SESSION_ATTEMPTS”)); $template->set_var(‘TODAY’, date(“M-d-Y h:i:s a”)); $template->set_var(‘TODAY_TS’, time()); $template->set_var(‘USERNAME’, $email); $template->set_var(‘REDIRECT_URL’, $url); $template->set_var(‘FORGOTTEN_PASSWORD_APP’, $FORGOTTEN_PASSWORD_APP); $template->parse(“fh”, “mainBlock”); $template->set_var(‘BASE_URL’, sprintf(“%s”,$this->base_url)); $template->pparse(“output”, “fh”); return 1; } function is_authenticated() { return (!empty($_SESSION[“SESSION_USERNAME”])) ? TRUE : FALSE; } function authenticate($user = null, $passwd = null) { $authObj = new Authentication($user, $passwd, $this->app_db_url); 130 Part II: Developing Intranet Solutions 08 549669 ch05.qxd 4/4/03 9:24 AM Page 130 . discussed in Chapter 6. Listing 5-2 shows the login application login .php, which implements these features. Listing 5-2: login .php < ?php require_once “login.conf”; require_once “login.errors”; /* Session. to authenticate the user. If the user is successfully authenticated by the class.Authentication .php object, the login application creates the session data necessary to let other applica- tions. for a config- urable number of times, then she is taken to an HTML page showing a warning about potential abuse of the system. This is to stop non-users from abusing the system. Valid users who