Eric @ EricBess WebHome

一味思考而不行动,将在不知不觉中变得无意义

Chinese (Simplified) flagItalian flagKorean flagPortuguese flagEnglish flagGerman flagFrench flagSpanish flagJapanese flagArabic flagRussian flagGreek flagDutch flagBulgarian flagCzech flagCroat flagDanish flagFinnish flagHindi flagPolish flagRumanian flagSwedish flagNorwegian flagCatalan flagFilipino flagHebrew flagIndonesian flagLatvian flagLithuanian flagSerbian flagSlovak flagSlovenian flagUkrainian flagVietnamese flag
By N2H

PHP的debug技术

给程序加上debug函数,来记录其他代码的结果, DEBUG为开关。 代码来源:wp-global-translator-plugin。

?Download degug.php
1
2
3
4
5
6
7
8
9
10
11
function debug($msg)
{
  if (DEBUG) {
    $today = date("Y-m-d H:i:s ");
    $myFile = dirname(__file__) . "/debug.log";
    $fh = fopen($myFile, 'a') or die("Can't open debug file. Please manually create the 'debug.log' file (inside the 'global-translator' directory) and make it writable.");
    $ua_simple = preg_replace("/(.*)\s\(.*/","\\1",$_SERVER['HTTP_USER_AGENT']);
    fwrite($fh, $today . " [from: ".$_SERVER['REMOTE_ADDR']."|$ua_simple] - " . $msg . "\n");
    fclose($fh);
  }
}

在程序的其他部分,需要时加上debug("")记录结果,如下面求当前URI的散列表现:$hash。

1
2
3
4
5
6
7
8
9
10
11
function gltr_get_page_content($lang, $url)
{
  $page = '';
  if (USE_CACHE) {
    $refresh = CACHE_TIMEOUT;
 
    $req = preg_replace('/(.*)\/$/', '\\1', $_SERVER['REQUEST_URI']);
    $req = preg_replace('/#.*$/', '', $req);
    $hash = str_replace(array('?','<','>',':','\\','/','*','|','"'), '_', $req);
    debug("Hashing uri: $req to: $hash");
    .......
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...
-欢迎为本文评级

相关日志

本文读者也关心以下内容:

  • N/A

07月 7th, 2008 作者: eric | Coding | Trackback ? | 无评论| Email This Post Print This Post | 111 views

Add a Comment

Leave a reply

No Comments