生命之中的最大错误在于:终日担心犯错误。— 爱尔伯特·哈伯德 (Elbert Hubbard 1856-1915),《笔记》
php的日志是值得关注的,包含错误日志和慢日志
一 错误输出
找到php.ini
比如会捕获到类似信息:
[23-Oct-2011 17:17:41] PHP Fatal error: Class 'Strclass' not found in /Data/webapps/www.aslibra.com/application/errors/error_404.php on line 11
这个有助于分析被访问的页面发生的致命错误或者是警告
二 慢日志
慢日志和数据库的慢查询有点类似,会记录下来执行超过多少时间的php执行的内容
这个如果是用fast-cgi的话,可以在fpm的配置里面有
比如上面是记录超过10秒的php,记录在相应的文件里
你可以拿到类似的日志:
日志里包含的信息:执行时间,访问入口,执行的函数的顺序和所有父级调用关系
比如这里可以获知是数据库查询时出现过慢,调用关系也一目了然,可以去排查跟踪了
一 错误输出
找到php.ini
log_errors = On
; Log errors to specified file.
error_log = /Data/logs/php/error.log
; Log errors to specified file.
error_log = /Data/logs/php/error.log
比如会捕获到类似信息:
[23-Oct-2011 17:17:41] PHP Fatal error: Class 'Strclass' not found in /Data/webapps/www.aslibra.com/application/errors/error_404.php on line 11
这个有助于分析被访问的页面发生的致命错误或者是警告
二 慢日志
慢日志和数据库的慢查询有点类似,会记录下来执行超过多少时间的php执行的内容
这个如果是用fast-cgi的话,可以在fpm的配置里面有
The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file
'0s' means 'off'
<value name="request_slowlog_timeout">10s</value>
The log file for slow requests
<value name="slowlog">/Data/logs/php/slow.log</value>
'0s' means 'off'
<value name="request_slowlog_timeout">10s</value>
The log file for slow requests
<value name="slowlog">/Data/logs/php/slow.log</value>
比如上面是记录超过10秒的php,记录在相应的文件里
你可以拿到类似的日志:
Oct 23 23:00:20.528849 pid 11586 (pool default)
script_filename = /www.aslibra.com/index.php
[0x00007fffae1869e0] mysql_query() /www.aslibra.com/system/database/drivers/mysql/mysql_driver.php:163
[0x00007fffae186bb0] _execute() /www.aslibra.com/system/database/DB_driver.php:453
[0x00007fffae186eb0] simple_query() /www.aslibra.com/system/database/DB_driver.php:299
[0x00007fffae1870b0] query() /www.aslibra.com/v3/models/myuser.php:51
[0x00007fffae1872a0] get_users_by_uids() /www.aslibra.com/v3/models/myuser.php:58
[0x00007fffae187580] get_users() /www.aslibra.com/v3/controllers/pic.php:134
[0x00007fffae187870] experience() /www.aslibra.com/v3/controllers/pic.php:349
[0x00007fffae187940] user() unknown:0
[0x00007fffae187cf0] call_user_func_array() /www.aslibra.com/system/core/CodeIgniter.php:297
[0x00007fffae187f40] +++ dump failed
script_filename = /www.aslibra.com/index.php
[0x00007fffae1869e0] mysql_query() /www.aslibra.com/system/database/drivers/mysql/mysql_driver.php:163
[0x00007fffae186bb0] _execute() /www.aslibra.com/system/database/DB_driver.php:453
[0x00007fffae186eb0] simple_query() /www.aslibra.com/system/database/DB_driver.php:299
[0x00007fffae1870b0] query() /www.aslibra.com/v3/models/myuser.php:51
[0x00007fffae1872a0] get_users_by_uids() /www.aslibra.com/v3/models/myuser.php:58
[0x00007fffae187580] get_users() /www.aslibra.com/v3/controllers/pic.php:134
[0x00007fffae187870] experience() /www.aslibra.com/v3/controllers/pic.php:349
[0x00007fffae187940] user() unknown:0
[0x00007fffae187cf0] call_user_func_array() /www.aslibra.com/system/core/CodeIgniter.php:297
[0x00007fffae187f40] +++ dump failed
日志里包含的信息:执行时间,访问入口,执行的函数的顺序和所有父级调用关系
比如这里可以获知是数据库查询时出现过慢,调用关系也一目了然,可以去排查跟踪了
注:分析html的好东西
最近想用php写一个爬虫,就需要解析html,在sourceforge上找到一个项目叫做PHP Simple HTML DOM Parser,它可以以类似jQuery的方式通过css选择器来返回指定的DOM元素,功能十分强大。
首先要在程序的开始引入simple_html_dom.php这个文件
include_once('simple_html_dom.php');
PHP Simple HTML DOM Parser提供了3种方式来创建DOM对象
最近想用php写一个爬虫,就需要解析html,在sourceforge上找到一个项目叫做PHP Simple HTML DOM Parser,它可以以类似jQuery的方式通过css选择器来返回指定的DOM元素,功能十分强大。
首先要在程序的开始引入simple_html_dom.php这个文件
include_once('simple_html_dom.php');
PHP Simple HTML DOM Parser提供了3种方式来创建DOM对象
一些文件操作如果需要和web服务器通信,是可以考虑用curl的。
正常的POST表单:
上传文件:
简单的php处理代码:
正常的POST表单:
curl -d "user=nickwolfe&password=test" http://www.aslibra.com/?test.php
上传文件:
localhost:aslibra$ curl -F upload=@/Media/test.txt http://www.aslibra.com/?test/upload
Array
(
[upload] => Array
(
[name] => test.txt
[type] => text/plain
[tmp_name] => /tmp/phpKDS0MR
[error] => 0
[size] => 335
)
)
Array
(
[upload] => Array
(
[name] => test.txt
[type] => text/plain
[tmp_name] => /tmp/phpKDS0MR
[error] => 0
[size] => 335
)
)
简单的php处理代码:
print_r($_FILES);
if(isset($_FILES['upload'])){
@copy( $_FILES['upload']['tmp_name'], '/tmp/upload_'.$_FILES['upload']['name'] );
@unlink( $_FILES['upload']['tmp_name'] );
}
if(isset($_FILES['upload'])){
@copy( $_FILES['upload']['tmp_name'], '/tmp/upload_'.$_FILES['upload']['name'] );
@unlink( $_FILES['upload']['tmp_name'] );
}
今天同事说下载某个图片回来浏览器读取不了,但地址直接放浏览器是正常的。
分析了一下,该图片服务器返回的数据是gzip压缩的,而且不管客户端是否发出支持与否,都按gzip格式发了
下载回来的文件格式为:
jhs_9.jpg: ASCII text, with CRLF line terminators
显然不是图片格式,是文本流
这个其实就做一个gzip解压就可以了:
分析了一下,该图片服务器返回的数据是gzip压缩的,而且不管客户端是否发出支持与否,都按gzip格式发了
下载回来的文件格式为:
jhs_9.jpg: ASCII text, with CRLF line terminators
显然不是图片格式,是文本流
curl -I http://xxx/uploadimages/api/jhs_9.jpg
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 11 Aug 2011 01:58:15 GMT
Content-Type: image/jpeg
Connection: keep-alive
Last-Modified: Mon, 11 Jul 2011 02:05:56 GMT
ETag: "4cd000000000516-40ca-4a7c19fa8a900"
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 16560
Accept-Ranges: bytes
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 11 Aug 2011 01:58:15 GMT
Content-Type: image/jpeg
Connection: keep-alive
Last-Modified: Mon, 11 Jul 2011 02:05:56 GMT
ETag: "4cd000000000516-40ca-4a7c19fa8a900"
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 16560
Accept-Ranges: bytes
这个其实就做一个gzip解压就可以了:
1 下载文件
wget http://downloads.zend.com/optimizer/3.3.9/ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz
2 解压
tar zxvf ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz
cd ZendOptimizer-3.3.9-linux-glibc23-i386
这里要注意,进入data文件夹后,so文件是对应版本的,看好系统中的php版本再安装,我安装的是对应5.2版本PHP的。
把 ZendOptimizer.so 文件拷贝到某个目录,比如:
/usr/local/Zend/lib
把下列行加入php.ini,不要加入任何空格和tab
zend_optimizer.optimization_level=15
zend_extension=”/usr/local/Zend/lib/ZendOptimizer.so”
3 重启
/path/to/php-fpm restart
=问题集锦=
1 cannot restore segment prot after reloc: Permission denied
这是selinux的问题,如果无大的问题,可以关闭selinux
2 和eAccelerator冲突
wget http://downloads.zend.com/optimizer/3.3.9/ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz
2 解压
tar zxvf ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz
cd ZendOptimizer-3.3.9-linux-glibc23-i386
这里要注意,进入data文件夹后,so文件是对应版本的,看好系统中的php版本再安装,我安装的是对应5.2版本PHP的。
把 ZendOptimizer.so 文件拷贝到某个目录,比如:
/usr/local/Zend/lib
把下列行加入php.ini,不要加入任何空格和tab
zend_optimizer.optimization_level=15
zend_extension=”/usr/local/Zend/lib/ZendOptimizer.so”
3 重启
/path/to/php-fpm restart
=问题集锦=
1 cannot restore segment prot after reloc: Permission denied
这是selinux的问题,如果无大的问题,可以关闭selinux
引用
关闭SELinux即可解决:
修改/etc/sysconfig/selinux,修改为SELINUX=disabled
执行/usr/sbin/setenforce 0立即关闭,且无需重启系统
如果你不希望关闭SELinux的话,也可以
chcon -t shlib_t ZendOptimizer.so
chcon -t texrel_shlib_t ZendOptimizer.so
修改/etc/sysconfig/selinux,修改为SELINUX=disabled
执行/usr/sbin/setenforce 0立即关闭,且无需重启系统
如果你不希望关闭SELinux的话,也可以
chcon -t shlib_t ZendOptimizer.so
chcon -t texrel_shlib_t ZendOptimizer.so
2 和eAccelerator冲突
参考整理的,代码放了挺久,忘记出处了,分离内容目前测试看的是正常
有需要的同学可以参考一下
有需要的同学可以参考一下
<?php
#从输入读取到所有的邮件内容
$email = "";
$fd = fopen("php://stdin", "r");
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
#记录所有的内容,测试
file_put_contents("/tmp/mail/".time(), $email);
#处理邮件
$lines = explode("\n", $email);
// empty vars
$from = "";
$date = "";
$subject = "";
$message = "";
$splittingheaders = true;
for ($i=0; $i<count($lines); $i++) {
if ($splittingheaders) {
// look out for special headers
if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
$subject = $matches[1];
}
if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
if(strpos($lines[$i],"<")){
//the name exist too in from header
$data = explode('<',$lines[$i]);
$from = substr(trim($data[1]),0,-1);
}else{
//only the mail
$from = $matches[1];
}
}
if (preg_match("/^Date: (.*)/", $lines[$i], $matches)) {
$date = $matches[1];
}
} else {
// not a header, but message
$message .= $lines[$i]."\n";
}
if (trim($lines[$i])=="") {
// empty line, header section has ended
$splittingheaders = false;
}
}
$when = date("Y-m-d G:i:s");
$data = explode('@',$from);
$username = $data[0];
#记录到数据库
$sql = "insert into mails ( `username`, `from`, `subject`, `date`, `message`) values ( '$username', '$from', '$subject', '$when', '$message')";
#测试
file_put_contents("/tmp/mail2.log", $sql);
?>
#从输入读取到所有的邮件内容
$email = "";
$fd = fopen("php://stdin", "r");
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
#记录所有的内容,测试
file_put_contents("/tmp/mail/".time(), $email);
#处理邮件
$lines = explode("\n", $email);
// empty vars
$from = "";
$date = "";
$subject = "";
$message = "";
$splittingheaders = true;
for ($i=0; $i<count($lines); $i++) {
if ($splittingheaders) {
// look out for special headers
if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
$subject = $matches[1];
}
if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
if(strpos($lines[$i],"<")){
//the name exist too in from header
$data = explode('<',$lines[$i]);
$from = substr(trim($data[1]),0,-1);
}else{
//only the mail
$from = $matches[1];
}
}
if (preg_match("/^Date: (.*)/", $lines[$i], $matches)) {
$date = $matches[1];
}
} else {
// not a header, but message
$message .= $lines[$i]."\n";
}
if (trim($lines[$i])=="") {
// empty line, header section has ended
$splittingheaders = false;
}
}
$when = date("Y-m-d G:i:s");
$data = explode('@',$from);
$username = $data[0];
#记录到数据库
$sql = "insert into mails ( `username`, `from`, `subject`, `date`, `message`) values ( '$username', '$from', '$subject', '$when', '$message')";
#测试
file_put_contents("/tmp/mail2.log", $sql);
?>
昨天到今天,一直有个sql语句让我很郁闷
一个查询语句怎么会多做事一个列呢?
在前后加上单引号,选出的数据是 categoryid=0的,更加郁闷了,明显不应该~~
虽然人老了,可脑子还没有退化到写错sql的地步
今天发现了问题,做url地址时href="xxx?id=808"
点击后发现地址栏不对,=和808之间有空格
估计是特殊字符,urlencode结果是 “%EF%BB%BF808”
前面真的是特殊字符!
这个文本是从excel复制的,问题出现在此
从excel复制文本的童鞋要注意了。。。以此为戒。。 再复制一次即可
把这个文本再复制,粘贴到另外一个文本,就正常了。
#1054 - Unknown column '808' in 'where clause'
SELECT max( ma.magid ) magid
FROM aslibra.z_magazine ma
LEFT JOIN test.mag m ON m.id = ma.magid
WHERE ma.categoryid = 808
AND m.pub = '1'
GROUP BY ma.categoryid
LIMIT 0 , 30
SELECT max( ma.magid ) magid
FROM aslibra.z_magazine ma
LEFT JOIN test.mag m ON m.id = ma.magid
WHERE ma.categoryid = 808
AND m.pub = '1'
GROUP BY ma.categoryid
LIMIT 0 , 30
一个查询语句怎么会多做事一个列呢?
在前后加上单引号,选出的数据是 categoryid=0的,更加郁闷了,明显不应该~~
虽然人老了,可脑子还没有退化到写错sql的地步
今天发现了问题,做url地址时href="xxx?id=808"
点击后发现地址栏不对,=和808之间有空格
估计是特殊字符,urlencode结果是 “%EF%BB%BF808”
前面真的是特殊字符!
这个文本是从excel复制的,问题出现在此
从excel复制文本的童鞋要注意了。。。以此为戒。。 再复制一次即可
把这个文本再复制,粘贴到另外一个文本,就正常了。





