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
Figure 5-10: How database-based sessions persist in Web server farms. Listing 5-12 shows libsession_handler.php which implements all these functions. Listing 5-12: lib.session_handler.php <?php error_reporting(E_ALL); require_once(‘constants.php’); require_once(‘class.DBI.php’); require_once ‘DB.php’; $DB_URL = “mysql://root:foobar@localhost:/sessions”; $dbi = new DBI($DB_URL); Continued Web Server 1 Web Server 2 Load Balancer Request for application X Session Database Server Old Session File New Session File User request for application X Web Server 3 Web Server n Request 2 for application X User request for application X Chapter 5: Central Authentication System 151 08 549669 ch05.qxd 4/4/03 9:24 AM Page 151 Listing 5-12 (Continued) $SESS_LIFE = get_cfg_var(“session.gc_maxlifetime”); function sess_open($save_path, $session_name) { return true; } function sess_close() { return true; } function sess_read($key) { global $dbi, $DEBUG, $SESS_LIFE; $statement = “SELECT value FROM sessions WHERE “ . “sesskey = ‘$key’ AND expiry > “ . time(); $result = $dbi->query($statement); $row = $result->fetchRow(); if ($row) { return $row->value; } return false; } function sess_write($key, $val) { global $dbi, $SESS_LIFE; $expiry = time() + $SESS_LIFE; $value = addslashes($val); $statement = “INSERT INTO sessions “. “VALUES (‘$key’, $expiry, ‘$value’)”; $result = $dbi->query($statement); if (! $result) { $statement = “UPDATE sessions SET “ . “ expiry = $expiry, value = ‘$value’ “ . “ WHERE sesskey = ‘$key’ AND expiry > “ . time(); $result = $dbi->query($statement); } 152 Part II: Developing Intranet Solutions 08 549669 ch05.qxd 4/4/03 9:24 AM Page 152 return $result; } function sess_destroy($key) { global $dbi; $statement = “DELETE FROM sessions WHERE sesskey = ‘$key’”; $result = $dbi->query($statement); return $result; } function sess_gc($maxlifetime) { global $dbi; $statement = “DELETE FROM sessions WHERE expiry < “ . time(); $qid = $dbi->query($statement); return 1; } session_set_save_handler( “sess_open”, “sess_close”, “sess_read”, “sess_write”, “sess_destroy”, “sess_gc”); ?> Here the sess_open(), sess_close(), sess_read(), sess_destory(), and sess_gc() methods use a DBI object from our class.DBI.php class to implement database-based session management. To implement this database-based session management in our framework, we need to do the following: 1. Place the lib.session_handler.php in the framework directory. For example, if you’re keeping the class.PHPApplication.php in the /usr/php/framework directory, then you should put the lib.session_handler.php in the same directory. 2. Create a database called sessions using mysqladmin command such as mysqladmin -u root -p create sessions. You will need to know the username (here root) and password that is allowed to create databases. Next create a table called sessions using the sessions.ddl script with the mysql -u root -p -D sessions < sessions.sql command. Here’s the sessions.sql: Chapter 5: Central Authentication System 153 08 549669 ch05.qxd 4/4/03 9:24 AM Page 153 CREATE TABLE sessions ( sesskey varchar(32) NOT NULL default ‘’, expiry int(11) NOT NULL default ‘0’, value text NOT NULL, PRIMARY KEY (sesskey) ) TYPE=MyISAM; 3. Modify the following line in lib.session_handler.php to reflect your user name, password, and database host name: $DB_URL = “mysql://root:foobar@localhost:/sessions”; Here user name is root, password is foobar, and database host is local- host . You should change them if they’re different for your system. 4. Add the following line at the beginning of the class.PHPApplication.php file. require_once ‘lib.session_handler.php’; After you’ve completed these steps, you can run your login application and see that sessions are being created in the sessions table in the sessions database. To view sessions in your sessions database, run mysql -u root -p -D sessions. When you’re logged into the sessions database, you can view sessions using queries such as the following: mysql> select * from sessions; + + + + | sesskey | expiry | value | + + + + | 3b6c2ce7ba37aa61a161faafbf8c24c7 | 1021365812 | SESSION_ATTEMPTS|i:3; | + + + + 1 row in set (0.00 sec) After a successful login: mysql> select * from sessions; + + + -+ | sesskey | expiry | value | + + + -+ | 3b6c2ce7ba37aa61a161faafbf8c24c7 | 1021365820 | SESSION_ATTEMPTS|i:3;SESSION_USERNAME|s:15:”joe@evoknow.com”; | + + + -+ 154 Part II: Developing Intranet Solutions 08 549669 ch05.qxd 4/4/03 9:24 AM Page 154 1 row in set (0.00 sec) After logging out: mysql> select * from sessions; Empty set (0.00 sec) You can see that the session is started after login.php and the session is removed once the user runs logout.php. Summary In this chapter you learned about a central authentication system which involves a login and logout application and a central authentication database. All PHP appli- cations in your intranet or Web can use this central authentication facility. When an application is called directly by entering the URL in the Web browser, it can check for the existence of a session for the user and if an existing session is found, she is allowed access or else she is redirected to the login form. The logout applica- tion can be linked from any PHP application to allow the user log out at any time. Once logged out the session is removed. Having a central authentication system such as this helps you reduce the amount of code and maintenance you need to do for creating a seamless authentication process throughout your entire Web or intranet environment. Chapter 5: Central Authentication System 155 08 549669 ch05.qxd 4/4/03 9:24 AM Page 155 . libsession_handler .php which implements all these functions. Listing 5-12: lib.session_handler .php < ?php error_reporting(E_ALL); require_once(‘constants .php ); require_once(‘class.DBI .php ); require_once. lib.session_handler .php in the framework directory. For example, if you’re keeping the class.PHPApplication .php in the /usr /php/ framework directory, then you should put the lib.session_handler .php in the. your system. 4. Add the following line at the beginning of the class.PHPApplication .php file. require_once ‘lib.session_handler .php ; After you’ve completed these steps, you can run your login application