了解过PHP内核的同学都知道,PHP的一次请求的生命周期
1.启动Apache后,PHP解释程序也随之启动。PHP调用各个扩展的MINIT方法,从而使这些扩展切换到可用状态
2.当一个页面请求发生时,SAPI层将控制权交给PHP层。于是PHP设置了用于回复本次请求所需的环境变量。同时,它还建立一个变量表,用来存放执行过程 中产生的变量名和值。PHP调用各个模块的RINIT方法,即“请求初始化”。RINIT方法可以看作是一个准备过程, 在程序执行之间就会自动启动
3.如同PHP启动一样,PHP的关闭也分两步。一旦页面执行完毕(无论是执行到了文件末尾还是用exit或die函数中止),PHP就会启动清理程序。它会按顺序调用各个模块的RSHUTDOWN方法。 RSHUTDOWN用以清除程序运行时产生的符号表,也就是对每个变量调用unset函数。
4.最后,所有的请求都已处理完毕,SAPI也准备关闭了,PHP开始执行第二步:PHP调用每个扩展的MSHUTDOWN方法,这是各个模块最后一次释放内存的机会
以上是运行在Apache服务中,而Nginx+FastCgi似乎不是这样,这一点我会后期深究,今天主要探讨日志功能,便也后期学习
首先我们按照我的这篇博文(在Linux下编写php扩展)生成一个扩展
然后我们在myext.c文件中添加一个日志函数,然后分别在
PHP_MINIT_FUNCTION,PHP_MSHUTDOWN_FUNCTION,PHP_RINIT_FUNCTION,PHP_RSHUTDOWN_FUNCTION四个函数中添加调用日志的函数
然后编译扩展,重新服务,允许等操作,看看是否有日志
下面我给出一个完整的文件
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2016 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: |
+----------------------------------------------------------------------+
*/ /* $Id$ */ #ifdef HAVE_CONFIG_H
#include "config.h"
#endif #include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_myext.h" /* If you declare any globals in php_myext.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(myext)
*/ /* True global resources - no need for thread safety here */
static int le_myext;
void save_log();
/* {{{ PHP_INI
*/
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("myext.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_myext_globals, myext_globals)
STD_PHP_INI_ENTRY("myext.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_myext_globals, myext_globals)
PHP_INI_END()
*/
/* }}} */ /* Remove the following function when you have successfully modified config.m4
so that your module can be compiled into PHP, it exists only for testing
purposes. */ /* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_myext_compiled(string arg)
Return a string to confirm that the module is compiled in */
PHP_FUNCTION(confirm_myext_compiled)
{
char *arg = NULL;
int arg_len, len;
char *strg; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
return;
} len = spprintf(&strg, , "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "myext", arg);
RETURN_STRINGL(strg, len, );
} void save_log(char *str)
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime ); char buf[];
sprintf(buf, "%s", asctime (timeinfo)); FILE *fp = fopen("/Users/zongshuai/log/c.log", "a+");
////这里写一个绝对路径,请改成一下,大家还得注意权限问题,如果发现打印不出内容,检查一下权限
if (fp==) {
printf("can't open file\n");
return;
} strcat(str,"\r\n"); fseek(fp, ,SEEK_END);
fwrite(buf, strlen(buf) - , , fp);
fwrite(" ", , , fp);
fwrite(str, strlen(str) * sizeof(char), , fp);
fclose(fp);
return;
} PHP_FUNCTION(sum_two_num)
{
long x,y,z; if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &x,&y) == FAILURE) {
RETURN_FALSE;
} char str[] = "EXECED MYFUNCTION";
save_log(str); z = x * y; RETURN_LONG(z);
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
unfold functions in source code. See the corresponding marks just before
function definition, where the functions purpose is also documented. Please
follow this convention for the convenience of others editing your code.
*/ /* {{{ php_myext_init_globals
*/
/* Uncomment this function if you have INI entries
static void php_myext_init_globals(zend_myext_globals *myext_globals)
{
myext_globals->global_value = 0;
myext_globals->global_string = NULL;
}
*/
/* }}} */ /* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(myext)
{
/* If you have INI entries, uncomment these lines
REGISTER_INI_ENTRIES();
*/
char str[] = "EXECED PHP_MINIT_FUNCTION";
save_log(str);
return SUCCESS;
}
/* }}} */ /* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(myext)
{
/* uncomment this line if you have INI entries
UNREGISTER_INI_ENTRIES();
*/
char str[] = "EXECED PHP_MSHUTDOWN_FUNCTION";
save_log(str);
return SUCCESS;
}
/* }}} */ /* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(myext)
{
char str[] = "EXECED PHP_RINIT_FUNCTION";
save_log(str);
return SUCCESS;
}
/* }}} */ /* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(myext)
{
char str[] = "EXECED PHP_RSHUTDOWN_FUNCTION";
save_log(str);
return SUCCESS;
}
/* }}} */ /* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(myext)
{
php_info_print_table_start();
php_info_print_table_header(, "myext support", "enabled");
php_info_print_table_end(); /* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}
/* }}} */ /* {{{ myext_functions[]
*
* Every user visible function must have an entry in myext_functions[].
*/
const zend_function_entry myext_functions[] = {
PHP_FE(confirm_myext_compiled, NULL) /* For testing, remove later. */
PHP_FE(sum_two_num, NULL)
PHP_FE_END /* Must be the last line in myext_functions[] */
};
/* }}} */ /* {{{ myext_module_entry
*/
zend_module_entry myext_module_entry = {
STANDARD_MODULE_HEADER,
"myext",
myext_functions,
PHP_MINIT(myext),
PHP_MSHUTDOWN(myext),
PHP_RINIT(myext), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(myext), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(myext),
PHP_myext_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */ #ifdef COMPILE_DL_myext
ZEND_GET_MODULE(myext)
#endif /*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/