1. Trang chủ
  2. » Công Nghệ Thông Tin

Secure PHP Development- P23 pdf

5 187 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Architecture of an Intranet Application
Tác giả Evoknow, Inc.
Trường học Not Available
Chuyên ngành Secure PHP Development
Thể loại Thesis
Năm xuất bản 2003
Thành phố Not Available
Định dạng
Số trang 5
Dung lượng 112,49 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Listing 4-2 shows an error message handler, which loads and displays error mes-sages in the application’s default language.. Because an application’s default lan-guage can be changed in

Trang 1

apiVersion() : This is a utility method that returns the version number of

the DBI object The DBI abstraction class enables you to connect to any database and perform any SQL query, such as SELECT, INSERT, UPDATE, DELETE, and so forth Because it hides the database vendor-specific details from your application, porting to other databases become a much easier task

Now let’s look at how we can develop an error handler class

Creating an Error Handler Class

Every application needs to display error messages In the old days, error messages were usually hard-coded in the executable programs and were very difficult to understand, let alone modify!

Now, in the days of Web interface, we should not resort to the old way of show-ing hard-coded error messagshow-ing because the application can be used in so many parts of the world Error messages written in English are just not friendly enough for the world in this Internet age So applications that have internationalizable error message support will have broader reach

Listing 4-2 shows an error message handler, which loads and displays error mes-sages in the application’s default language Because an application’s default lan-guage can be changed in the configuration file, it becomes very easy to display error messages in different languages

Listing 4-2: class.ErrorHandler.php

<?php /*

* CVS ID: $Id$

*/

/*

* Centalizes all error messages.

* Supports internationalization of error messages.

*

* @author EVOKNOW, Inc <php@evoknow.com>

* @access public

*/

define(‘ERROR_HANDLER_LOADED’, TRUE);

class ErrorHandler {

function ErrorHandler($params = null) {

global $DEFAULT_LANGUAGE;

$this->language = $DEFAULT_LANGUAGE;

Continued

Trang 2

Listing 4-2(Continued)

$this->caller_class = (!empty($params[‘caller’])) ? $params[‘caller’] : null;

$this->error_message = array();

//error_reporting(E_ERROR | E_WARNING | E_NOTICE);

$this->load_error_code();

} function alert($code = null, $flag = null) {

$msg = $this->get_error_message($code);

if (!strlen($msg)) {

$msg = $code;

}

if ($flag == null) {

echo “<script>alert(‘$msg’);history.go(-1);</script>”;

} else if (!strcmp($flag,’close’)){

echo “<script>alert(‘$msg’);window.close();</script>”;

} else { echo “<script>alert(‘$msg’);</script>”;

} } function get_error_message($code = null) {

if (isset($code)) {

if (is_array($code)) {

$out = array();

foreach ($code as $entry) {

array_push($out, $this->error_message[$entry]);

} return $out;

} else { return (! empty(>error_message[$code])) ?

$this->error_message[$code] : null;

} } else { return (! empty(>error_message[‘MISSING’])) ?

$this->error_message[‘MISSING’] : null;

Trang 3

} function load_error_code() {

global $ERRORS;

if (empty($ERRORS[$this->language])) {

return FALSE;

} while (list($key, $value) = each ($ERRORS[$this->language])) {

$this->error_message[$key] = $value;

} return TRUE;

} }

?>

The class.ErrorHandler.php class assumes that the application has all its error messages defined in an application-specific configuration file and all error mes-sages are stored in a multidimensional array called $ERRORS For example:

<?php // US English

$ERRORS[‘US’][‘SAMPLE_ERR_CODE’] = “This is an error message.”;

// Spanish

$ERRORS[‘ES’][‘SAMPLE_ERR_CODE’] = “Esto es un mensaje de error.”;

//German

$ERRORS[‘DE’][‘SAMPLE_ERR_CODE’] = “Dieses ist eine Fehlermeldung.”;

?>

If this code is stored in appname.errorsfile and loaded by an application using

require_once(‘appname.errors’), then the ErrorHandler class can print the SAMPLE_ERR_CODEerror message in any of the three languages, depending on the default language settings

You can translate your error messages in multiple languages using Language Translation Tools provided by Google at http://translate.

google.com/translate_t Be aware that not all automatic translations are perfect.

Trang 4

You can set an application’s default language using the $DEFAULT_LANGUAGE variable in a configuration file for your application For example,

<?php // appname.conf // Default language for

$DEFAULT_LANGUAGE = ‘US’;

?>

If this configuration is loaded by an application using the ErrorHandlerclass, all error messages will be displayed in U.S English

ErrorHandler()is the constructor function for the class.ErrorHandler.php This function sets the default language of the error handler to what is set in the application configuration as global $DEFAULT_LANGUAGE variable

This method can be passed an associative array as a parameter If the parameter array has a key=value pair called caller=class_name, then it sets the member variable called caller_class to the value

The constructor also initializes a member array called error_messageand loads the error code for the default language by calling the load_error_code()method The error handler class ErrorHandler is automatically invoked by the PHPApplication classso you don’t need to create an error handler manually in your application code

Now let’s look at the other functions available in ErrorHandlerclass

alert() : This function displays an internationalized error message using

a simple JavaScript pop-up alert dialog box It is called with the error code The get_error_message()method is used to retrieve the appropri-ate error message in default application language from the application’s error configuration file

get_error_message() : This function retrieves the error messages for

given error code If an array of error codes is supplied as parameter, the function returns an array of error messages If no error code is supplied, the function returns a default error message using the MISSINGerror code

load_error_code() : This function loads the application’s error code in

from the global $ERRORSarray to its own member array variable error_message This function is called from the constructor method and does not need to be called manually, unless you want to reload error mes-sages from $ERRORS

Trang 5

Creating a Built-In Debugger Class

When developing applications, each developer uses at least some form of debug-ging Although PHP-supported Integrated Development Environments (IDEs) are becoming available, they’re still not the primary development tools for most PHP developers, who are still using echo, print, and printf functions to display debugging information during development

The debugging class called class.Debugger.phpis a bit more advanced than the standard echo, print, and printfmessages

It provides a set of facilities that include

◆ Color-coding debug messages

◆ Automatically printing debug line numbers

◆ Optionally buffering debug messages

◆ Prefixing debug messages with a given tag to make it easy to identify messages in a large application

Listing 4-3 shows the debugger class that is part of our application framework

It can be used to perform basis application debugging

Listing 4-3: class.Debugger.php

<?php

/*

* CVS ID: $Id$

*/

define(‘DEBUGGER_LOADED’, TRUE);

class Debugger { var $myTextColor = ‘red’;

function Debugger($params = null) {

// Debugger constructor method

$this->color = $params[‘color’];

$this->prefix = $params[‘prefix’];

$this->line = 0;

$this->buffer_str = null;

$this->buffer = $params[‘buffer’];

$this->banner_printed = FALSE;

Continued

Ngày đăng: 07/07/2014, 07:20

TỪ KHÓA LIÊN QUAN