dongliu4320 2009-05-15 13:07
浏览 40
已采纳

当php设置为loglog时,记录REQUEST_URI变量

This is the source code of php_log_err. I would like to modify it to be able to log the variable _SERVER["REQUEST_URI"]

/* {{{ php_log_err
 */
PHPAPI void php_log_err(char *log_message TSRMLS_DC)
{
        FILE *log_file;
        char error_time_str[128];
        struct tm tmbuf;
        time_t error_time;

        /* Try to use the specified logging location. */
        if (PG(error_log) != NULL) {
#ifdef HAVE_SYSLOG_H
                if (!strcmp(PG(error_log), "syslog")) {
                        php_syslog(LOG_NOTICE, "%.500s", log_message);
                        return;
                }
#endif
                log_file = VCWD_FOPEN(PG(error_log), "ab");
                if (log_file != NULL) {
                        time(&error_time);
                        strftime(error_time_str, sizeof(error_time_str), "%d-%b-%Y %H:%M:%S", php_localtime_r(&error_time, &tmbuf));
                        fprintf(log_file, "[%s] ", error_time_str);
                        fprintf(log_file, "%s", log_message);
                        fprintf(log_file, "%s", PHP_EOL);
                        fclose(log_file);
                        return;
                }
        }

        /* Otherwise fall back to the default logging location, if we have one */

        if (sapi_module.log_message) {
                sapi_module.log_message(log_message);
        }
}
/* }}} */

I've got this which shouldn't be far from the solution but I can't get it to work:

char key[] = "REQUEST_URI";
int key_size = sizeof(key);
zval **hsv;
zval **var;

if (SUCCESS != zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &hsv)) {
    if (SUCCESS != zend_hash_find(Z_ARRVAL_PP(hsv), (char *) key, key_size, (void **) &var)) {
        if (!(Z_STRVAL_PP(var) && Z_STRLEN_PP(var))) {
        }
    }
}
  • 写回答

1条回答 默认 最新

  • douming4359 2009-05-15 14:58
    关注

    Nevermind, I finally found the answer...

    char *ip_address;
    char *uri;
    
    ip_address = sapi_getenv("HTTP_X_FORWARDED_FOR", 20 TSRMLS_CC);
    if (ip_address == NULL) {
      ip_address = sapi_getenv("REMOTE_ADDR", 11 TSRMLS_CC);
      if (ip_address == NULL) {
        ip_address = "no_ip_address";
      }
    }
    
    uri = sapi_getenv("REQUEST_URI", 11 TSRMLS_CC);
    if (uri == NULL) {
      uri = "no_uri";
    }
    
    php_syslog(LOG_NOTICE, "%.500s [ip:%s][uri:%s]", log_message, ip_address, uri);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化