Xdebug’s step debugger not working in phpStorm.

TL;DR

Try deleting all PHP > Servers

Then refresh web page again. If PHP run through line that has breakpoint phpStorm will pop up for you to click accept incoming request.


It’s annoying when I change computer and sometime I encounter this problem. PHP was installed Xdebug properly, I can confirm from php -v and phpinfo(); that will show Xdebug as installed extension.

PHP 8.3.14 (cli) (built: Nov 19 2024 15:14:23) (NTS DEBUG)
Copyright (c) The PHP Group
Zend Engine v4.3.14, Copyright (c) Zend Technologies
    with Xdebug v3.3.2, Copyright (c) 2002-2024, by Derick Rethans
    with Zend OPcache v8.3.14, Copyright (c), by Zend Technologies

When I encounter this problem, it only affects on one project, while the other projects have no problem.

I started to get a clue from these threads. [1] [2]. But even though I unchecked that “Use path mappings” checkbox, it still doesn’t work. But in the end, I found that it could work again once I completely removed the server entry.

I would like to thank Steve E and Richard Pickering.

Gracefully stop PHP process with PCNTL

เวลาเขียน process ไรสักอย่างด้วย PHP ที่ใส่ loop ไว้เพื่อให้มันทำงานไปตลอดจนกว่าจะโดน kill เราสามารถใช้ PCNTL เพื่อให้ script PHP รู้ว่ามีคนขอ ให้ process หยุดทำงานได้แล้ว ถ้ามีงานที่ทำต่อเนื่องอยู่ยังไม่ถึง checkpoint จะได้มีโอกาสทำต่อให้เสร็จก่อน และก็ exit; ตัวเองซะ ไม่ต้องไปทำ task ต่อไป.

Continue reading “Gracefully stop PHP process with PCNTL”

ปัญหา CURL ไป https:// บางที่แล้ว error

ถ้าเสิร์ช Google หลายๆ ที่จะบอกให้ปิด CURLOPT_SSL_VERIFYPEER ตอนเรียกใช้ curl ซึ่งจริงๆ แล้วเพื่อความปลอดภัย เราไม่ควรจะปิดการเช็คตรงนั้น

สำหรับวิธีแก้ที่ถูกต้องคือ ให้เราไปดาวน์โหลดไฟล์ cacert.pem จากเว็บนี้ http://curl.haxx.se/docs/caextract.html มา

แล้วแก้ที่ไฟล์ php.ini ลักษณะนี้

curl.cainfo="c:\php\cacert.pem"
openssl.cafile="c:\php\cacert.pem"

Installing Apache 2.4, PHP 5.5 Windows 7

  1. สร้างโฟลเดอร์ c:/wamp/
  2. ดาวน์โหลดไฟล์ httpd-2.4.7-win32-VC11.zip (ถ้าอยากได้เวอร์ชั่นล่าสุดให้ดูที่เว็บนี้ http://www.apachelounge.com/download/) บทความนี้เลือกใช้ win32 เพราะว่า VC11 x86 Thread Safe ในเว็บ PHP มันยังเป็น experimental อยู่
  3. เอาไฟล์ของ httpd ที่โหลดในข้อก่อนหน้า มา Extract ไว้ที่พาท
    c:/wamp/apache2.4_win32/
  4. ดาวน์โหลดไฟล์ php-5.5.7-Win32-VC11-x86.zip (ถ้าอยากได้เวอร์ชั่นล่าสุดหาดูจากที่นี่ http://windows.php.net/download/ ให้เลือกอันที่เป็น VC11 Thread Safe ส่วนถ้า x64 มันไม่ experimental แล้ว ก็น่าโหลดเอามาใช้นะ แต่ต้องเปลี่ยน httpd ให้เป็น win64 ด้วย)
  5. สร้างโฟลเดอร์ c:/wamp/php-5.5.7_win32/ เอาไฟล์ที่โหลดก่อนนี้มาใส่พาทนี้
  6. แก้ไฟล์ c:/wamp/apache2.4_win32/conf/httpd.conf
    Replace: c:/Apache24/htdocs
    With: c:/wamp/www
  7. เติม code นี้เข้าไปที่ด้านล่างสุด
    LoadModule php5_module "c:/wamp/php-5.5.7/php5apache2_4.dll"
    AddHandler application/x-httpd-php .php
    PHPIniDir "c:/wamp/php-5.5.7"
    DirectoryIndex index.html index.php
  8. Load Module ดังนี้
    1. mod_expires.so
    2. mod_headers.so
    3. mod_rewrite.so
    4. mod_socache_shmcb.so
    5. mod_ssl.so
    6. mod_vhost_alias.so
  9. Uncomment บรรทัดนี้ Include conf/extra/httpd-ssl.conf
  10. แก้ไฟล์ c:/wamp/apache2.4_win32/conf/extra/httpd-ssl.conf
    1. SSLCertificateFile “C:\wamp\apache2.4_win32\conf\server.crt”
    2. SSLCertificateKeyFile “C:\wamp\apache2.4_win32\conf\server.key”
    3. DocumentRoot “c:/wamp/www”
  11. ไปที่โฟลเดอร์ php-5.5.7_win32 แล้ว copy php.ini-development ตั้งชื่อไฟล์เป็น php.ini
  12. แก้ไฟล์ php.ini
    1. extension_dir = “C:/wamp/php-5.5.7_win32/ext”
    2. date.timezone = “Asia/Bangkok”
    3. enable extensions
      1. php_curl.dll
      2. php_gd2.dll
      3. php_imap.dll
      4. php_mbstring.dll
      5. php_mysql.dll
      6. php_mysqli.dll
      7. php_openssl.dll
      8. php_tidy.dll
    4. เติมโค๊ดนี้ที่ท้ายไฟล์
      zend_extension="c:\wamp\php-5.5.7_win32\ext\dbg-php-5.5.dll"
      [debugger]
      debugger.enabled=on
      debugger.profiler_enabled=on
      debugger.hosts_allow= 127.0.0.1 localhost ::1 127.0.0.1
      debugger.hosts_deny=ALL
      debugger.ports=7869
  13. copy ไฟล์ dbg-php-5.5.dll จากในโฟลเดอร์โปรแกรม phpED ไปใส่ตรง c:\wamp\php-5.5.7_win32\ext\ ด้วย
  14. เปิด cmd โดย run as administrator
    1. รันคำสั่ง
    2. cd /wamp/apache2.4_win32/conf/
    3. ..\bin\openssl.exe req -config openssl.cnf -x509 -nodes -subj “/C=TH/ST=Bangkok/L=Bangkok/O=localhost/OU=SEC/CN=localhost” -days 1825 -newkey rsa:2048 -keyout server.key -out server.crt
    4. c:\wamp\apache2.4_win32\bin\httpd.exe -k install
  15. (ข้อนี้ข้ามไปก็ได้ ไว้ถ้ามีปัญหาค่อยกลับมาทำ)
    ใน Environment Variables ตรง System Variables
    ที่ตัวแปร PATH ให้เติม C:\wamp\apache2.4_win32\; C:\wamp\apache2.4_win32\bin ต่อเข้าไป

 

วิธีการ configure vhost

<VirtualHost 127.0.0.1:80>
  DocumentRoot "C:/wamp/site2/"
  ServerName www.site2.local
  ServerAlias site2.local www.site2.local
</VirtualHost>
<VirtualHost 127.0.0.1:443>
  DocumentRoot "C:/wamp/site2/"
  ServerName www.site2.local
  ServerAlias site2.local www.site2.local
  SSLEngine on
  SSLCertificateFile "c:/Wamp/Apache2.4_win32/conf/server.crt"
  SSLCertificateKeyFile "c:/Wamp/Apache2.4_win32/conf/server.key"
</Virtual>

ถ้าในกรณีที่ DocumentRoot ของ VirtualHost อยู่คนละพาทกับ DocumentRoot หลักใน httpd.conf ให้ไปเพิ่ม <Directory … > ….. </Directory> ใน httpd.conf ด้วย

function fgetcsv with Thai Language

ฟังก์ชั่น fgetcsv ใช้สำหรับอ่านไฟล์ csv โดยมันจะทำการ parse ออกมาเป็น Array ให้เราเลยครับ. แต่ก่อนตอนที่ผมยังไม่รู้จักคำสั่งนี้ นั่งเขียนตัว Parse เองตั้งนานครับ แถมยังมีบั๊กอีกต่างหาก (- -‘)

เข้าเรื่องครับ… ผมยังไม่ได้ศึกษาอย่างจริงจังเหมือนกัน ว่าสาเหตุมันคืออะไรกันแน่ (เพราะขี้เกียจครับ) เอาเป็นบอกปัญหาและวิธีแก้เลยละกันนะครับ

ปัญหา คือ มันไม่สามารถ parse ภาษาไทยได้ครับ  (เฉพาะบาง server ที่มี setting อะไรบางไม่ปรกติ)

วิธีแก้ ก็ง่ายๆ ครับ.. ใช้คำสั่ง  setlocale  ครับ.  ประมาณนี้

<?php
    setlocale ( LC_ALL, 'en_US.UTF-8' );
    ...
    ...
    $fields = fgetcsv ( $fp , $maxlength , $separator );
?>

ผมก็ยังไม่ได้ค้นคว้าเหมือนกันว่า มันมีตัวเลือกอะไรบ้าง และมีผลอย่างไร  แต่เอาเป็นว่าให้ใช้งานได้พอครับ