PHP的相关内容分享
分页: 1/11 第一页 1 2 3 4 5 6 7 8 9 10 下页 最后页 [ 显示模式: 摘要 | 列表 ]
php的日志是值得关注的,包含错误日志和慢日志

一 错误输出

找到php.ini
log_errors = On

; 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>


比如上面是记录超过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


日志里包含的信息:执行时间,访问入口,执行的函数的顺序和所有父级调用关系
比如这里可以获知是数据库查询时出现过慢,调用关系也一目了然,可以去排查跟踪了
Tags:
注:分析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对象
Tags: ,
今天同事说下载某个图片回来浏览器读取不了,但地址直接放浏览器是正常的。
分析了一下,该图片服务器返回的数据是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


这个其实就做一个gzip解压就可以了:
Tags: , ,
今天测试一台服务器的php代码,发现 $_SERVER["PHP_SELF"] 是空值,这个真是奇怪,原先运行nginx没事,换成lighttpd就出了问题。我在别的地方的lighttpd没有这个毛病,经过判断,可能是php编译或者设置的问题。

特别的对照了phpinfo里面的 cgi-fcgi 一栏的配置
cgi.fix_pathinfo 的值不同
找到php的配置文件,设置为1,解决~
参考整理的,代码放了挺久,忘记出处了,分离内容目前测试看的是正常
有需要的同学可以参考一下

<?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);
?>
Tags: , ,
昨天到今天,一直有个sql语句让我很郁闷

#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


一个查询语句怎么会多做事一个列呢?
在前后加上单引号,选出的数据是 categoryid=0的,更加郁闷了,明显不应该~~
虽然人老了,可脑子还没有退化到写错sql的地步

今天发现了问题,做url地址时href="xxx?id=808"
点击后发现地址栏不对,=和808之间有空格

估计是特殊字符,urlencode结果是 “%EF%BB%BF808”
前面真的是特殊字符!

这个文本是从excel复制的,问题出现在此
从excel复制文本的童鞋要注意了。。。以此为戒。。 再复制一次即可
把这个文本再复制,粘贴到另外一个文本,就正常了。
Tags: , ,
apache的配置参考

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /codeigniter
  RewriteCond %{REQUEST_URI} ^system.*
  RewriteRule ^(.*)$ /index.php/$1 [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond $1 !^(index\.php|images|robots\.txt|css)
  RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
  ErrorDocument 404 /index.php
</IfModule>
Tags: , ,
分页: 1/11 第一页 1 2 3 4 5 6 7 8 9 10 下页 最后页 [ 显示模式: 摘要 | 列表 ]

阅读推荐

服务器相关推荐

开发相关推荐

应用软件推荐