简体   繁体   中英

PHP performance issue require_once AJAX event handler

I have a PHP file that handles my post data from forms however I seem to be having some kind of performance/memory issue when I post a form that uses another PHP file via require_once . The first time the post is handled and the performance is fine but with every subsequent post it gets slower and slower until the browser window hangs and I see some busy php cgi threads on the IIS server.

I am using PHP 5.6 (php-5.6.11-Win32-VC11-x86) and jQuery 2.1.4.

Short of posting the entire code basically when the submit button is pressed jQuery makes an XHR request and the form is passed to a form_handler.php which then decides how to handle the data.

All forms that are processed within that file work fine, however I have a forms that require extended processing I am trying to offload that to another PHP file. The code that manages this looks like this:

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/>";
}

The specified file then does some stuff and generates the the HTML that is returned via an echo. I am not sure whether require_once is the issue and I should be looking to use curl or exec to get the output from the other file.

The post received is one field and the processor file is and modified copy of this example .

I have modified it to use preg_match instead of ereg and to build $html instead of all the echo's and prints so that I can just echo the $html when complete.

Any guidance that can be offered would be appreciated.

As suspected by some of the comments I was indeed looking in the wrong place, and the issue turned out to be the JS that was adding the form event handler. The other forms on the site only really get called and submitted once which is why I hadn't spotted the issue before. Basically as the the event handler was just being re-applied when the content changed is was causing the submit to multiply the AJAX request until the point the client said no more!

Bad Code:

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();
    });
}

Good code:

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();
    });
}

The key difference being that that handler is turn off if it already exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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