JochenJung
JochenJung

Reputation: 7213

PHP closes connection after 10sec without data beeing echoed

I have a PHP webpage, that generates statistics and takes about 15sec to generate them, before sending them to the Browser.

The issue I have is that the browser ends up loading with a blank page. Unfortunately I don't have access to the error_log file.

But I figured, that if I start sending data in the foreach-loop which consumes most of the generation time like so

echo ' ';

the connection will not terminate and the page will load.

First I thought this might be a memory_limit or max_execution_time issue, so I increased both, without any luck. But this would also seam odd to me since hitting the max_execution_time would result in a blank page no matter I'm sending echo ' ' or not.

Is there an other PHP setting, I don't know of, that would cause a connection to terminate after about 10sec without data beeing send?

Edit:

The page is not completely blank. Here is the data beeing sent:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <meta name="author" content="Gerd Grützmacher">
  <meta name="robots" content="index,follow">
  <meta http-equiv="Language" content="de">  
  <link rel="icon" href="/favicon.png" type="image/png">

  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

  <!--[if (gte IE 5.9)]><!-->
  <link rel="stylesheet" type="text/css" href="css/gruppenunterkuenfte-min.css" />
  <!--<![endif]-->

  <!--[if IE 6]>
  <style type="text/css" media="screen, projection">
       @import url(css/ie6.css);
  </style>
  <![endif]-->

  <!--[if gte IE 5.9]><!-->
    <script type="text/javascript" src="scripts.js"></script>
  <!--<![endif]-->

After this point the statistics are beeing generated and therefore I only see a blank page.

Here is the HTTP-Header:

quest URL:http://gruppenunterkuenfte.de/index.php?mod=home&action=stats_belegungsanfrage2
Request Method:GET
Status Code:200 OK
Request Headers
Accept:application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Cache-Control:max-age=0
Referer:http://gruppenunterkuenfte.de/index.php?mod=home&action=stats_belegungsanfrage2
User-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3
Query String Parameters
mod:home
action:stats_belegungsanfrage2
Response Headers
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:close
Content-Type:text/html; charset=utf-8
Date:Sat, 25 Sep 2010 15:07:11 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Pragma:no-cache
Server:Apache/2.2.16
Transfer-Encoding:chunked
X-Powered-By:PHP/5.2.11

Upvotes: 3

Views: 604

Answers (10)

mellowsoon
mellowsoon

Reputation: 23261

It's possible your code is crashing Apache, or maybe PHP is crashing. In both cases no error messages could be sent back to the browser. Are you using the same versions of Apache and PHP on your home computer for testing? The same OS?

Instead of echo '' inside of your loop, trying sleep(10). You'll know it's not a timeout issue if the page doesn't go blank after 10 seconds.

Take a look at what you're doing inside the foreach loop. There's a hundred things that could crash Apache, PHP, or both. Especially in scenarios where you doing heavy data crunching.

Upvotes: 0

Sekhar
Sekhar

Reputation: 5797

How about sending the browser two components, one ajax block, and one iframe - hidden iframe may be.. Use the iframe's meta-refresh to keep it refreshing - and using the ajax block to wait till your php code outputs..

Upvotes: 0

Sabeen Malik
Sabeen Malik

Reputation: 10880

Here are my 2 cents:

First off i would make sure that all types of buffering is off. A gentleman was kinda enough to provide this code once:

@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);
for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
ob_implicit_flush(1);

After that i would suggest that you send an echo ' '; after some intervals. For instance if your code has a loop, send an echo after lets say every 10th iteration or something like that, based on how long each iteration takes. That way you aren't wasting time sending echos and you page stays alive as well.

Upvotes: 0

ts.
ts.

Reputation: 10709

did you try to sens any header just at the begining of script (Content-Type or something like that)?

Upvotes: 0

bcosca
bcosca

Reputation: 17555

Make sure you're not sending out control characters. They'll terminate HTML output. You might want to try:

echo preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F]/','',$output);

This will output all characters including whitespaces (tabs, carriage returns, line feeds).

Upvotes: 0

Bery
Bery

Reputation: 1094

have you tried to debug the code on a local computer? I had some troubles setting error messages on the server of my ISP. They managed them for all of the clients and hid them - thinking that it would prevent customers from seeing ugly warnings or errors (e.g. after php update or such).

  1. if you do not see any error try to test it on localhost
  2. if you do not see any connection timeout in you browser (at least my firefox prints it), then the timeout is not caused in between the server and client.

I suggest you try to debug the code - try to locate why the output stops exactly on line with the JS file inclusion. Does the script allways stop on the same line?

Upvotes: 0

wimvds
wimvds

Reputation: 12850

Unfortunately I don't have access to the error_log file.

Errr... Why don't you point error_log to a file/location you have access to? Just set it at the start of your script using ini_set, ie.

ini_set('log_errors', 1);
ini_set('error_log', '/path/to/yourlog.log');

You could also change the value of error_reporting as you see fit.

Upvotes: 0

Petah
Petah

Reputation: 46040

Check that an error is not occurring by enabled error reporting before you do anything:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

Upvotes: 0

mrpatg
mrpatg

Reputation: 10117

Not sure how your code is laid out, but if you are looping through any results to display them, try echoing them each loop, or echo something each loop.

Without seeing code, hard to tell.

Upvotes: 0

jay.lee
jay.lee

Reputation: 19837

Based on the info so far, I'm thinking that the browser may be timing out the connection due to inactivity.

When you output the delayed single char (' ' in your case) try putting a flush() call after it to try and flush the buffer through the network and hopefully generate some activity. If this doesn't work, perhaps try sending a larger string; you may need to fill the output buffer before it will flush at all.

Upvotes: 1

Related Questions