Jump to content

Recommended Posts

Hi I have found another CMS tutorial however i am still having problems =(

 

This CMS uses a cache to reduce server load however i dont think it is saving properly and when i add a new article it comes up with the error message :Warning: Cannot modify header information - headers already sent by (output started at /home/dancdoit/public_html/admin/includes/config.php:6) in /home/dancdoit/public_html/admin/article.php on line 30.

 

Config .php only contains my MySql details to log in.

 

Here is the complete code

<?php
include 'includes/config.php';
include 'includes/dbopen.php';

/*
This is the directory where we store the cache file,
you can change it any directory you want as long as
PHP can write to it.
*/ 
$cacheDir  = dirname(__FILE__) . '/cache/';

/*
Generate the cache filename, the cache name is simply
an underscore followed by the article id. In case this
script is called without any id (i.e. we want to show
the article list ) then use index.html as the cache name
*/
if (isset($_GET['id'])) {
$cacheFile = $cacheDir . '_' . $_GET['id'] . '.html';
} else {
$cacheFile = $cacheDir . 'index.html';
}	

/* 
If we found the the cache file
read it and send to the client
*/
if (file_exists($cacheFile))
{
header("Content-Type: text/html");
readfile($cacheFile);
exit;
}

/*
If the script reaches this point, then the cache file
is not found. Now we fetch the info from the database
*/

// if no id is specified, list the available articles
if(!isset($_GET['id']))
{
$self   = $_SERVER['PHP_SELF'];

$query  = "SELECT id, title FROM cmsarticles ORDER BY id";
$result = mysql_query($query) or die('Error : ' . mysql_error()); 

$content =  '<ol>';
while($row = mysql_fetch_array($result, MYSQL_NUM))
{
	list($id, $title) = $row;
	$content .= "<li><a href=\"$self?id=$id\">$title</a></li>\r\n";
}

$content .= '</ol>';

$title   = 'Available Articles';
} else {
// get the article info from database
$query   = "SELECT title, content FROM cmsarticles WHERE id=".$_GET['id'];
$result  = mysql_query($query) or die('Error : ' . mysql_error()); 
$row     = mysql_fetch_array($result, MYSQL_ASSOC); 

$title   = $row['title'];
$content = $row['content'];
}	

include 'includes/dbclose.php';

// start output buffering ( we need the generated html
// page to create the cache file )
ob_start();
?>
<html>
<head>
<title>
<?php echo $title;?>
</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<table width="600" border="0" align="center" cellpadding="10" cellspacing="1" bgcolor="#336699">
<tr> 
  <td bgcolor="#FFFFFF">
<h3 align="center"><?php echo $title;?></h3>
   <?php echo $content;?>
  </td>
</tr>
</table>
</body>
</html>
<?php

// get the buffer
$buffer = ob_get_contents();

// end output buffering, the buffer content
// is sent to the client
ob_end_flush();

// now we create the cache file
$fp = @fopen($cacheFile, "w");
@fwrite($fp, $buffer);
@fclose($fp);
?>

 

This is line 30

header("Content-Type: text/html");

 

Thanks Sims

Link to comment
https://forums.phpfreaks.com/topic/133087-solved-more-cms-problems/
Share on other sites

When i delete the '@'s i get the error messages

 

 

Warning: fopen(/home/dancdoit/public_html/admin/cache/index.html) [function.fopen]: failed to open stream: No such file or directory in /home/dancdoit/public_html/admin/article.php on line 102

 

Warning: fwrite(): supplied argument is not a valid stream resource in /home/dancdoit/public_html/admin/article.php on line 103

 

Warning: fclose(): supplied argument is not a valid stream resource in /home/dancdoit/public_html/admin/article.php on line 104

ok, first, add a + to the w:

$fp = fopen($cacheFile, "w+");

that will create the file if id doesn't exist

 

...this caching method has no way of telling when the DB has been updated. you need to either check for a change, or just delete the cache file whenever you update the DB. Then when someone visits the page, it will recreate the cache file with the new info

Ok thanks i think that works however i want to make it that instead of just showing the link it shows the whole article but i cant find out how to do that.

 

Heres the part of the code

if(!isset($_GET['id']))
{
   $self   = $_SERVER['PHP_SELF'];

   $query  = "SELECT id, title, content FROM cmsarticles ORDER BY id";
   $result = mysql_query($query) or die('Error : ' . mysql_error()); 
   
   $content =  '<ol>';
   while($row = mysql_fetch_array($result, MYSQL_NUM))
   {
      list($id, $title) = $row;
      $content .= "<li><a href=\"$self?id=$id\">$title</a></li>\r\n";
   }
   
   $content .= '</ol>';
   
   $title   = 'Available Articles';
} else {
   // get the article info from database
   $query   = "SELECT title, content FROM cmsarticles WHERE id=".$_GET['id'];
   $result  = mysql_query($query) or die('Error : ' . mysql_error()); 
   $row     = mysql_fetch_array($result, MYSQL_ASSOC); 
   
   $title   = $row['title'];
   $content = $row['content'];
}   

include 'includes/dbclose.php';

 

How do i add to the second $content variable to include the content from the row 'content'?

ah, in that case:

 

if(!isset($_GET['id']))
{
   $self   = $_SERVER['PHP_SELF'];

   $query  = "SELECT id, title, content FROM cmsarticles ORDER BY id";
   $result = mysql_query($query) or die('Error : ' . mysql_error());
   
   $content =  '<ol>';
   while($row = mysql_fetch_array($result, MYSQL_NUM))
   {
      list($id, $title, $text) = $row;
      $content .= "<li>\r\n";
      $content .= "<a href=\"$self?id=$id\">$title</a><br />\r\n";
      $content .= "<p>$text</p>\r\n";
      $content .= "</li>\r\n";
   }
   
   $content .= '</ol>';
   
   $title   = 'Available Articles';
} else {
   // get the article info from database
   $query   = "SELECT title, content FROM cmsarticles WHERE id=".$_GET['id'];
   $result  = mysql_query($query) or die('Error : ' . mysql_error());
   $row     = mysql_fetch_array($result, MYSQL_ASSOC);
   
   $title   = $row['title'];
   $content = $row['content'];
}   

include 'includes/dbclose.php';

 

I had to use $text to hold the variable, as $content is already used. Does that send you in the right direction?

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.