PHP slow if requested with AJAX?

I made a simple Webpage with an empty form Tag. This Tag is filled with the response of an AJAX request. The request asks a PHP script for data. This script returns its execution time. Now there is something really odd. If I type in the address by hand then the script tells me

<!-- Duration: 0.8 milliseconds (~1242 pages per second) -->

But if I use the build-in network request logger of Chrome (for watching what has been loaded) then I got this

<!-- Duration: 52.7 milliseconds (~19 pages per second) -->

Any ideas why it is 80 times slower? I repeat: Same script, same parameter, identical response (but the duration time of cause), same server, different request types: AJAX and browser address line.

<?php
class AbstractModule
{

    final function __construct(..)
    {
        // for measuring creation time
        $this->starttime = microtime(true); 
    }

    public final function return_duration()
    {
        $duration = (microtime(true) - $this->starttime) * 1000;
        return "\n<!-- Duration: " . number_format($duration, 1, '.', '') . " milliseconds (~" . number_format(1000 / $duration, 0, '.', '') . " pages per second) -->";
    }

}

$demo = new AbstractModule();
// doing very much :)<
echo $demo->return_duration();
?>

Thanks.

Answers


Do you use sessions? The difference might be that the ajax request restarts a session each time, because you don't send any cookies along.

Otherwise, I suggest you break out a debugger and track down the culprit.


Need Your Help

Simple ajax not successful

jquery html ajax

I know this is probably a basic error that I have made so my apologies in advance but I did check and cannot find an answer that deals with a really basic ajax load issue like this.