Page reloading and loading .css, .jpeg, .js as if normal php scripts

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • henryrhenryr
    New Member
    • Jun 2007
    • 103

    Page reloading and loading .css, .jpeg, .js as if normal php scripts

    Appologies for unclear title. I don't really understand the problem I'm facing.

    My system: PHP 5.2.1, Win2K, Apache 2, MySQL 5 (local - problem is same on my live site running php 4.3.9, Linux, Apache 2). Various browsers.

    No errors are reported (set to ALL and STRICT) with my problem.

    To describe the problem a little more:

    The symptoms - very slow page load. I can see the page reloading simply by observing the 'progress' bar in any browser. It will get to about 90% and then drop to 0, then get to about 90% etc. It is clearly loading the page more than once (well apparently). Otherwise, the scripts run with no problems.

    The problem (I think). In an attempt to debug this problem, I've placed a function at the very very top of my first script which sends the REQUEST_URI reference and a timestamp to a text file. Inside some of the scripts in the page, I've placed similar functions to record some variables and arrays to the same text file. I've placed a third function at the very end of the last script on my page (ie the footer).

    When I load a single page, the log file I created, shows a little more what is happening. The REQUEST_URI reference first says the page that I wanted (eg /home). Then i get something like /mycss/default.css as the file which is loaded. The odd thing is that the CSS files are all loaded inside the header which is included after the first script is loaded.

    Another oddity is that I include about 3 or 4 standard CSS files in each page and this problem never occurs with them. I have a simple script which changes the CSS files included depending on user preference and these extra files are the only ones with the problem. Incidentally, it's not just CSS but also image and JS files.

    I also checked the problem by having a session variable increment each time the page was loaded. For one load on one page, I got 27 counts!!! It seems that the script passed over the $_SESSION['page_count']++ 27 times! (it isn't in a loop by the way).

    I actually managed to reduce that count by using absolute path references on some images. But I don't understand why that had an impact.

    So the crux of my problem (appologies for long explanation!) - I can see the impact and I've tried very hard to determine the cause. I think I have the cause but I don't know the solution.

    As you can see I'm getting very stuck and the performance hit is pretty huge. I'd be very very grateful for any help! Thanks in advance.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, henryrhenryr.

    Originally posted by henryrhenryr
    The problem (I think). In an attempt to debug this problem, I've placed a function at the very very top of my first script which sends the REQUEST_URI reference and a timestamp to a text file. Inside some of the scripts in the page, I've placed similar functions to record some variables and arrays to the same text file. I've placed a third function at the very end of the last script on my page (ie the footer).
    Let's have a look at that function.

    Comment

    • henryrhenryr
      New Member
      • Jun 2007
      • 103

      #3
      Hi! Thanks for replying so quickly! Here are the functions.

      [PHP]
      function debug($line,$fi le,$var=NULL,$l og=TRUE) {
      $r=NULL;
      if ($log) { $hr= NULL; } else { $hr= '<hr />'; }
      $r .= '
      <div class="debug">' .$hr.'Line: <b>'.$line.'</b> in File: '.$file.'<br />The given <b>$var</b>('.gettype($v ar).') is <br />';
      if (is_array($var) && !empty($var)) {
      $r .= display_array($ var);
      }
      elseif (is_object($var )) {
      $r .= var_export($var ,TRUE);
      }
      elseif (is_string($var )) {
      $r .= '('.strlen($var ).') '.$var;
      }
      elseif (is_resource($v ar)) {
      $r .= var_export($var ,TRUE);
      }
      else {
      $r .= var_export($var ,TRUE);
      }
      $r .= $hr.'</div>';
      if ($log) {
      $debug_log= './debug.html';
      $f=fopen($debug _log,'a');
      fwrite($f,$r);
      }
      else {
      echo $r;
      }
      }

      function debug_start() {
      define('DEBUG_S TART',microtime ());
      $r = '<div class="start">D ebugging Started of page '.$_SERVER['REQUEST_URI'].' at '.date("d m Y Gis").'</div>';
      $debug_log= './debug.html';
      $f=fopen($debug _log,'a');
      fwrite($f,$r);
      }

      function debug_end() {
      $now=microtime( );
      $page_load_time =$now-DEBUG_START;
      $r = '<div class="end">Deb ugging Ended of page '.$_SERVER['REQUEST_URI'].' at '.date("d m Y Gis").', taking '.$page_load_ti me.' seconds</div>';
      $debug_log= './debug.html';
      $f=fopen($debug _log,'a');
      fwrite($f,$r);
      }
      [/PHP]

      I think they aren't causing any problems with the pages themselves. I just use it to record some variables if I'm trying to find a logical error.

      I have been continuing to investigate. I have worked out a bit more of the problem. The page that is being loaded isn't the css/js/jpeg file, it's the 404 error page that I made and directed to in my .htaccess.

      The links to the css files in particular were a bit dodgy and then the server is returning a page not found error which returns my 404 page. That page is a normal page with menu, images, lots of includes etc. This will certainly explain the length of time to load.

      The remaining problem is that I can't always be certain that a link (eg image, css, js file) will be valid (although obviously I'll try). But I don't want a mistake in the link to slow the loading of a page because it's loading the 404 error message in the background.

      Is there a way to prevent this 404 error from loading but retain the nice looking page for genuinely not found pages.

      Thanks again!
      Last edited by henryrhenryr; Jun 22 '07, 05:20 PM. Reason: mistake bb tag

      Comment

      • henryrhenryr
        New Member
        • Jun 2007
        • 103

        #4
        Update:

        I think my problem revolves around HTTP headers. I have got the Live Headers extension for firefox and I can see that my .css need to be in one file to reduce requests and I need to use some headers to force caching of particular small gifs which seem to be downloaded seperately!

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya, henryrhenryr.

          Couple of things I would check:
          • Do you have a custom 404 error page configured? Double-check your httpd.conf file and search for ErrorDocument.
          • Check your webserver's error_log file (on a *n?x system, it's probably located at /var/log/httpd/error_log). See what pages the browser was trying to access.
          • Check the HTML of the page in the browser. Look for links to nonexistent files.

          Comment

          • henryrhenryr
            New Member
            • Jun 2007
            • 103

            #6
            Thanks for reply...sorry it's been a few days!

            Do you have a custom 404 error page configured? Double-check your httpd.conf file and search for ErrorDocument.

            ----> Yes. I was using a custom 404 page. I have removed it and the load speed dramatically increased. But now I have no 404 page - is there anyway to get around this?

            Thanks!

            Comment

            Working...