调试PHP错误经常用到的一些

时间:12-05-24 栏目:问题及解决 作者:admin 评论:3 点击: 16,656 次

ini_set('error_log','errorLog.txt');#记录下来所有发现的错误到文件里去。
ini_set('display_errors', 1);#显示错误
ini_set('memory_limit','512M');#有些错误的出现和内存使用有关系,把这个值调大试试看
ini_set("display_errors",0)
ini_set("error_reporting",E_ALL);
ini_set("error_log","<日志文件名>")
ini_set("log_errors",1);
#错误显示级别
if(defined('E_DEPRECATED'))
error_reporting(E_ALL & ~E_NOTICE^E_DEPRECATED);
else
error_reporting(E_ALL & ~E_NOTICE);

#所有 ini_set的内容都可以在php.ini中定义
#转义输出HTML代码
echo htmlspecialchars($html);
echo '<textarea style="width: 100%;">'.htmlspecialchars($html).'</textarea>'; 

#实时推送到浏览器的输出
ob_start(); 
ob_end_flush(); 
ob_implicit_flush(true); 
echo str_pad('',4096); 
echo 'hello'; 
flush(); 

#查看堆栈信息 echo '<pre>';debug_print_backtrace();echo '</pre>';

#指定时区
date_default_timezone_set("PRC");

#指定输出的HTML编码
header("Content-Type: text/html; charset=utf-8");

#打印函数所有的参数
echo '
<pre>';print_r(func_get_args());echo '</pre>';

#显示当前文件,行,以及执行时间
$mtime = explode(' ', microtime());
$time_start = $mtime[1] + $mtime[0];
function getmicrotime() {
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
function get_use_time($min=false) {
global $time_start;
$time_end = getmicrotime();
$times = $time_end - $time_start;
$times = sprintf('%.5f',$times);
if($min==false) {
$use_time = "用时:". $times ."秒";
}else {
$use_time = $times;
}
return $use_time;
}

echo __FILE__.':'.__LINE__.':'. get_use_time().'';

/**
* 错误处理
*/
function _e($msg,$type=''){
static $i=0;
$i++;
$error['msg'] = $msg;
$error['type'] = $type;
$GLOBALS[ERROR_STACK_NAME][$i] = $error;
return ERROR_STACK_NAME.$i;
}

function onError($errNo, $errMsg, $file, $line, $errcontext) {
$errorlevels = array(
2048 => 'Warning',
2048 => 'Notice',
1024 => 'Warning',
1024 => 'Notice',
512 => 'Warning',
256 => 'Error',
128 => 'Warning',
64 => 'Error',
32 => 'Warning',
16 => 'Error',
8 => 'Notice',
4 => 'Error',
2 => 'Warning',
1 => 'Error');
//print_r(array('code'=>$errno, 'string'=>$errstr, 'file'=>$errfile, 'line'=>$errline,'codeinfo'=>$errorlevels[$errno]));
$t = error_reporting();
if (!($errNo & error_reporting())) {
return;
}
$errType = $errNo;

/** Get Advance Error msg **/
if(strpos($errMsg,ERROR_STACK_NAME)==0){
$i = substr($errMsg,strlen(ERROR_STACK_NAME));
if(isset($GLOBALS[ERROR_STACK_NAME][$i])){
$error = $GLOBALS[ERROR_STACK_NAME][$i];
$errMsg = $error['msg'];
$errType = $error['type'];
}
}
$errMsg = nl2br($errMsg);

if($errType=='smarty'){
$errType     = '模版系统错误';

}elseif ($errType=='sql'){
$errType     = '数据库操作错误';
}else{
$errType     = "PHP错误[$errType]";
}

$title = '系统错误';

$msg = "
<strong>错误类型:</strong><blockquote>$errType</blockquote>
<strong>错误位置:</strong><blockquote>$file 第 $line 行</blockquote>
<strong>错误信息:</strong><blockquote>$errMsg</blockquote>
";
echo $msg;flush();
//msgBox($title,$msg,'',false,2);
exit();
}
function shutdown_function(){

$errorlevels = array(
2048 => 'Warning',
2048 => 'Notice',
1024 => 'Warning',
1024 => 'Notice',
512 => 'Warning',
256 => 'Error',
128 => 'Warning',
64 => 'Error',
32 => 'Warning',
16 => 'Error',
8 => 'Notice',
4 => 'Error',
2 => 'Warning',
1 => 'Error');
$error = error_get_last();
if($error){
echo '</tr></table></div></div></div></iframe><div style="clear:both"></div><div style=" background-color:#FFBB00"><pre>';
echo '最后一个错误:';
$error ['type']= $errorlevels[$error ['type']];
print_r($error );
debug_print_backtrace();
echo '</pre></div>';
}else{
echo '本页面无错误!';
}

}
register_shutdown_function('shutdown_function');#定义最后一个错误捕获

define('ERROR_STACK_NAME', 'ERROR_STACK');
set_error_handler('onError');#定义错误处理函数
error_reporting(9);

 

LH的方法是: 1.此错误前刚刚改过什么地方,从此入手 2.从链接入手,通过访问不同模块确定错误可能存在与某个模块 3.从代码入手,链接反复出错,需要查看框架其实处开始 4.从服务器配置入手,切换服务器配置

Z的方法是:

可看php的错误日志,文件要可写。

声明: 本文由( admin )原创编译,转载请保留链接: 调试PHP错误经常用到的一些

调试PHP错误经常用到的一些:目前有3 条留言

  1. 地板
    HK:

    不错。

    2012-05-24 3:08 pm [回复]
  2. 板凳
    夜い难眠:

    很实用,很给力,
    青剑大哥,高手。 💡

    2012-08-19 7:33 pm [回复]
  3. 沙发
    admin:

    很多時候,PHP出錯的原因是和UTF-8 BOM編碼有關。

    2012-08-31 11:03 pm [回复]

发表评论


------====== 本站公告 ======------
联系信息:
电话:19970108113(微信同号) QQ:3142401606
支付宝:https://me.alipay.com/lxq73061
相关插件程序等信息均会在站内发布,敬请关注。

读者排行