繁体   English   中英

PHP性能问题require_once AJAX事件处理程序

[英]PHP performance issue require_once AJAX event handler

我有一个PHP文件可以处理来自表单的发布数据,但是当我通过require_once发布使用另一个PHP文件的表单时,似乎出现了某种性能/内存问题。 第一次处理该帖子并且性能良好,但是随后的每个帖子都会变得越来越慢,直到浏览器窗口挂起,并且我看到IIS服务器上有一些繁忙的php cgi线程。

我正在使用PHP 5.6(php-5.6.11-Win32-VC11-x86)和jQuery 2.1.4。

当按下提交按钮时,基本上没有发布整个代码,jQuery发出了XHR请求,并将表单传递给form_handler.php ,由该表单决定如​​何处理数据。

在该文件中处理的所有表单都可以正常工作,但是我有一个表单需要扩展处理,因此我试图将其卸载到另一个PHP文件中。 管理该代码的代码如下所示:

if($options[0] == "processor"){
    //Data can not be handled and must use processor
    $sql = "select processor_file from dbo.processor where reference_id = '".$options[1]."'";
    $result = query_to_field($sql);
    $processor = '../processors/'.$result;
    if(file_exists($processor)) require_once $processor;
    else echo "Error Processor Not Found<br/>";
}

然后,指定的文件执行一些操作,并生成通过回显返回的HTML。 我不确定是否是require_once的问题,我应该寻找使用curlexec来获取其他文件的输出。

收到的帖子是一个字段,处理器文件是此示例的修改后的副本

我已经对其进行了修改,以使用preg_match而不是ereg并构建$html而不是所有回显和打印,以便在完成时仅回显$html

可以提供的任何指导将不胜感激。

正如某些评论所怀疑的那样,我的确在错误的地方寻找了问题,结果是添加表单事件处理程序的JS。 该网站上的其他表格只真正被调用过一次并提交过,这就是为什么我以前没有发现这个问题。 基本上,由于事件处理程序只是在内容更改时才重新应用,因此导致提交将AJAX请求倍增,直到客户端不再说了!

错误代码:

function formhandlers(){        
    $('form').submit(function(e) {
        // get the form data
        var id = $(this).attr('id');
        // process the form
        var dsttarg = document.getElementById(id).getAttribute('target');
        var fd = new FormData(document.getElementById(id));
        postdata(fd,encodeURI('./include/post_handler.php?function='+id),dsttarg);
        e.preventDefault();
    });
}

好代码:

function formhandlers(){    
    $('form').off('submit').on('submit', function(e) {
        // get the form data
        var id = $(this).attr('id');
        // process the form
        var dsttarg = document.getElementById(id).getAttribute('target');
        var fd = new FormData(document.getElementById(id));
        postdata(fd,encodeURI('./include/post_handler.php?function='+id),dsttarg);
        e.preventDefault();
    });
}

关键区别在于该处理程序如果已经存在则关闭。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM