分类 php 下的文章

php逐行读取数据

        $inputFile = './url_input.txt';
        $outputFile = './url_output.txt';
        // 获取当前positin
        $position = I('get.position');
        if (!$position)
        {
            file_put_contents($outputFile,"");
        }
        $file = fopen($inputFile, "r");
        $retuen = array('status'=>true,'finished'=>false,'position'=>0);
        //Output a line of the file until the end is reached
        //把指针移动到当前要读取的位置
        fseek($file,$position,SEEK_SET);
        if(!feof($file))
        {
            // 读取一行数据
            $url = fgets($file);
            $url = $this->getUrlFunc($url);
            // 记录当前指针位置,用于下次请求
            $retuen['position'] = ftell($file);
            $retuen['url'] = $url;
            file_put_contents($outputFile,$url . "\r\n",FILE_APPEND);
        }
        else
        {
            $retuen = array('status'=>true,'finished'=>true);
        }
        fclose($file);
        $this->ajaxReturn($retuen);

phpsession失效

一个基于thinkphp的项目,session设置无效
在a.php页面设置完session之后,可以打印出session的值,但是刷新之后就又没了
在服务器新建一个可以访问的php文件

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
session_start();
session_register('A');
$_SESSION['A'] = 10;
echo $_SESSION['A'];
?>

访问,报如下错误,

Warning: session_start(): open(/var/lib/php/session/sess_veiso2o3bco5do2mlkvhi18jq5, O_RDWR) failed: Permission denied (13) in /home/xxx.com/public_html/test1.php on line 4 Deprecated: Function session_register() is deprecated in /home/xxx.com/public_html/test1.php on line 5 10 Warning: Unknown: open(/var/lib/php/session/sess_veiso2o3bco5do2mlkvhi18jq5, O_RDWR) failed: Permission denied (13) in Unknown on line 0 Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php/session) in Unknown on line 0

修改session的目录为程序有权限操作的目录即可
修改thinkphp入口文件,在定义 APP_PATH 的代码下加上

session_save_path(APP_PATH . 'Runtime/session/');

configure: error: libevent >= 1.4.11 could not be found

编译php5.3.3的时候报错
编译选项

./configure --prefix=/usr/local/php-5.3.3 \
--with-config-file-path=/usr/local/php-5.3.3/etc \
--with-bz2 \
--with-curl \
--enable-ftp \
--enable-sockets \
--disable-ipv6 \
--with-gd \
--with-jpeg-dir=/usr/local \
--with-png-dir=/usr/local \
--with-freetype-dir=/usr/local \
--enable-gd-native-ttf \
--with-iconv-dir=/usr/local \
--enable-mbstring \
--enable-calendar \
--with-gettext \
--with-libxml-dir=/usr/local \
--with-zlib \
--with-pdo-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-mysql=mysqlnd \
--enable-dom \
--enable-xml \
--enable-fpm \
--enable-debug \
--with-libdir=lib64

错误信息

configure: error: libevent >= 1.4.11 could not be found

安装相关的包

yum -y install libevent

依然报错
还是失败了,继续google,发现有人在ubuntu里面也遇到这个问题,再安装libevent-dev这个包之后解决问题了,于是我依样画葫芦:

yum -y install libevent-dev

安装完成

php-fpm开启core dump

转载自:https://kn007.net/topics/php-fpm-how-to-core-dump/
首先一点,需要打开debug参数,如果编译的时候没有打开,需要重新编译,编译时添加参数:

--enable-debug

设置内核core dump出来的存放路径(注意目录要有权限给php写):

echo "/tmp/core.%e.%p.%t" > /proc/sys/kernel/core_pattern

设置core dump出来的文件大小不做限制:

ulimit -c unlimited

关闭core dump只需要将大小限制为0就不会输出了(获得调试信息后设置这个就关闭core dump了)

ulimit -c 0

测试core dump是否开启成功
创建c程序文件

vim a.c
#include <stdio.h>;
int func(int *p)
{
        *p = 0;
}
int main()
{
        func(NULL);
        return 0;
}
gcc -o main a.c#编译
./main#执行,然后就会出现这个文件了

获得core dump文件,用gdb进行调试,比如:

gdb ~/main core.main.5021.iZ23sw4oxfvZ.1477623334

打开php-fpm的slowlog

  1. 编辑 php-fpm 配置文件
    最近在在转移一个discuz站点的时候,打开网站一直处在请求状态,服务器不返回任何东西,也不结束连接,因为是测试服务器,机器的负载也非常低,cpu内存什么的基本上没使用,打开一个只有一句phpinfo函数的页面又是正常的,问题也不知道如何排除,然后打开php-fpm的slowlog来追踪是哪里代码出的问题


修改request_slowlog_timeout,例如:

;10代表达到或者超过10秒的脚本
request_slowlog_timeout=10
;日志路径
slowlog = /tmp/slow.log
  1. 重启 php-fpm 即可