Jump to content

fwrite and fopen in cache system


Philwn

Recommended Posts

I am getting a rediculous amount of errors in the log on my host saying the following but for numerous referer addresses.

[Wed Sep 21 12:15:32 2011] [error] [client 85.189.254.101] PHP Warning:  fwrite(): supplied argument is not a valid stream resource in /home/sites/myurl.co.uk/public_html/Category.php on line 122, referer: http://www.myurl.co.uk/Category/En/Floats/

it is a simple cache system i use and it appears to work as when you view source at the bottom of the page is the comment about the file being cached and retrieved. My page code:

 

<?php 
   $cat = $_GET["cat"];
   $sub_cat = $_GET["sub"];
   $language = $_GET["lang"];
$cachefile = "cache/".$cat."-".$sub_cat."-".$language.".html";


      $cachetime = 60 * 60 * 400; // 5 days


      // Serve from the cache if it is younger than $cachetime

      if (file_exists($cachefile) && (time() - $cachetime
         < filemtime($cachefile))) 
      {

         include($cachefile);


         echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))." -->";


         exit;

      }

      ob_start(); // start the output buffer
include("./includes/db_con.inc.php");
$page_title = $cat . " - " . $sub_cat;
$sql_meta = mysql_query("SELECT * FROM sub_cat WHERE name='$sub_cat'");
$row_meta = mysql_fetch_assoc($sql_meta);
$meta_desc = $row_meta['META_DESC'];
$meta_key = $row_meta['META_KEY'];
include("./includes/head.php"); 
include("./includes/header.php");
mysql_free_result($sql_meta);
  ?>

<div id="main-page-header">
<?php 
$desc_sql = mysql_query("SELECT * FROM sub_cat WHERE name='$sub_cat'");
$row_desc = mysql_fetch_assoc($desc_sql);
$description = $row_desc['description'];
echo "<img src='$img_loc/product-pages/sub-cat-head/".str_replace(" ", "-", $row_desc['name'])."-sub-cat-head.jpg' width='940' height='310' />";
mysql_free_result($desc_sql);
?>
</div>

  <div id="page-content">
  
    <div id="main-page-text">
    
    <h1><?php echo $sub_cat; ?></h1>
<h2 class="crumbs"><a href="<?php echo "$url/Products/$lang/"; ?>">Products</a> > <a href="<?php echo "$url/Category/$lang/".urlencode($cat)."/"; ?>"><?php echo $cat; ?></a> > <?php echo $sub_cat; ?></h2>
<p>
<?php echo $description; ?>
</p>

  <script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like layout="button_count" show_faces="false" width="170" font="verdana"></fb:like>

    
  <div id="related-wrap">
  
  <h2><?php echo $sub_cat; ?> Products</h2>
    
    <?php
$range_products = mysql_query("SELECT * FROM $table WHERE CATEGORY='$cat' AND SUB_CATEGORY='$sub_cat' AND display='1' ORDER BY sortID ASC");
while($row_range = mysql_fetch_array($range_products))
    { 
$prodid = str_replace("/", "-", $row_range['PRODID']);
$cat_link = urlencode($row_range['CATEGORY']);
$sub_cat_link = urlencode($row_range['SUB_CATEGORY']);
echo "<div class='related-products'>\n<a class='related-img' href='$url/Product/$lang/$cat_link/$sub_cat_link/$prodid/'>\n<img src='$img_loc/product-pages/Range-thmb/$prodid.jpg' width='120' height='120' /></a>\n";
    echo "<h5>" . $row_range['PROD_TITLE'] . "</h5>\n";
    echo "</div>\n";
}
mysql_free_result($range_products);
?>

    </div>    
    
     <div id="FAQ-wrap"><h2>Frequently Asked Questions</h2>
  <?php
  $QA_table = $lang . "_qanda";
  if ($id=="") {
      $list_QA = mysql_query("SELECT * FROM $QA_table WHERE SUB_CATEGORY='$sub_cat' AND DISPLAY='1'") or die();
  }
  else {
  $list_QA = mysql_query("SELECT * FROM $QA_table WHERE PRODID='$id' AND DISPLAY='1'") or die();
	}
  while($row_QA = mysql_fetch_array($list_QA))
{ 
  echo "<div id='FAQ-QA'>";
  echo "<h2 class='FAQ-question'>Q: ".$row_QA['QUESTION']."</h2>";
     echo "<p class='FAQ-question'>A: ".$row_QA['ANSWER']."</p></div>";
}
mysql_free_result($list_QA);
  ?> 
    <div id="FAQ-question">
        <h2 class="white">Got a question about the 
        
        <span class="got-question-product"><?php echo $sub_cat; ?></span> 
        
        </h2>  
      </div>
      
      </div>

    

    </div>


    
    <div id="totem-menu-container"><?php include("./includes/search.php") ?>
    
    <?php include("./includes/totem.php") ?>
    
    </div>
  
  </div>
  </div>
<!-- MAIN CONTENT OF PAGE BELOW HERE -->

<!-- END MAIN CONTENT OF PAGE BELOW HERE -->

  
  
  
<?php include("./footer.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(); 
?>
   
   
   

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.