Tag Archive ดีบัก

Byphunsanit

php: xdebug

การเขียน unit testing หรือทำ uat โปรแกรมที่เขียนว่ามีข้อบกพร่องบ้างหรือเปล่า การ debug ใน php จะนิยมใช้ XDEBUG EXTENSION FOR PHP การติดตั้ง xdebug ใน xampp ไม่ยากเลย (ง่ายกว่าใน docker ตั้งเยอะ) ผมเคยเขียนการติดตั้งไว้ใน ตรวจสอบโค้ดและวัดความเร็ว PHP ด้วย xdebug แต่ไม่ได้อธิบายขั้นตอนอย่างละเอียดเอาไว้ ครั้งนี้จะเขียนวิธีอย่างละเอียด

การติดตั้ง xdebug

  1. download extension มาจาก XDEBUG EXTENSION FOR PHP | DOWNLOADS
    แต่ถ้าไม่แน่ใจ ทางผู้พัฒนาได้ทำ Tailored Installation Instructions ไว้ให้ แค่ copy หน้า phpinfo ของเว็บที่เราใช้มาวางใน textarea แล้วกด Analyse my phpinfo() output ก็จะได้ version และ config มาแล้ว
  2. นำไฟล์ .dll ไปไว้ใน php’s Extensions directory: เช่น C:\xampp\php\ext
  3. เปิดไฟล์ php’s Configuration File: เช่น C:\xampp\php\php.ini เพิ่มคอนฟิก
    [Xdebug]
    xdebug.profiler_append = 1
    xdebug.profiler_enable = 0
    xdebug.profiler_enable_trigger = 0
    xdebug.profiler_output_dir = "C:\xampp\tmp"
    xdebug.profiler_output_name = cachegrind.out.%t.%p
    xdebug.remote_enable = true
    xdebug.remote_handler = "dbgp"
    xdebug.remote_host = "localhost"
    xdebug.remote_port = 9000
    xdebug.trace_output_dir = "C:\xampp\tmp"
    zend_extension = "C:\xampp\php\ext\php_xdebug-2.5.5-7.1-vc14.dll"

    ด้านล่างของไฟล์

  4. เปิดและปิด Apache ใหม่อีกครั้ง

เปิดหน้า phpinfo อีกครั้ง ลองค้นหาคำว่า xdebug อีกครั้ง ถ้าเจอแปลว่าการติดตั้งเสร็จแล้ว

Byphunsanit

PHP: ดูการทำงานสร้าง flowchart (backtrace)

ถ้าเรียนเขียน program มาคงจะคุ้นเคยกับ flowchart การทำงานของของโปรแกรม แต่ในชีวิตการทำงานจริงๆ โดยเฉพาะถ้าเขียน code โดยใช้ framework ต่างๆ จะไม่เป็นเหมือนที่ได้ออกแบบไว้เสมอไป เพราะว่าบางครั้งโปรแกรมจะทำงานให้เราเองโดยที่ไม่ได้สั่ง สาเหตุคือ มีการเรียกใช้ hook หรือ tricker ทำให้เกิด process ที่อาจจะไม่ทราบที่มา หรือต้องมาแก้งานของคนอื่นโดนที่ไม่มีเอกสารให้

นอกจากการใช้วิธี PHP: list included หรือ required ไฟล์ที่ใช้ ถ้าเราใช้ตัว framework ต่างๆ มักจะมีการเตรียม function ในการสร้าง backtrace ไว้ให้ แต่ถ้าไม่มีหรือเป็น code ที่เขียนด้วยตัวเอง ยังสามารถใช้ debug_backtrace ในการค้นหาที่มาที่ไปได้เช่น

<?php

function getBacktrace()
{
    $backtrace = debug_backtrace();
    echo '<pre>', print_r($backtrace, true), '</pre>';
    fwrite(fopen('logs_debug_backtrace.txt', 'a+'), "\n\n" . __FILE__ . ' :' . __LINE__ . "\n\n" . print_r($backtrace, true));
}

function getPhpInfo($what)
{
    phpinfo($what);

    /* จะดูว่า ทำไม่ getPhpInfo ถึงทำงาน */
    getBacktrace();
}

function index()
{
    getPhpInfo(INFO_ENVIRONMENT);
}

index();

เมื่อทดสอบดูจะได้ผลลัพธ์

Array
(
    [0] => Array
        (
            [file] => D:\xampp\htdocs\snippets\PHP\debug.debug_backtrace.php
            [line] => 13
            [function] => getBacktrace
            [args] => Array
                (
                )

        )

    [1] => Array
        (
            [file] => D:\xampp\htdocs\snippets\PHP\debug.debug_backtrace.php
            [line] => 18
            [function] => getPhpInfo
            [args] => Array
                (
                    [0] => 16
                )

        )

    [2] => Array
        (
            [file] => D:\xampp\htdocs\snippets\PHP\debug.debug_backtrace.php
            [line] => 21
            [function] => index
            [args] => Array
                (
                )

        )

)

เพราะว่าเราเขียน function debug_backtrace() ไว้ใน function getBacktrace() อีกชั้น เพื่อที่ความง่ายในการค้นหา flow เมื่อต้องการดูว่า function หรือ class ที่เราสนใจมันมี flow มายังไงก็แค่ไปเรียกใน function ที่สงสัย เราก็จะได้ลำดับการทำงานย้อนกลับไป เราจะได้ทราบทั้งฟังก์ชั้น คลาส บรรทัด ไฟล์ แม้แต่ตัวแปรที่ส่งเข้าไปใน function นั้นๆ

Byphunsanit

PHP: list included หรือ required ไฟล์ที่ใช้

การเขียนโปรแกรมโดยใช้ php นิยมจะแยกไฟล์ออกเป็นส่วนๆ เพื่อความสะดวกในการเขียนและ reused ไฟล์เพื่อที่จะนำมาใช้ซ้ำอีกครั้ง เช่น ในเว็บทั่วไปมันจะมีส่วนของ header และ footer เหมือนกันทุกๆหน้า แทนนี้จะเขียนส่วนหัวและส่วนท้ายทุกครั้ง สำหรับทุกหน้าเพื่อแสดงผลเหมือนๆ กันก็จะนิยมสร้างเป็นไฟล์ แยกออกไป จากนั้นก็แทรกเข้ามาโดยใช้ function included หรือ required

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title><?php echo $title; ?></title>
</head>
<body>

และ

</body>
</html>

ถ้าหากต้องมีการเปลี่ยน design ก็สามารถแก้ได้โดยแก้ใน 2 ไฟล์นี้เท่านั้นแทนที่จะต้องไปไล่เปลี่ยนในทุกๆไฟล์ (มีแค่ 100 หน้าก็เหนื่อยแล้ว)

แต่เมื่อโครงสร้างไฟล์ใน project เริ่มมีความซับซ้อน เช่น ไม่ทราบว่าไฟล์ที่เราใช้คำสั่ง included หรือ required นั้นมีไฟล์ไหนบ้าง เพราะว่าไฟล์ที่ include เข้ามาสามารถแทรกไฟล์ส่วนอื่นๆ เข้ามาเพิ่มได้เหมือนกันเช่น header.php อาจจะมีการแทรกไฟล์ menu.php เพื่อแสดงส่วนของเมนู ภาษาพีเฮชพีจึงได้เตรียม function get_included_files เข้ามาช่วย debug ไฟล์ทั้งหมดที่แทรกเอาเข้ามา

ตัวอย่างการใช้งาน

<?php
$title = 'names of included or required files';
include 'header.php';

function getIncludedFiles()
{
    $files = get_included_files();
    echo '<pre>', print_r($files, true), '</pre>';
    fwrite(fopen('logs_get_included_files.txt', 'a+'), "\n\n" . __FILE__ . ' :' . __LINE__ . "\n\n" . print_r($files, true));

}

getIncludedFiles();

include 'footer.php';

ทดลองเรียกดู จะเห็นว่าจะแสดงเป็น array

Array
(
    [0] => D:\xampp\htdocs\snippets\PHP\get_included_files.php
    [1] => D:\xampp\htdocs\snippets\PHP\header.php
)

สังเกตดูจะเป็นว่าจะแสดง array ออกมาและเขียนไฟล์ logs_get_included_files.txt เพิ่มขึ้นมา โดยเริ่มจากไฟล์ debug.get_included_files.php เองและแสดงไฟล์ header.php ที่แทรกเข้ามา แต่กลับไม่แสดง footer.php เพราะว่าแทรกเข้ามาหลัง function get_included_files() ในการใช้งานจริงๆ จึงควรแทรกไว้ที่ลำดับการทำงานสุดท้ายเช่นไฟล์ footer.php จึงจะสามารถ list ไฟล์ที่ใช้ได้ครบ

นอกจากนี้ยังสามารถเขียนให้จบในบรรทัดเดียวได้

fwrite(fopen('get_included_files.txt', 'a+'), print_r(get_included_files(), true));

Byphunsanit

PHP: json_encode ไม่มีผลลัพธ์

เจอว่า json_encode มันไม่ return ผลลัพธ์ ออกมาเลยในหน้าเดียวกัน แต่ต่างกันที่ product id ลอง print_r / var_dump ตัว array ที่เข้าไปใน function ก็ปกติดี ไม่ได้มากมาย หรือมีเครื่องหมายพิเศษอะไร ลอง debug ดูถึงเจอว่าถ้าใช้ function json_last_error() มันจะ return เลข 7 กลับมาในหน้าที่มีปัญหา ลองค้นดูรหัส 7 คือ infinity number ลอง unset ตัว key ที่เป็น float(-INF) ดูก็กลับเป็นปกติจริงๆ

รหัสต่างๆ คือ

JSON error codes
Code Constant Meaning Availability
0 JSON_ERROR_NONE No error has occurred  
1 JSON_ERROR_DEPTH The maximum stack depth has been exceeded  
2 JSON_ERROR_STATE_MISMATCH Invalid or malformed JSON  
3 JSON_ERROR_CTRL_CHAR Control character error, possibly incorrectly encoded  
4 JSON_ERROR_SYNTAX Syntax error  
5 JSON_ERROR_UTF8 Malformed UTF-8 characters, possibly incorrectly encoded PHP 5.3.3
6 JSON_ERROR_RECURSION One or more recursive references in the value to be encoded PHP 5.5.0
7 JSON_ERROR_INF_OR_NAN One or moreNANor INFvalues in the value to be encoded PHP 5.5.0
8 JSON_ERROR_UNSUPPORTED_TYPE A value of a type that cannot be encoded was given PHP 5.5.0
9 JSON_ERROR_INVALID_PROPERTY_NAME A property name that cannot be encoded was given PHP 7.0.0
10 JSON_ERROR_UTF16 Malformed UTF-16 characters, possibly incorrectly encoded PHP 7.0.0
<!doctype html>
<html>
   <head>
      <meta charset="utf-8">
      <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
      <title>PHP: json_encode error</title>
      <meta content="Pitt Phunsanit" name="author" />
   </head>
   <body>
      <div class="container">
         <?php
            $datas = [
            	'ininfity' => -9e1000,
            	'title' => 'title',
            ];
            ?>
         <div class="row"><label class="col-md-2" for="">Datas Arrya:</label><textarea class="col-md-10" cols="100" rows="6"><?=var_dump($datas); ?></textarea></div>
         <div class="row"><label class="col-md-2" for="">Json Datas:</label><textarea class="col-md-10" cols="100" rows="6"><?=json_encode($datas); ?></textarea></div>
         <div class="row">
            <label class="col-md-2" for="">Json Error:</label>
            <div class="col-md-10"><?=json_last_error(); ?></div>
         </div>
         <table class="table table-striped">
            <caption><strong>JSON error codes</strong></caption>
            <thead>
               <tr>
                  <th>Code</th>
                  <th>Constant</th>
                  <th>Meaning</th>
                  <th>Availability</th>
               </tr>
            </thead>
            <tbody class="tbody">
               <tr>
                  <td><strong><code><?=JSON_ERROR_NONE; ?></code></strong></td>
                  <td><strong><code>JSON_ERROR_NONE</code></strong></td>
                  <td>No error has occurred</td>
                  <td class="empty">&nbsp;</td>
               </tr>
               <tr>
                  <td><strong><code><?=JSON_ERROR_DEPTH; ?></code></strong></td>
                  <td><strong><code>JSON_ERROR_DEPTH</code></strong></td>
                  <td>The maximum stack depth has been exceeded</td>
                  <td class="empty">&nbsp;</td>
               </tr>
               <tr>
                  <td><strong><code><?=JSON_ERROR_STATE_MISMATCH; ?></code></strong></td>
                  <td><strong><code>JSON_ERROR_STATE_MISMATCH</code></strong></td>
                  <td>Invalid or malformed JSON</td>
                  <td class="empty">&nbsp;</td>
               </tr>
               <tr>
                  <td><strong><code><?=JSON_ERROR_CTRL_CHAR; ?></code></strong></td>
                  <td><strong><code>JSON_ERROR_CTRL_CHAR</code></strong></td>
                  <td>Control character error, possibly incorrectly encoded</td>
                  <td class="empty">&nbsp;</td>
               </tr>
               <tr>
                  <td><strong><code><?=JSON_ERROR_SYNTAX; ?></code></strong></td>
                  <td><strong><code>JSON_ERROR_SYNTAX</code></strong></td>
                  <td>Syntax error</td>
                  <td class="empty">&nbsp;</td>
               </tr>
               <tr>
                  <td><strong><code><?=JSON_ERROR_UTF8; ?></code></strong></td>
                  <td><strong><code>JSON_ERROR_UTF8</code></strong></td>
                  <td>Malformed UTF-8 characters, possibly incorrectly encoded</td>
                  <td>PHP 5.3.3</td>
               </tr>
               <tr>
                  <td><strong><code><?=JSON_ERROR_RECURSION; ?></code></strong></td>
                  <td><strong><code>JSON_ERROR_RECURSION</code></strong></td>
                  <td>One or more recursive references in the value to be encoded</td>
                  <td>PHP 5.5.0</td>
               </tr>
               <tr>
                  <td><strong><code><?=JSON_ERROR_INF_OR_NAN; ?></code></strong></td>
                  <td><strong><code>JSON_ERROR_INF_OR_NAN</code></strong></td>
                  <td>
                     One or more
                     <a class="link" href="http://php.net/manual/de/language.types.float.php#language.types.float.nan" target="_blank"><strong><code>NAN</code></strong></a>
                     or <a class="link" href="http://php.net/manual/de/function.is-infinite.php" target="_blank"><strong><code>INF</code></strong></a>
                     values in the value to be encoded
                  </td>
                  <td>PHP 5.5.0</td>
               </tr>
               <tr>
                  <td><strong><code><?=JSON_ERROR_UNSUPPORTED_TYPE; ?></code></strong></td>
                  <td><strong><code>JSON_ERROR_UNSUPPORTED_TYPE</code></strong></td>
                  <td>A value of a type that cannot be encoded was given</td>
                  <td>PHP 5.5.0</td>
               </tr>
               <tr>
                  <td><strong><code><?=JSON_ERROR_INVALID_PROPERTY_NAME; ?></code></strong></td>
                  <td><strong><code>JSON_ERROR_INVALID_PROPERTY_NAME</code></strong></td>
                  <td>A property name that cannot be encoded was given</td>
                  <td>PHP 7.0.0</td>
               </tr>
               <tr>
                  <td><strong><code><?=JSON_ERROR_UTF16; ?></code></strong></td>
                  <td><strong><code>JSON_ERROR_UTF16</code></strong></td>
                  <td>Malformed UTF-16 characters, possibly incorrectly encoded</td>
                  <td>PHP 7.0.0</td>
               </tr>
            </tbody>
         </table>
      </div>
   </body>
</html>
Byphunsanit

เก็บ CodeIgniter log ด้วย hook

วิธีที่จะพัฒนาและดูแลเว็บคือทำระบบ log ที่จะบันทึกข้อมูลการใช้งาน ซึ่งจะบันทึกสิ่งที่เกิดขึ้นไว้สำหรับเข้ามาดูเหตุการณ์ที่เกิดขึ้นย้อนหลังได้ มีประโยชน์ในการดูแลระบบโดยเฉพาะตัวที่เป็น API ให้ระบบอื่นๆเรียกใช้ สำหรับ ci เราสามารถเขียนได้โดยการใช้ hook

ระบบ Hook (ตะขอ) คือ เป็นตะขอที่เกี่ยวกับเหตุการณ์ซักอย่างแล้วจึงทำงาน เหมือนกับ tricker ในดาต้าเบส หรือจะ อธิบายการทำงานง่ายๆ ก็เหมือน คุณไปที่ร้านอาหารคิดไม่ออกว่าจะทานอะไร ใช้วิธีรอให้เพื่อนสั่งแล้วบอกว่า หมีทู่ (me too.) นั้นละครับ

  1. ก่อนอื่น ไปเปิดการใช้งาน hook ก่อน เปิดไฟล์ /application /config/config.php แก้ $config[‘enable_hooks’] = FALSE; เป็น TRUE;
  2. เปิดไฟล์ /application /config/ hooks.php เพิ่มบรรทัด
    $hook['post_system'][] = array(
            'class' => '',
            'function' => 'log_Profiling',
            'filename' => 'log_Profiling.php',
            'filepath' => 'hooks'
    );
    
  3. จากนั้นไปสร้างไฟล์ log_Profiling.php ใน /application/hooks เนื้อหาตามนี้ครับ
    <?php
    function log_Profiling(){
    global $CI ,$application_folder;
    	$output = '<html><body>';
    	$output .= '<fieldset id="ci_profiler_benchmarks" style="border:1px solid #c00000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">
    <legend style="color:#c00000;">OUTPUT</legend>'.$CI->output->get_output().'</fieldset>';
    	if( ! isset($_POST['profiler'])){
    		$CI->load->library('profiler');
    		if ( ! empty($CI->_profiler_sections)){
    			$CI->profiler->set_sections($this->_profiler_sections);
    		}
    		$output .= $CI->profiler->run();
    	}
    	$output .= '</body></html>';
    	$fp = fopen($application_folder.'/logs/Profiling/'.date('Y-m-d-H-i-s-U').'_'.$CI->router->fetch_class().'_'.$CI->router->fetch_method().'.html' ,'w');
    	flock($fp ,LOCK_EX);
    	fwrite($fp ,$output);
    	flock($fp ,LOCK_UN);
    	fclose($fp);
    }
    
  4. สร้างโฟลเดอร์ application/logs/Profiling/