Monday, August 5, 2019

[Laravel - Model] ข้อควรระวังของการใช้ฟังก์ชั่น where และ orWhere ของ Laravel Model
[Laravel - Model] Warning: Using where and orWhere functions of Laravel Model

สมมติว่า
  • OrganizationChildren เป็น Laravel model ที่ผูกกับตาราง organization_children ในฐานข้อมูล
  • OrganizationChildren ประกอบด้วยฟิลด์ parent_organization_id และ child_organization_id 
  • $org1->id และ $org2->id เป็นค่า id ของ Organization โดยมีค่าเป็น 1 และ 2 ตามลำดับ

ถ้าเราต้องการ query ข้อมูล โดยใช้ SQL ดังนี้
select * 
from `organization_children` 
where (`parent_organization_id` = 1 and `child_organization_id` = 2) or 
(`parent_organization_id` = 2 and `child_organization_id` = 1)

[Laravel - Package] วิธีการเปลี่ยนข้อมูลที่ใช้สำหรับ Subject Claim ของ JWT ใน Laravel Model
[Laravel - Package] How to Change Data for Subject Claim of JWT in Laravel Model

JSON Web Token (JWT) ถูกใช้ใน stateless web application

jwt-auth เป็น JWT package ที่นิยมใช้สำหรับ Laravel framework

package นี้จะใช้ ID ของผู้ใช้งานเป็น sub claim (subject claim) โดยอัตโนมัติ

เนื่องจาก payload ของ JWT สามารถ decode ที่ใดก็ได้ โดยไม่จำเป็นต้องใช้ key ใดๆ การใช้ ID ของผู้ใช้งาน จึงทำให้เกิดปัญหาทางด้านความปลอดภัย

คุณสามารถเปลี่ยนฟิลด์ ID ของผู้ใช้งานเป็นฟิลด์อื่นด้วยวิธีตามด้านล่างนี้ เพื่อหลีกเลี่ยงปัญาด้านความปลอดภัย

Saturday, August 3, 2019

[PHP] วิธีการพิมพ์ข้อความออกทาง STDOUT หรือ STDERR หรือ SYSLOG
[PHP] How to Print Out Message to STDOUT or STDERR or SYSLOG

ในบางครั้งคุณมีความจำเป็นต้องพิมพ์ข้อความออกทาง system log หรือ I/O streams อื่นๆ เช่น standard output, standard error

สมมติว่า $str เป็นข้อความที่คุณต้องการ print

คุณสามารถใช้ method ข้างล่างสำหรับการพิมพ์ข้อความออก system log
syslog(LOG_INFO, $str);
อ่านข้อมูลเพิ่มเติมที่ https://www.php.net/manual/en/function.syslog.php

คุณสามารถใช้ method ข้างล่างสำหรับการพิมพ์ข้อความออก I/O streams อื่นๆ
$out = fopen('php://stdout', 'w'); //output handler
fputs($out, $str); //writing output operation
fclose($out); //closing handler
โดยที่ php://stdout สามารถแทนด้วย php://stderr หรือ php://output
อ่านข้อมูลเพิ่มเติมที่ https://www.php.net/manual/en/wrappers.php.php

Monday, July 8, 2019

[Docker] วิธีการ Bash หรือ Shell เข้าไปภายใน Docker Container ด้วยผู้ใช้งานที่กำหนด
[Docker] How to Bash or Shell into Docker Container with Specific User

คุณสามารถ shell เข้าไปใน docker container ได้ โดยใช้คำสั่งดังนี้
docker exec -it --user <username> <container_id> bash
โดยที่
  • username เป็น username ของผู้ใช้งานภายใน Docker container
  • container_id เป็น ID ของ Docker container นั้นๆ
โดยปกติแล้ว คุณไม่จำเป็นต้องใส่ user option ก็ได้ แต่คุณจะ shell เข้าไปเป็น root user ซึ่งคำสั่งที่ใช้จะเป็นดังนี้
docker exec -it <container_id> bash
ในบางครั้ง Docker container อาจจะไม่มี bash คุณต้องเปลี่ยนไปใช้คำสั่งดังนี้แทน
docker exec -it <container_id> /bin/sh

Monday, July 1, 2019

[PHP] วิธีการเปิดใช้ environment variables ใน PHP FPM
[PHP] How to Enable Environment Variables in PHP FPM

PHP FPM จะถูก clear environment variables ออก เนื่องจากเหตุผลทางด้านความปลอดภัย

แต่ในบางครั้งเรามีความจำเป็นต้องใช้ตัวแปรพวกนี้ เช่น environment variables ที่ถูกส่งผ่านเข้ามาจากการสร้าง Docker image เพื่อเก็บข้อมูล log หรือสถิติต่างๆ

สำหรับการอนุญาตให้ environment variables ถูกส่งต่อไปยัง PHP FPM ให้แก้ไขไฟล์ /etc/php/7.2/fpm/pool.d/www.conf โดยเอา comment บรรทัดข้างล่างนี้ออก
;clear_env = no
จะได้ผลลัพธ์ดังภาพข้างล่าง

หลังจากนั้นให้ restart service php7.2-fpm ด้วยคำสั่งดังนี้
service php7.2-fpm restart