I’m using an external server (RSS Digest) to cache the feeds you (may) see in the lower right hand corner of the front page. Occasionally their server craps out, and prior to my recent changes to the blog’s code, the page would only half render. There are several steps I’ve made to fix this:

1) First, I split up the page into seperate, independent, DIVs. Previously everything was in one giant DIV, which looked nice (the bottom was always squared off) but it meant that if something on the sidebar was taking a long time to load, the styles from the CSS wouldn’t be applied to much of the page. Disaster. So now there are a bunch of independent DIVs.

2) The blog’s software (wordpress) is based on PHP; putting the code:

< ?php flush();?>

before the external includes tells the server to write everything generated so far to the browser.

3) Instead of using straightforward PHP includes, use the following instead (put it in a php file, and then include that php file in the head of the page). It will attempt to get a specified url, and will time out after a number of seconds. You can deal with the timeout in various ways. First, the function:


< ?php
function GetPage($addr, $to=1) {
    preg_match('/^http:\/\/([^\/]*)(.*)$/i', trim($addr), $m);
    $host = $m[1];
    $target = $m[2];
    if (trim($target)=='') {
        $target = '/';
    }

    $fp = @fsockopen ($host, 80, $errno, $errstr, $to);

    $res = '';
    if (!$fp) {
           return (FALSE);
    } else {
       @fputs ($fp, "GET $target HTTP/1.0\r\nHost: $host\r\n\r\n");
       while (!feof($fp)) {
           $res .= @fgets ($fp,128);
       }
       @fclose ($fp);
    }
     return(substr(strstr($res, "\r\n\r\n"), 4));
}
?>
 

Now call it from your page using something like:


< ?php $blogroll= GetPage("http://...blah.html", 2.0);
      if ($blogroll === FALSE) {
          echo("RSS down. Try later.");
      } else {
          echo($blogroll);
      }
?>

Thanks to this guy for the code.



Related Leave a Comment