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

Secure PHP Development- P24 pptx

5 253 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

Định dạng
Số trang 5
Dung lượng 96,05 KB

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

Nội dung

The color is used to display the debug information in a given color.. The prefix variable is used to prefix each debug message displayed, which allows for easier identifi-cation of messa

Trang 1

Listing 4-3(Continued)

}

function print_banner() {

if ($this->banner_printed == TRUE) {

return 0;

}

$out = “<br><br><font color=’$this->myTextColor’>”

“<strong>Debugger started for $this->prefix</strong>”

“</font><br><hr>”;

if ($this->buffer == TRUE ){

$this->buffer_str = $out;

} else { echo $out;

$this->banner_printed = TRUE;

}

return 1;

}

function write($msg) {

$out = sprintf(“<font color=’%s’>%03d &nbsp;</font>”

“<font color=%s>%s</font><br>\n”,

$this->myTextColor,

$this->line++,

$this->color,

$msg);

if ($this->buffer == TRUE) {

$this->buffer_str = $out;

} else { echo $out;

} }

Trang 2

function debug_array($hash = null) {

while(list($k, $v) = each($hash)) {

$this->write(“$k = $v”);

} }

function set_buffer() {

$this->buffer = TRUE;

}

function reset_buffer() {

$this->buffer = FALSE;

$this->buffer_str = null;

}

function flush_buffer() {

$this->buffer = FALSE;

$this->print_banner();

echo $this->buffer_str;

}

}

?>

The debugger class has the following methods:

Debugger(): This is the constructor function for the debugger class

(class.Debugger.php) This function initializes the color, prefix, line, and buffer_str, banner_printedmember variables The color is used to display the debug information in a given color The prefix variable is used

to prefix each debug message displayed, which allows for easier identifi-cation of messages.

The line variable is initialized to zero, which is automatically incremented

to help locate debug information quickly The buffer_strvariable is used

to store buffered debug information The banner_printedvariable, which

Trang 3

controls the banner printing, is set to FALSE The debugger can be invoked

in an application called test_debugger1.php as follows:

<?php // Turn on all error reporting error_reporting(E_ALL);

// If you have installed framewirk directory in // a different directory than

// %DocumentRoot%/framework, change the setting below

$APP_FRAMEWORK_DIR=$_SERVER[‘DOCUMENT_ROOT’] ‘/framework’; // Insert the path in the PHP include_path so that PHP // looks for our PEAR, PHPLIB and application framework // classes in these directories

ini_set( ‘include_path’, ‘:’

$APP_FRAMEWORK_DIR ‘:’ ini_get(‘include_path’));

// Now load our Debugger class from application framework require_once(‘class.Debugger.php’);

$myDebugger = new Debugger(array(

‘color’ => ‘red’,

‘prefix’ => ‘MAIN’,

‘buffer’ => FALSE) );

// Define an array of fruits

$fruits = array(‘apple’, ‘orange’, ‘banana’);

// Show the array contents

$myDebugger->debug_array($fruits);

?>

In this example, a new Debuggerobject called $myDebuggeris created, which will print debug messages in red color and use ‘MAIN’as the prefix for each message The buffering of debug messages is disabled as well.

print_banner(): This function prints a banner message as follows:

Debugger started for PREFIX

The PREFIXis set when the object is created.

Trang 4

write(): This function displays a debug message using the chosen color

and automatically prints the debug message line number If debug buffer-ing is on, then the message is written to the buffer (buffer_str).

debug_array(): This function allows you to debug an associative array It

prints the contents of the associative array parameter using the write() method.

set_buffer(): This function sets the buffering of debug messages.

reset_buffer(): This function resets the buffering of debug messages.

flush_buffer(): This function prints the buffer content along with the

debug banner.

Now let’s look at how an application called test_debugger2.php can use this debugging facility:

<?php // Turn on all error reporting error_reporting(E_ALL);

// If you have installed framewirk directory in // a different directory than

// %DocumentRoot%/framework, change the setting below

$APP_FRAMEWORK_DIR=$_SERVER[‘DOCUMENT_ROOT’] ‘/framework’;

// Insert the path in the PHP include_path so that PHP // looks for our PEAR, PHPLIB and application framework // classes in these directories

ini_set( ‘include_path’, ‘:’

$APP_FRAMEWORK_DIR ‘:’ ini_get(‘include_path’));

// Now load our Debugger class from application framework require_once(‘class.Debugger.php’);

// Create a variable

$name = ‘M J Kabir’;

$myDebugger = new Debugger(array(

‘color’ => ‘blue’,

‘prefix’ => ‘MAIN’,

‘buffer’ => 0) );

Trang 5

// Write the variable out using debugger write() method

$myDebugger->write(“Name = $name”);

?>

This will print a message such as the following:

<font color=’red’>000 &nbsp;</font>

<font color=blue>Name = M J Kabir</font><br>

Buffering debug messages enables you to print all debug messages together, which is often very beneficial in identifying a flow sequence For example, here an application called test_debugger3.php buffers debugging information and prints the information when the buffer is flushed using flush_buffer()method found in the Debugger class.

<?php // Turn on all error reporting error_reporting(E_ALL);

// If you have installed framewirk directory in // a different directory than

// %DocumentRoot%/framework, change the setting below

$APP_FRAMEWORK_DIR=$_SERVER[‘DOCUMENT_ROOT’] ‘/framework’; // Insert the path in the PHP include_path so that PHP // looks for our PEAR, PHPLIB and application framework // classes in these directories

ini_set( ‘include_path’, ‘:’

$APP_FRAMEWORK_DIR ‘:’ ini_get(‘include_path’));

// Now load our Debugger class from application framework require_once(‘class.Debugger.php’);

// Create a variable

$name = ‘M J Kabir’;

$email = ‘kabir@evoknow.com’;

$myDebugger = new Debugger(array(

‘color’ => ‘blue’,

‘prefix’ => ‘MAIN’,

‘buffer’ => TRUE) );

$myDebugger->write(“Name = $name”);

$myDebugger->write(“Email = $email”);

echo “This will print before debug messages.\n\n”;

$myDebugger->flush_buffer();

?>

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

TỪ KHÓA LIÊN QUAN