Archive

Posts Tagged ‘PHP’

PHP function to javascript

June 26, 2012 Leave a comment

php function ที่แปลงไปเป็น ภาษา js
http://phpjs.org/

Categories: Jquery, PHP Tags: ,

ฟังก์ชั่นเช็ค GIF ว่าเป็น Animation หรือไม่

February 16, 2012 Leave a comment

function is_ani($filename) {
$filecontents = file_get_contents($filename);

$str_loc = 0;
$count = 0;
while ($count < 2) { # There is no point in continuing after we find a 2nd frame
$where1 = strpos($filecontents, “\x00\x21\xF9\x04”, $str_loc);
if ($where1 === FALSE) {
break;
} else {
$str_loc = $where1 + 1;
$where2 = strpos($filecontents, “\x00\x2C”, $str_loc);
if ($where2 === FALSE) {
break;
} else {
if ($where1 + 8 == $where2) {
$count++;
}
$str_loc = $where2 + 1;
}
}
}

if ($count > 1) {
return(true);
} else {
return(false);
}
}

Categories: PHP Tags: , , ,

วิธีดัก Error อัตโนมัติโดย PHP

August 16, 2011 Leave a comment

ก่อนอื่นให้สร้าง Function รองรับค่า Error ก่อน ตัวอย่างดังนี้

function customError($error_level,$error_message,$error_file,$error_line,$error_context)
{
$text = "<b>Error Level:</b> $error_level<br />";
$text .= "<b>Error Level:</b> $error_message<br />";
$text .= "<b>Error File:</b> $error_file<br />";
$text .= "<b>Error Line:</b> $error_line<br />";
$text .= "<b>Error Context:</b> $error_context<br />";
}

เราสามารถนำตัวแปร $text ไปประยุกต์ใช้เพิ่มข้อมูล Error Log ลง Database หรือให้ส่งเมล์ก็ได้

หลังจากนั้นใช้ฟังก์ชั่นด้านล่าง เพื่อให้ PHP ทราบว่าจะส่งค่า Error ไปที่ฟังก์ชั่นไหน และต้องการดัก Error Level ใด

set_error_handler(“customError”,E_ALL);

Error Report levels

สามารถเปลี่ยนแปลงการแจ้ง Error ได้ ด้วยการระบุ Level Error ที่ต้องการดัก

Value Constant Description
2 E_WARNING Non-fatal run-time errors. Execution of the script is not halted
8 E_NOTICE Run-time notices. The script found something that might be an error, but could also happen when running a script normally
256 E_USER_ERROR Fatal user-generated error. This is like an E_ERROR set by the programmer using the PHP function trigger_error()
512 E_USER_WARNING Non-fatal user-generated warning. This is like an E_WARNING set by the programmer using the PHP function trigger_error()
1024 E_USER_NOTICE User-generated notice. This is like an E_NOTICE set by the programmer using the PHP function trigger_error()
4096 E_RECOVERABLE_ERROR Catchable fatal error. This is like an E_ERROR but can be caught by a user defined handle (see also set_error_handler())
8191 E_ALL All errors and warnings, except level E_STRICT (E_STRICT will be part of E_ALL as of PHP 6.0)

เท่านี้ก็ดัก Error ได้ตามที่ต้องการแล้ว

Categories: PHP Tags: , ,

วิธีแก้ Cache แบบบ้าคลั่งของ IE8

April 20, 2011 Leave a comment
<?php
// prevent caching (php)
header('Cache-Control: no-cache');
header('Pragma: no-cache');
header('Expires: ' . gmdate(DATE_RFC1123, time()-1));
?>

ของผมเอาไปใส่ไว้ใน tag <head> ของไฟล์ Header ใช้งานได้ดีทีเดียว
สำหรับภาษาอื่น ๆ ดูได้ที่ IE8 aggressive caching fix

Categories: PHP Tags: , ,