Danh mục tài liệu

Secure PHP Development- P24

Số trang: 5      Loại file: pdf      Dung lượng: 96.05 KB      Lượt xem: 8      Lượt tải: 0    
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Secure PHP Development- P24: Welcome to Secure PHP Development: Building 50 Practical Applications. PHPhas come a long way since its first incarnation as a Perl script. Now PHP is a powerfulWeb scripting language with object-oriented programming support. Slowlybut steadily it has entered the non-Web scripting arena often reserved for Perl andother shell scripting languages. Arguably, PHP is one of the most popular Web platforms.
Nội dung trích xuất từ tài liệu:
Secure PHP Development- P2486 Part II: Developing Intranet Solutions Listing 4-3 (Continued) } function print_banner() { if ($this->banner_printed == TRUE) { return 0; } $out = “myTextColor’>” . “Debugger started for $this->prefix” . “”; if ($this->buffer == TRUE ){ $this->buffer_str .= $out; } else { echo $out; $this->banner_printed = TRUE; } return 1; } function write($msg) { $out = sprintf(“%03d ” . “%s\n”, $this->myTextColor, $this->line++, $this->color, $msg); if ($this->buffer == TRUE) { $this->buffer_str .= $out; } else { echo $out; } } Chapter 4: Architecture of an Intranet Application 87 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_printed member 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_str variable is used to store buffered debug information. The banner_printed variable, which88 Part II: Developing Intranet Solutions controls the banner printing, is set to FALSE. The debugger can be invoked in an application called test_debugger1.php as follows: In this example, a new Debugger object called $myDebugger is 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 PREFIX is set when the object is created. Chapter 4: Architecture of an Intranet Application 89 ◆ 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 thisdebugging facility: 90 Part II: Developing Intranet Solutions // Write the variable out using debugger write() method $myDebugger->write(“Name = $name”); ?> This will print a message such as the following: 000 Name = M. J. Kabir 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.