Doing more testing I found Firefox doesn't always revalidate its cache, so if a partial page is returned Firefox will continue displaying it and won't revalidate if reloaded untill the last modified date changes. Work around is to remove cache from the cache control.

<?php
header("Cache-Control: must-revalidate");
header("Pragma: cache");

$if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);

$mtime = filemtime("./cutenews/data/comments.txt");
$gmdate_mod = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';

if ($if_modified_since == $gmdate_mod) {
header("HTTP/1.0 304 Not Modified");
die();
}
header("Last-Modified: $gmdate_mod");
?>

I am thinking it is perhaps wiser to send the Last-Modified header before reading HTTP_IF_MODIFIED_SINCE.

 <?php
header("Cache-Control: cache, must-revalidate");
header("Pragma: cache");
header("Last-Modified: $gmdate_mod");

$if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);
$mtime = filemtime("./cutenews/data/comments.txt");
$gmdate_mod = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';

if ($if_modified_since == $gmdate_mod) {
header("HTTP/1.1 304 Not Modified");
die();
}
?>

Someone told me there is a bug with IE cache and you need to add "must-revalidate" in the cache control header or IE will NOT refresh the page like Netscape/Firefox does.

Pretty easy to add page caching and it makes a world of difference to your server when hosting cutenews and to your visitors! Open your main index script, say index.php and add this the code to very top before ANYTHING else.  Point filemtime to wherever you keep comments.txt. The reason we point to comments.txt is to provide a simple means to determine when to refresh the cache copy on the client side. Personally I produce a dummy.txt file whenever a comment or news is added and point to that. But this is sufficent for most cases to get rolling.


<?php
header("Cache-Control: must-revalidate"); 
header("Pragma: cache");

$if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);

$mtime = filemtime("./cutenews/data/comments.txt");
$gmdate_mod = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';

if ($if_modified_since == $gmdate_mod) {
    header("HTTP/1.0 304 Not Modified");
    die();
}
header("Last-Modified: $gmdate_mod");
?>

Now we need a simple means to refresh the cache in case of a news post instead of a comment.  The easist way to do this is edit addnews.mdu at the very bottom where you find:

  msg("info","News added", "The news item was successfully added.</br>$unapproved_status_msg");

Add this right below this:

 touch("./data/comments.txt");

Now you have a easy and fairly decent page caching setup for cutenews that will boost performance.

Name: Latest Comments List
Author: BadDog
Updated by: FUNimations
CuteNews Compatibility: 1.3.6 - * (no incompatibility reported yet)
Description: This will list the latest x number of comments made. This doesn't take post order in account, only the posting date of all comments.
Requirements: This is a rebuild to work with php5. This version might work in lower php versions aswell. Though this is unconfirmed.
Discussion Topic: the topic
Instructions:
Make a file with the following code in it.
<div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'><?php

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Update of Georgi Avramov's latest_comments.php by BadDog
Made more campatiable for CN 1.4 and also to include
title of the news for which the comment belongs to make it
usable in a sidebar.

UPDATE AGAIN BY FUNimations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
//absolute path to cutenews
$mPath = "C:\wamp\www\cutenews";
$reverse = false; //false = newest comments first.
require_once( $mPath."/data/config.php" );
require_once( $mPath."/inc/functions.inc.php" );
//number of latest comments to show
if(!isset($comments_number)) $comments_number = 3;

//URL to where you include your news, example: http://site.com/news.php
$site_url = 'http://localhost/cutenews/';

//more help on the format: http://www.php.net/manual/en/function.date.php
$date_format = 'd M y';

//can use: {url}, {name}, {date}, {comment}, {ip}, {mail}, {newsid}, {comid}, {title}
$ctemplate = ' {title}  by {name} - {comment}
';


$latest_comments = array();

$all_comments = file("$mPath/data/comments.txt");

foreach($all_comments as $comment_line)
{
    $comment_line_arr = explode("|>|", $comment_line);
    $newsid = $comment_line_arr[0];
    $comment_arr = explode("||", $comment_line_arr[1]);
    foreach($comment_arr as $single_comment)
    {
        $single_comment_arr = explode("|",$single_comment);
        $latest_comments[$single_comment_arr[0]] = array_merge( array( $newsid ), $single_comment_arr );
       
    }
}
if($reverse)
ksort($latest_comments);
else
krsort($latest_comments);

$t=0;
foreach($latest_comments as $comment){
$output = $ctemplate;

if ($comment[2]){

$title = get_title($comment[0], $mPath); //make sure path is passed to prevent open errors
$category = get_cat($comment[0], $mPath); //make sure path is passed to prevent open errors

$output = str_replace("{url}", $site_url."?subaction=showcomments&id={newsid}&ucat={category}#{comid}", $output);
$output = str_replace("{name}", $comment[2], $output);
$output = str_replace("{title}", $title, $output);
$output = str_replace("{mail}", $comment[3], $output);
$output = str_replace("{ip}", $comment[4], $output);
$output = str_replace("{comment}", $comment[5], $output);
$output = str_replace("{date}", date($date_format, $comment[1]), $output);
$output = str_replace("{newsid}", $comment[0], $output);
$output = str_replace("{comid}", $comment[1], $output);
$output = str_replace("{category}", $category, $output);
$output = replace_comment("show", $output);

echo $output . "\n";
$t++;
}
if($t == $comments_number) break;
}

function get_title($id, $mPath){

$all_news = file("$mPath/data/news.txt");
foreach ($all_news as $news) {
$news_arr = explode("|", $news);
if ($news_arr[0] == "$id") {
$title = $news_arr[2];
}
}
$title = ereg_replace("<[^>]*>","",$title); // incase there is html tags
$title = str_stop($title, 40); // keep the title length under 40 chars, edit to taste
return $title;
}

function get_cat($id, $mPath){

$all_news = file("$mPath/data/news.txt");
foreach ($all_news as $news) {
$news_arr = explode("|", $news);
if ($news_arr[0] == "$id") {
return $news_arr[6];
}
}
return $title;
}

function str_stop($string, $max_length){
if (strlen($string) > $max_length){
$string = substr($string, 0, $max_length);
$pos = strrpos($string, " ");
if ($pos === false) {
return substr($string, 0, $max_length)."...";
}
return substr($string, 0, $pos)."...";
}else{
return $string;
}
}
unset($reverse);
?></div>

OK, fixed latest_comments.php to work decent and fixed two flaws I found in it....NOW I just need to get a title returned so i can include it in the output as it is now it only shows the name. Soooooo I am thinking that I *could* open news.txt with the article ID that is saved in comments.txt to get a title for the comment and then trim it so longgggggggggg ones don't wrap to many lines since it will be in a sidebar......

Anyone want to spare me searching the code to find the php code to read the news.txt to match the article ID please!!! I just need a quick snippet/function to open it and extract the title.


Jetski,Aug 29 2005, 03:26 AM wrote:

Search the forum.. there is a addon around.. its called latest comments or somethisng like that
[right][snapback]49163[/snapback][/right]


There is a latest_comments.php that is broken and in need of a overhaul for 1.4....I'll see if I can fix it, but if PHP regex is non C it will take me some figuring.

Name: Latest Comments List
Author: BadDog
Updated by: Flexer
CuteNews Compatibility: 1.3.6 - * (no incompatibility reported yet)
Description: This will list the latest x number of comments made. This doesn't take post order in account, only the posting date of all comments.
Requirements: This original version has some problems when used with php5. So only use this when you have php 4 or lower.
Download: get the file here
Instructions:
Make a file with the following code in it. Don't forget to change the variables at the beginning of the file.
<div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'><?php
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Update of Georgi Avramov's latest_comments.php by BadDog
Made more campatiable for CN 1.4 and also to include
title of the news for which the comment belongs to make it
usable in a sidebar.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
//absolute path to cutenews
$mPath = "C:/httpd/htdocs/cutenews";

//number of latest comments to show
if(!isset($comments_number)) $comments_number = 5;

//URL to where you include your news, example: http://site.com/news.php
$site_url = 'http://127.0.0.1/index.php';

//more help on the format: http://www.php.net/manual/en/function.date.php
$date_format = 'd M y';

//can use: {url}, {name}, {date}, {comment}, {ip}, {mail}, {newsid}, {comid}, {title}
$ctemplate = ' {title}  by {name}
';


    $latest_comments = array_fill(1, $comments_number, array('newsid'=>'0','comid'=>'0','name'=>'','mail'=>'','ip'=>'','comment'=>''));

    function check_latest_comment($comment_line, $newsid){
        global $latest_comments;
        $tmp_latest_comments = $latest_comments;
        $comment_arr = explode("|", $comment_line);
        foreach($latest_comments as $key=>$latest_comment){

            if($latest_comment['comid'] < $comment_arr[0]){

                $previous = $latest_comments[$key];
                foreach($latest_comments as $my_key=>$latest_comment_arr){
                    if($key < $my_key){
                     $current = $latest_comments[$my_key];
                     $latest_comments[$my_key] = $previous;
                     $previous = $current;
                    }
                }

                $latest_comments[$key] = array(
                                       'newsid'    => $newsid,
                                       'comid'    => $comment_arr[0],
                                       'name'    => $comment_arr[1],
                                       'mail'    => $comment_arr[2],
                                       'ip'    => $comment_arr[3],
                                       'comment'=> $comment_arr[4],
                          );
            }
        }
    }

   $all_comments = file("$mPath/data/comments.txt");
    foreach($all_comments as $comment_line)
    {

  $comment_line_arr = explode("|>|", $comment_line);
        $newsid = $comment_line_arr[0];
        $comment_arr = explode("||", $comment_line_arr[1]);
  foreach($comment_arr as $single_comment)
  {
            check_latest_comment($single_comment, $newsid);
  }
    }

    foreach($latest_comments as $comment){
  $output = $ctemplate;

          if ($comment['name']){

          $title = get_title($comment['newsid'], $mPath); //make sure path is passed to prevent open errors
 
     $output = str_replace("{url}", $site_url."?subaction=showcomments&id={newsid}#{comid}", $output);
     $output = str_replace("{name}", $comment['name'], $output);
     $output = str_replace("{title}", $title, $output);
     $output = str_replace("{mail}", $comment['mail'], $output);
     $output = str_replace("{ip}", $comment['ip'], $output);
     $output = str_replace("{comment}", $comment['comment'], $output);
     $output = str_replace("{date}", date($date_format, $comment['comid']), $output);
     $output = str_replace("{newsid}", $comment['newsid'], $output);
     $output = str_replace("{comid}", $comment['comid'], $output);
          echo $output . "\n";
          }
}

function get_title($id, $mPath){
   
       $all_news = file("$mPath/data/news.txt");
       foreach ($all_news as $news) {
       $news_arr = explode("|", $news);
    if ($news_arr[0] == "$id") {
       $title = $news_arr[2];
       }
       }
     $title = ereg_replace("<[^>]*>","",$title); // incase there is html tags
     $title = str_stop($title, 40); // keep the title length under 40 chars, edit to taste
     return $title;
}

function str_stop($string, $max_length){
     if (strlen($string) > $max_length){
         $string = substr($string, 0, $max_length);
         $pos = strrpos($string, " ");
     if ($pos === false) {
          return substr($string, 0, $max_length)."...";
        }
          return substr($string, 0, $pos)."...";
        }else{
          return $string;
        }
}
?></div>