intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Secure PHP Development- P23

Chia sẻ: Cong Thanh | Ngày: | Loại File: PDF | Số trang:5

45
lượt xem
4
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Secure PHP Development- P23: Welcome to Secure PHP Development: Building 50 Practical Applications. PHP has come a long way since its first incarnation as a Perl script. Now PHP is a powerful Web scripting language with object-oriented programming support. Slowly but steadily it has entered the non-Web scripting arena often reserved for Perl and other shell scripting languages. Arguably, PHP is one of the most popular Web platforms.

Chủ đề:
Lưu

Nội dung Text: Secure PHP Development- P23

  1. Chapter 4: Architecture of an Intranet Application 81 ◆ 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 messaging 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
  2. 82 Part II: Developing Intranet Solutions 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 “alert(‘$msg’);history.go(-1);”; } else if (!strcmp($flag,’close’)){ echo “alert(‘$msg’);window.close();”; } else { echo “alert(‘$msg’);”; } } 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($this->error_message[$code])) ? $this- >error_message[$code] : null; } } else { return (! empty($this->error_message[‘MISSING’])) ? $this- >error_message[‘MISSING’] : null; }
  3. Chapter 4: Architecture of an Intranet Application 83 } 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: If this code is stored in appname.errors file and loaded by an application using require_once(‘appname.errors’), then the ErrorHandler class can print the SAMPLE_ERR_CODE error 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.
  4. 84 Part II: Developing Intranet Solutions You can set an application’s default language using the $DEFAULT_LANGUAGE variable in a configuration file for your application. For example, If this configuration is loaded by an application using the ErrorHandler class, 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_message and 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 class so you don’t need to create an error handler manually in your application code. Now let’s look at the other functions available in ErrorHandler class. ◆ 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 MISSING error code. ◆ load_error_code(): This function loads the application’s error code in from the global $ERRORS array 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.
  5. Chapter 4: Architecture of an Intranet Application 85 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.php is a bit more advanced than the standard echo, print, and printf messages. 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
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2