Link to home
Start Free TrialLog in
Avatar of scotthorton
scotthorton

asked on

php web cache routine not working quite right

I have some php code, that was developed with help from experts here.  The intent of the code was to allow me to use php includes in my web pages to combine common blocks of code which might change from time to time.  If I change the individual block/module of HTML (include file), the affecetd source files will auto-adjust and pickup the new HTML.  The code is supposed to build an exploded (post include) of itself in a directory (./cache).  If the user requests a file on my website, the cache is checked against the dependent modules and if no module is newer, the file is served from the cache.   The idea was to reduce the overhead of rebuilding the files with every access.

So, all this works great, most of the time.  Problem is occasionally I get the wrong file built with the right filename.  i.e. cache/example1.php will end up containing the actual HTML for sample1.php (some different file).  

Also, another issue, and perhaps related, when I look in my cache folder, I get a lot of files that should not be there at all.  It appears to be limited to files I have linked on teh pages, like pdf files (e.g "myfile1.pdf") .  The pdf should not go into the cache folder at all.  But, the cache will get apparently random entries that may relate to those linked files.  They will have filenames like "myfile1.pdf.php".

I don't know what casues the erroneous behavior, I was hoping you guys could help.  I've pasted a sample code sections from one of my source php files below:

This is running on an Apache server FWIW.


########### HEADER ##############
<?php        
// $reqfilename = basename($_SERVER['REQUEST_URI'], ".php");

// Uncomment below to make cache names be unique and allow dup file names across dir's
// $reqfilename = md5($_SERVER['SCRIPT_FILENAME']);
$reqfilename = basename($_SERVER['REQUEST_URI'], ".php");

$sendfile = $_SERVER['SCRIPT_FILENAME'];

$cachefile = "cache/".$reqfilename.".php";

$dependent1 = "includes/body/member.php";
$dependent2 = "includes/navbar.php";

// Serve from the cache if it is the same age or younger than the last 
// modification time of the included file (includes/$reqfilename)

  if (file_exists($cachefile) 
	&& (filemtime($sendfile) < filemtime($cachefile))
	&& (filemtime($dependent1) < filemtime($cachefile))
	&& (filemtime($dependent2) < filemtime($cachefile))
	) {  

    include($cachefile);
    echo "<!-- RequestName: $sendfile -->\n";
    echo "<!-- CacheName: $cachefile -->\n";
    echo "<!-- Cached ".date('F d Y H:i', filemtime($cachefile))."-->\n";
    exit;        
  }

//else
// start the output buffer adn "real" HTML       
ob_start(); 
?>

############### typical includes ####################
<?php include("includes/navbar.php"); ?> 	    
<?php include("includes/body/member.php"); ?>


#####################3 footer #######################3
<?php    
    // PHP, open the cache file for writing
    $fp = fopen($cachefile, 'w');

    // save the contents of output buffer to the file        
    fwrite($fp, ob_get_contents());

    // close the file        
    fclose($fp);

    // Send the output to the browser        
    ob_end_flush();
?>

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Honestly, unless you can show that the dynamic scripts are so slow that the web site suffers performance problems, I would just abandon the whole cache design pattern.  It's really only useful when it is implemented at the level of an opcode cache, or when the scripts that build the pages take very long times to run.

What was the reason for this cache scheme in the first place?  Did you have benchmarks that showed the site was suffering bad performance from too much traffic?
Avatar of scotthorton
scotthorton

ASKER

Hi Ray:

The website is running on a pretty "low power" machine.  Its a dedicated server with an unmetered connection that's grandfathered on price.  SO we dont' want to upgrade it.

But the primary impetus wasn't so much an immediate hit issue as it was a long term thing.  These web pages get edited relatively infrequently.  So what I envisioned was a method to "auto build" a new page any time a module was edited.  Then, for the next several weeks, or even months, the "static" version of the page woudl be served removing any load at all for any PHP includes.

My second concern was one of browser caching.  And I may be incorrect on my perception of the bhavior.  If I edit a php include module, the code caused the "static" (cache) file to regenerate.  So that static file got a new tiemstamp.  But if I did not use the cache sheme, if I edit a php include module, the target file (e.g. index.php) woudl not get a new timestamp.  I figured a browser woudl not see the the target file as having changed and so would not refresh it's copy.  It is very important that the browser does not use a cached copy if I change one of the modules of code (php includes).

So, those were my reasons.  Overall this works great most of the time.  But several times lately, when I've made changes to "modules" of code that are included on several top level php files, the cache file generated for each respective top level php will be the same file (same content for every file, which is incorrect for all but one, but all have their correct file name).  So when a visitor goes to look at the various files on the website, many will return the same content.

There are no duplicate file names at the top level.  There are some duplicate names on the includes.  For example "results.php" may have an include in it for "includes/body/results.php".  But I don't think that's the problem.  Then again, I don't know what's causing the issue so I mention it for completeness.

It is as if they are all using the same variable/reference and when I get simultaneous hits on the site, the incorrect source code gets stuffed into the cache using the requested file name.

Thanks,
Scott




PHP includes are such low-workload things, that I think you may be making too much of them.  Have you got any benchmarks showing that your pages are meaningfully faster when served from the static files?  If not, you probably want to make up those benchmarks.  If you can't measure it, you can't improve it (as they way).

Browser caching is a different beast.  Your meta tags and HTTP headers influence this.  I have had success preventing browser caching with this in the <head> of the document.

<META HTTP-EQUIV="Expires" content="Mon, 01 Jan 1990 01:00:00 GMT" />
<META HTTP-EQUIV="Pragma" content="no-cache">

It may or may not make sense for your setup, but I have done something like this for a content-dense site in the past.  I don't do it any more, but that's beside the point.

To get a new version of the file, you just delete the static ".htm" version.  I'm not sure about the paths to the scripts - you might want to tinker around with the code at lines 8-11
<?php // RAY_php_to_html.php
error_reporting(E_ALL);

// DEMONSTRATE HOW TO DYNAMICALLY CREATE A STATIC HTML PAGE
// THE SCRIPT WILL PRODUCE THE HTML PAGE IF IT IS PRESENT IN THE DIRECTORY
// IF NOT, THE SCRIPT WILL RUN THE PHP PAGE AND PHP WILL CREATE THE HTML PAGE

// IDENTIFY THE STATIC VERSION OF THIS PAGE http://php.net/manual/en/language.constants.predefined.php
$filename = __FILE__;
$script   = basename($filename, '.php');
$static   = $script . '.htm';

// SEND THE STATIC VERSION IF IT EXISTS
if (file_exists($static))
{
    echo file_get_contents($static);
    die();
}

// START THE OUTPUT BUFFER
ob_start();

// CREATE SOME DYNAMIC CONTENT
echo date('r');

// ACCESS THE OUTPUT BUFFER, WRITE THE HTML FILE
$htm = ob_get_contents();
file_put_contents($static, $htm);

// THE BUFFERS ARE AUTOMATICALLY FLUSHED AT SCRIPT END, SO NO MORE CODE IS NEEDED

Open in new window

Ray:

One of the main target objectives with this setup was to allow me to edit one of my "php include" files and any of the affected web pages would auto-update.  That way I wouldn't have to go address every affected file evey time I made a change.  Is there any way to include that behavior.  That part was working the "old" way.

Meanwhile, I tried the code above.  I am missing something.  I copied an existing php file and changed the "header" to the code above, I deleted the php "footer" that I had.  When I try to access it, the browser produces no output, and I do not get a html page created.

A live copy of it is at:
www.msxc.com/testing.php

The file text is below, in it's entirety:



<?php // RAY_php_to_html.php
error_reporting(E_ALL);

// DEMONSTRATE HOW TO DYNAMICALLY CREATE A STATIC HTML PAGE
// THE SCRIPT WILL PRODUCE THE HTML PAGE IF IT IS PRESENT IN THE DIRECTORY
// IF NOT, THE SCRIPT WILL RUN THE PHP PAGE AND PHP WILL CREATE THE HTML PAGE

// IDENTIFY THE STATIC VERSION OF THIS PAGE http://php.net/manual/en/language.constants.predefined.php
$filename = __FILE__;
$script   = basename($filename, '.php');
$static   = $script . '.htm';

// SEND THE STATIC VERSION IF IT EXISTS
if (file_exists($static))
{
    echo file_get_contents($static);
    die();
}

// START THE OUTPUT BUFFER
ob_start();

// CREATE SOME DYNAMIC CONTENT
echo date('r');

// ACCESS THE OUTPUT BUFFER, WRITE THE HTML FILE
$htm = ob_get_contents();
file_put_contents($static, $htm);

// THE BUFFERS ARE AUTOMATICALLY FLUSHED AT SCRIPT END, SO NO MORE CODE IS NEEDED
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>MSXC Links | Mid-South Cross Country Racing</title>
        <!-- Javascript - DO NOT CHANGE ORDER  -->
        <link href="default.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="scripts/cycle/jquery.min.js"></script>
        <script type="text/javascript" src="scripts/cycle/cycle.js"></script>
        <script type="text/javascript" src="scripts/logo-array-setup.js"></script>
        <script type="text/javascript" src="scripts/logo-rotation.js"></script>
        <script type="text/javascript" src="scripts/hoverfixonly.js"></script>
</head>

<body>
<div class="wrap">
  <!-- HTML Includes - clear cache by deleting cache/* -->
  <?php include("includes/header.php"); ?>          
  <div class="banner1"> </div>
  <div class="ad-boxes"> </div>
  <?php include("includes/attentionbutton.php"); ?>         
  <?php include("includes/navbar.php"); ?>          

  <?php include("includes/body/underconst.php"); ?>

  <div class="ad-boxes2"></div>
  <?php include("includes/footer.php"); ?>
</div>

</body>
</html>

Open in new window

If it helps, a working version using the original php code is at http://www.msxc.com/links.php
Edit:  I see I am getting the date.  So I'm presuming all my code would have to be built via the echo?  How would all my include files work then?

I wish there were a way to fix the old code.  It's working great in that regard (includes causing auto-updates).


Looks like ob_start() and ob_flush() are causing the issues to my laymen view.  The filenames are correct, only the respective content gets hosed osccasionally.  I have no idea why the pdf links are getting created as a-pdf-filename.pdf.php though.

Here is the code to a (mostly) working page using the original code:


<?php
// $reqfilename = basename($_SERVER['REQUEST_URI'], ".php");

// Uncomment below to make cache names be unique and allow dup file names across dir's
// $reqfilename = md5($_SERVER['SCRIPT_FILENAME']);
$reqfilename = basename($_SERVER['REQUEST_URI'], ".php");

$sendfile = $_SERVER['SCRIPT_FILENAME'];

$cachefile = "cache/".$reqfilename.".php";
//$cachefile = "cache/test.php";

// Serve from the cache if it is the same age or younger than the last 
// modification time of the included file (includes/$reqfilename)

  if (file_exists($cachefile) && (filemtime($sendfile) < filemtime($cachefile))) {  

    include($cachefile);
    echo "<!-- RequestName: $sendfile -->\n";
    echo "<!-- CacheName: $cachefile -->\n";
    echo "<!-- Cached ".date('F d Y H:i', filemtime($cachefile))."-->\n";
    exit;        
  }

//else
// start the output buffer adn "real" HTML       
ob_start(); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>MSXC Links | Mid-South Cross Country Racing</title>
        <!-- Javascript - DO NOT CHANGE ORDER  -->
        <link href="default.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="scripts/cycle/jquery.min.js"></script>
        <script type="text/javascript" src="scripts/cycle/cycle.js"></script>
        <script type="text/javascript" src="scripts/logo-array-setup.js"></script>
        <script type="text/javascript" src="scripts/logo-rotation.js"></script>
        <script type="text/javascript" src="scripts/hoverfixonly.js"></script>
</head>

<body>
<div class="wrap">
  <!-- HTML Includes - clear cache by deleting cache/* -->
  <?php include("includes/header.php"); ?>          
  <div class="banner1"> </div>
  <div class="ad-boxes"> </div>
  <?php include("includes/attentionbutton.php"); ?>         
  <?php include("includes/navbar.php"); ?>          

  <?php include("includes/body/underconst.php"); ?>

  <div class="ad-boxes2"></div>
  <?php include("includes/footer.php"); ?>
</div>

</body>
</html>

<?php    
    // PHP, open the cache file for writing
    $fp = fopen($cachefile, 'w');

    // save the contents of output buffer to the file        
    fwrite($fp, ob_get_contents());

    // close the file        
    fclose($fp);

    // Send the output to the browser        
    ob_end_flush();
?>

Open in new window

In my cache today I have

logo-rotation.js.php

if you view that file:
http://www.msxc.com/cache/logo-rotation.js.php

It has the contents of:
http://www.msxc.com/fliers.php

For some reason it seems that whenever this bogus file gets built, it always has the contents of fliers.php.   No idea why it picks that file to choke on.

Unless something in that file is casuing it.

That file is below:


<?php
$reqfilename = basename($_SERVER['REQUEST_URI'], ".php");

$sendfile = $_SERVER['SCRIPT_FILENAME'];

$cachefile = "cache/".$reqfilename.".php";
$dependent1 = "includes/body/fliers.php";

// Serve from the cache if it is the same age or younger than the last 
// modification time of the included file (includes/$reqfilename)

  if (file_exists($cachefile) 
        && (filemtime($sendfile) < filemtime($cachefile))
        && (filemtime($dependent1) < filemtime($cachefile))
) {  

    include($cachefile);
    echo "<!-- RequestName: $sendfile -->\n";
    echo "<!-- CacheName: $cachefile -->\n";
    echo "<!-- Cached ".date('F d Y H:i', filemtime($cachefile))."-->\n";
    exit;        
  }

//else
// start the output buffer adn "real" HTML       
ob_start(); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>MSXC | Mid-South Cross Country Racing</title>
        <!-- Javascript - DO NOT CHANGE ORDER  -->
        <link href="default.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="scripts/cycle/jquery.min.js"></script>
        <script type="text/javascript" src="scripts/cycle/cycle.js"></script>
        <script type="text/javascript" src="scripts/logo-array-setup.js"></script>
        <script type="text/javascript" src="scripts/logo-rotation.js"></script>
        <script type="text/javascript" src="scripts/hoverfixonly.js"></script>
</head>

<body>
<div class="wrap">
  <!-- HTML Includes - clear cache by deleting cache/* -->
  <?php include("includes/header.php"); ?>          
  <div class="banner1"> </div>
  <div class="ad-boxes"> </div>
  <?php include("includes/attentionbutton.php"); ?>         
  <?php include("includes/navbar.php"); ?>          

  <?php include("includes/body/fliers.php"); ?>

  <div class="ad-boxes2"></div>
  <?php include("includes/footer.php"); ?>
</div>

</body>
</html>

<?php    
    // PHP, open the cache file for writing
    $fp = fopen($cachefile, 'w');

    // save the contents of output buffer to the file        
    fwrite($fp, ob_get_contents());

    // close the file        
    fclose($fp);

    // Send the output to the browser        
    ob_end_flush();
?>

Open in new window

Not really sure where you are with this project now, and it seems to have morphed from a question into a debugging exercise.  Please let me recommend a book for you that will help you get a foundation in PHP and MySQL.  It has great examples and a downloadable code library that you can copy and modify for your own use.
http://www.sitepoint.com/books/phpmysql4/

Sitepoint also has some really good books on HTML, and there are good tutorials on the PHP.net site
http://us3.php.net/tut.php
http://us3.php.net/manual/en/tutorial.php

Going forward, I am going to recommend that you get a professional developer involved to help you get out of the woods here.  When things start happening like files appearing in the wrong directories, something is just plain wrong with the design.  And when I saw this construct (from code snippet at ID:33692952) I became concerned for you.  In a sensible design, here would never be a place where you would be running PHP scripts after you have sent the closing HTML tag to the browser.

If you still want to try to do this yourself, go back to my code example at ID:33691650.  Look at line 24.  Create your entire dynamic web page there.  That is all you have to do.  Then when you have updated anything that changes the static version of your pages, just delete the static version of the page!  PHP will do the rest on the next page load.  It will serve the dynamic version once and store the static version for all subsequent later use, until you delete the static version.  And on the next page load it will create a new static version from the then-current dynamic components.  You don't need any of that conditional logic about file time on the cache files.  In fact, that may be part of what is wrong today.  But rather than try to debug it, I recommend you discard it and start over with a design that works.

Best of luck with your project, ~Ray
</body>
</html>

<?php    
    // PHP, open the cache file for writing
    $fp = fopen($cachefile, 'w');

Open in new window

> Look at line 24.  Create your entire dynamic web page there.  That is all you have to do

Ray.  I understand.  If there is something inherintly wrong with the design, I have no desire to work on it.  The code as is was provided by a "expert".  Doesn't mean it's good as you see an obvious issue.

So, regarding your code

> Look at line 24.  Create your entire dynamic web page there.  That is all you have to do

How do I get the dynamic content in there.  I understand the echo that you gave as a sample, but how do I get th ecode below "fed" into it at line 24?

Would I have to do an echo for each line?
How would the PHP includes look?

I tried a simple
echo '

all my HTML

'

but that doesn't work.  Can you help me with that last little bit to get my HTML "fed" into it at line 24?

If the PHP includes are obeyed once fed in, too, the solution would be just fine for my needs.

Thanks again.
Scott







<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>MSXC Links | Mid-South Cross Country Racing</title>
        <!-- Javascript - DO NOT CHANGE ORDER  -->
        <link href="default.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="scripts/cycle/jquery.min.js"></script>
        <script type="text/javascript" src="scripts/cycle/cycle.js"></script>
        <script type="text/javascript" src="scripts/logo-array-setup.js"></script>
        <script type="text/javascript" src="scripts/logo-rotation.js"></script>
        <script type="text/javascript" src="scripts/hoverfixonly.js"></script>
</head>

<body>
<div class="wrap">
  <!-- HTML Includes - clear cache by deleting cache/* -->
  <?php include("includes/header.php"); ?>          
  <div class="banner1"> </div>
  <div class="ad-boxes"> </div>
  <?php include("includes/attentionbutton.php"); ?>         
  <?php include("includes/navbar.php"); ?>          

  <?php include("includes/body/underconst.php"); ?>

  <div class="ad-boxes2"></div>
  <?php include("includes/footer.php"); ?>
</div>

</body>
</html>

Open in new window

Ray:

I'm trying to get your most basic code to work.  All I did to is is add the ?> to the end of it (also tried it without it as priovided).  The webpage presents the date as expected.  No .htm file is created however.  Cany you help me get that part working?

I have:

With attachment...

Live at www.msxc.com/testing.php


<?php // RAY_php_to_html.php
error_reporting(E_ALL);

// DEMONSTRATE HOW TO DYNAMICALLY CREATE A STATIC HTML PAGE
// THE SCRIPT WILL PRODUCE THE HTML PAGE IF IT IS PRESENT IN THE DIRECTORY
// IF NOT, THE SCRIPT WILL RUN THE PHP PAGE AND PHP WILL CREATE THE HTML PAGE

// IDENTIFY THE STATIC VERSION OF THIS PAGE http://php.net/manual/en/language.constants.predefined.php
$filename = __FILE__;
$script   = basename($filename, '.php');
$static   = $script . '.htm';

// SEND THE STATIC VERSION IF IT EXISTS
if (file_exists($static))
{
    echo file_get_contents($static);
    die();
}

// START THE OUTPUT BUFFER
ob_start();

// CREATE SOME DYNAMIC CONTENT
echo date('r');

// ACCESS THE OUTPUT BUFFER, WRITE THE HTML FILE
$htm = ob_get_contents();
file_put_contents($static, $htm);

// THE BUFFERS ARE AUTOMATICALLY FLUSHED AT SCRIPT END, SO NO MORE CODE IS NEEDED

Open in new window

Obviously this is untested, but hopefully you can follow the pattern.  Replace line 24 with something like this -- it is the code that generates your web page.
// LINE 24
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>MSXC Links | Mid-South Cross Country Racing</title>
        <!-- Javascript - DO NOT CHANGE ORDER  -->
        <link href="default.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="scripts/cycle/jquery.min.js"></script>
        <script type="text/javascript" src="scripts/cycle/cycle.js"></script>
        <script type="text/javascript" src="scripts/logo-array-setup.js"></script>
        <script type="text/javascript" src="scripts/logo-rotation.js"></script>
        <script type="text/javascript" src="scripts/hoverfixonly.js"></script>
</head>

<body>
<div class="wrap">
  <!-- HTML Includes - clear cache by deleting cache/* -->';
  
include("includes/header.php");

echo '<div class="banner1"> </div>
  <div class="ad-boxes"> </div>';
  
include("includes/attentionbutton.php");
include("includes/navbar.php");
include("includes/body/underconst.php");

echo '<div class="ad-boxes2"></div>';
include("includes/footer.php");
echo '</div>

</body>
</html>';

Open in new window

Ray:

Thank you that helps, I had figured out the echo part.  Mostly (didn't know what to do with the includes, do now).

But the core (simple) code stil isn't generatingthe the .htm file.  Can you tell me anything to check to see why?

Thanks,
Scott

Ray:

I've got the dynamic content generating correctly now.  All I need is to see why it is not generating the static (htm) page.  Is there a path I need to explicitly indicate?  I've got everythign loaded in the web root.

I tried setting the directory permissions to 777 just in case that was the issue, that did not work.

I added some echos to debug it, all appears to be working except the file_put_contents line.  Do you know why it would fail to write the htm file?  I also added an explicit path to see if that would help, no luck.  

Current code to try to debug it below.  Will not create the htm file.

Live link www.msxc.com/testing.php


<?php // RAY_php_to_html.php
error_reporting(E_ALL);

// DEMONSTRATE HOW TO DYNAMICALLY CREATE A STATIC HTML PAGE
// THE SCRIPT WILL PRODUCE THE HTML PAGE IF IT IS PRESENT IN THE DIRECTORY
// IF NOT, THE SCRIPT WILL RUN THE PHP PAGE AND PHP WILL CREATE THE HTML PAGE

// IDENTIFY THE STATIC VERSION OF THIS PAGE http://php.net/manual/en/language.constants.predefined.php
$filename = __FILE__;
$script   = basename($filename, '.php');
$static   = "/home/msxc/html/" . $script . '.htm';

// SEND THE STATIC VERSION IF IT EXISTS
if (file_exists($static))
{
  echo "File found";
    //echo file_get_contents($static);
    die();
}

// START THE OUTPUT BUFFER
ob_start();

echo date('r');

echo $script . "<HR>";
echo $static . "<HR>";
echo $filename . "<HR>";

// ACCESS THE OUTPUT BUFFER, WRITE THE HTML FILE
$htm = ob_get_contents();
echo "++++++++++++++++++++++++++++++<br><br>";
echo $htm . "<HR>";
file_put_contents($static, $htm);

// THE BUFFERS ARE AUTOMATICALLY FLUSHED AT SCRIPT END, SO NO MORE 

Open in new window


<sigh>

I think I figured out my problem.  file_put_contents appeard to eb from php 5.  I run a Centos server installation which folows Redhat Enterprises conservative package installations.  So, I do not have php 5.  Mine is 4.3.9.

I replaced it with the code below.  Is that OK?  Have I broken any rule or done wnaythign wrong, etc.?
// set file to write
// set file to write
$ofile = 't3.htm';
// open file
$fh = fopen($ofile, 'w') or die('Could not open file!');
// write to file
fwrite($fh, $htm) or die('Could not write to file');
// close file
fclose($fh);

Open in new window

By trial and error I found that fwrite() would not work unless I set the root directory permissions to 777.  Didn't like that much so I created a directory and set it to 755 but made it's owner apache:users.  Then it works.

I think I have it.  If I haven't done anything wrong with the file writign above.  To get ti to work I:
- pointed the static files to a folder named static
- chown apache:users on that folder, oterwise it woudl nto write to it
- added the code to check tiemstapms so the files will self check their dependent files times and regenerate somewhat automatically, like the original code.

Seems to be working.  I would be very grateful if you would take a quick look and see if I didn anythign wrong, or violatied and god practie, like the origial apparantly did.  If it looks OK, I will start converting allmy files to get this issue fixed.




<?php 
error_reporting(E_ALL);

// IDENTIFY THE STATIC VERSION OF THIS PAGE http://php.net/manual/en/language.constants.predefined.php
$filename = __FILE__;
$script   = basename($filename, '.php');
$static   = "static/" . $script . '.htm';
$dynamic  = $script . '.php';

// Identify dependent files
$dependent1 = "includes/deleteme.txt";

// SEND THE STATIC VERSION IF IT EXISTS
if (file_exists($static)
        && (filemtime($dynamic) < filemtime($static))
        && (filemtime($dependent1) < filemtime($static))
        )
        {
        echo file_get_contents($static);
        die();
}


// START THE OUTPUT BUFFER
ob_start();

echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>MSXC Links | Mid-South Cross Country Racing</title>
        <!-- Javascript - DO NOT CHANGE ORDER  -->
        <link href="default.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="scripts/cycle/jquery.min.js"></script>
        <script type="text/javascript" src="scripts/cycle/cycle.js"></script>
        <script type="text/javascript" src="scripts/logo-array-setup.js"></script>
        <script type="text/javascript" src="scripts/logo-rotation.js"></script>
        <script type="text/javascript" src="scripts/hoverfixonly.js"></script>
</head>

<body>
<div class="wrap">
  <!-- HTML Includes - clear cache by deleting cache/* -->';
  
include("includes/header.php");

echo '<div class="banner1"> </div>
  <div class="ad-boxes"> </div>';
  
include("includes/attentionbutton.php");
include("includes/navbar.php");
include("includes/body/underconst.php");

echo '<div class="ad-boxes2"></div>';
include("includes/footer.php");
echo '</div>

</body>
</html>';
 
echo "

<!-- statc file generated: " . date('r') . "  -->";


// ACCESS THE OUTPUT BUFFER, WRITE THE HTML FILE
$htm = ob_get_contents();

// version 5 php only: file_put_contents($static, $htm);
// version 4:
// set file to write
$ofile = $static; 
// open file
$fh = fopen($ofile, 'w') or die('Could not open file!');
// write to file
fwrite($fh, $htm) or die('Could not write to file');
// close file
fclose($fh);

// THE BUFFERS ARE AUTOMATICALLY FLUSHED AT SCRIPT END, SO NO MORE

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ee_auto
ee_auto

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial