Primero q nada gracias por tu respuesta Triby.
Precisamente no uso con base de datos porq soy novato aun, ademas busque muchos ejemplos pero la mayoria son scripts grandes y yo lo unico q quiero es un formulario con nick y texto para dejar un simple comentario, nada sofisticado.
Por eso opte por este sin db y mas adelante, cuando adquiera mas conocimientos, crear uno mejor.
Lo q no entendi es como relacionar el codigo q me dejaste con el script.
Este es el codigo q utiliza el sistema de comentarios:
Código PHP:
<?function open_file($id)
{
if(!file_exists('comments'))
mkdir('comments');
$file = 'comments/'.$id.'.txt';
$fp = @fopen($file, 'r');
if(!$fp)
{
$fp = fopen($file, 'w');
fclose($fp);
$fp = fopen($file, 'r');
}
return $fp;
}
// function to shorten long url
function shorten_long_words($str)
{
$maxwordlen = 30;
$replacement = '........';
$rpls = round(strlen($replacement)/2);
$words = explode(' ', $str);
for($i=0; $i<count($words); $i++)
{
$wordlen = strlen($words[$i]);
if( $wordlen > $maxwordlen)
{
$del = round(($wordlen - $maxwordlen)/2);
$mid = round($wordlen/2);
$start = $mid - $del - $rpls;
$end = $mid + $del + $rpls;
$first = substr($words[$i], 0, $start);
$last = substr($words[$i], $end);
$words[$i] = $first.$replacement.$last;
}
}
$str = implode(' ', $words);
return $str;
}
// stri_replace() function
// Avaliable in PHP 5 but defined as custom function for php 4 as stri_rep
function stri_replace($find,$replace,$string)
{
if(!is_array($find))
$find = array($find);
if(!is_array($replace))
{
if(!is_array($find))
$replace = array($replace);
else
{
// this will duplicate the string into an array the size of $find
$c = count($find);
$rString = $replace;
unset($replace);
for ($i = 0; $i < $c; $i++)
{
$replace[$i] = $rString;
}
}
}
foreach($find as $fKey => $fItem)
{
$between = explode(strtolower($fItem),strtolower($string));
$pos = 0;
foreach($between as $bKey => $bItem)
{
$between[$bKey] = substr($string,$pos,strlen($bItem));
$pos += strlen($bItem) + strlen($fItem);
}
$string = implode($replace[$fKey],$between);
}
return($string);
}
// VIEW COMMENTS +++++++++++++++++
function view_comments($id)
{
?>
<script type="text/javascript" language="JavaScript" src="comments_ram.js"><!--//--></script>
<div id="comments">
<script type="text/javascript" language="JavaScript">
<!--
write_comments_control(<?php echo $id ?>);
//-->
</script>
<?php
$fp = open_file($id);
$read = fread($fp, 80000);
if(!empty($read))
{
$post = split ('---', $read);
for($i=0; $i<count($post); $i++)
{
$data = split ('#', $post[$i]);
$date = $data[0];
$name = $data[1];
$email = $data[2];
$comment= $data[3];
if(!empty($email))
$name = '<a href="mailto:'.$email.'">'.$name.'</a>';
?>
<div class="item">
<div class= "name">[ 2006-12-05 ] :: <?php echo $name; ?></div>
<div class="comment"><?php echo $comment; ?></div>
</div>
<?php
}
}
else
{
echo '<div class="item">No comments posted here.</div>';
}
?>
</div>
<?php
}
// POST COMMENTS +++++++++++++++++
function post_items($id)
{
// Settings::::::::::::
$allowed = 10; // Number of entries to store
$spliter = '---'; // String that is used to split each posts from file
$date = date('Y-m-d', time()); // Todays data format
$filter_file = 'bad_words.txt'; // File that stores all words to filter
$replacement = '***'; // The text that will replace bad words if found
$name = $_POST[name];
$email = $_POST[email];
$comment = $_POST[comment];
if(empty($name) or empty($comment))
{
echo 'Name and comment can\'t be left blank';
exit();
}
// This part filters the user input ===========================================================================
// Part 1: ###### Filters url injection
// array of text to ban
$ban = Array('http://', 'www\.', '\.com', '\.net', '\.org', '\.edu', '\.tk', '\.nfo', '\.uk', '\.au', '\.us');
for($i=0; $i<count($ban); $i++)
{
$name = eregi_replace($ban[$i], '', $name);
$comment = eregi_replace($ban[$i], '', $comment);
}
// Part 2: ###### Filters special charecters and html tag escape situtation
// Filter name
$name = ereg_replace('[^A-Za-z ]', '', $name);
// Filter comment
$comment = ereg_replace('[#]', '', $comment);
$comment = eregi_replace($spliter, '', $comment);
$comment = strip_tags($comment);
$comment = addslashes($comment);
//Filter email if available
if(!empty($email))
{
$email = ereg_replace('[^A-Za-z@._]', '', $email);
}
// Part 3: ###### Seeks and filter junk long words
$comment = shorten_long_words($comment);
// Part 4: ###### Seeks and filter bad words
// The file that contains all the bad words to filter
// Put the bad words in this file, separate words with new line
// This reads the file contents and stores the result words in a array
$bad_words = file($filter_file);
// Now loops through each bad words and replace every time badword found
// Explodes each bad words in array and store it in a temporary vaiable
// And Makes case insensitive replacement of bad word with replacement
// Filtering name and comment
for($i=0; $i<sizeof($bad_words); $i++)
{
$temp = rtrim($bad_words[$i]);
$name = stri_replace ($temp, $replacement, $name);
$email = stri_replace ($temp, $replacement, $email);
$comment = stri_replace ($temp, $replacement, $comment);
}
// Note: I didn't filter email address
// End of filtering user input =====================================================================================
// Now formatting out the new entry
$new_post = $date.'#'.$name.'#'.$email.'#'.$comment.'
';
//opens the file in read mood
$file = 'comments/'.$id.'.txt';
$fp = @fopen($file, 'r');
// If file can be openned then do this
if($fp)
{
//read file
$read = fread($fp, 80000);
// if not blank file then splits to find each posts
if(!empty($read))
{
// Finds out how many time spliter found. this value+1 is the number of post stored in file
$count = substr_count($read,$spliter);
$count++; // This is number that how many post are in the file.
// Start activities to delete last entry when allowed limit touches
if($count == $allowed)
{
// Finds out the last occurance positon of spliter.
// Required to delete the last entry if it exceeds maximum allowed limit
$pos = strrpos($read, $spliter);
// Now we shall delete the string after $pos
$read = substr($read, 0, $pos);
}
// The output to write on file
$output = $new_post.$spliter.'
'.$read;
}
else // When blank file then only write the new entry in file;
{
$output = $new_post;
}
//close file;
fclose($fp);
}
// Now opens the file in write mood to save new item
$file = 'comments/'.$id.'.txt';
$fp = fopen($file, 'w');
fwrite($fp, $output, 80000);
fclose($fp);
}
// End of Functions ------------------------------------------------------------------------------------------------
$action = $_POST['action'];
if($action == 'post')
{
$id = $_POST['id'];
post_items($id);
echo '<title>Adding your comment...</title><body onload="window.close();"></body>';
}
?>
Y este es el q hice para mostrar la noticia con su caja de comentarios, q seria visor.php:
Código PHP:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Documento sin título</title>
<style type="text/css">
.style1 {
text-align: center;
font-size: xx-large;
}
</style>
</head>
<body style="background-color: #FFFFCC">
<?php include('db-cnx.php'); ?>
<table style="width: 100%" align="center">
<tr>
<td class="style1">
<?php
$not_ID = $_GET['not_ID'];
$sqlQueryNot = mysql_query("SELECT not_ID, notUser, notTitulo, notTexto FROM sn_noticias WHERE not_ID = '$not_ID'", $db_link)
or die(mysql_error);
while($rowNot = mysql_fetch_array($sqlQueryNot)){
echo $rowNot['notUser'];
echo "<h1>$rowNot[notTitulo]</h1>";
echo nl2br($rowNot['notTexto']);
}
?>
</td>
</tr>
</table>
<br>
<table style="width: 100%">
<tr>
<td>
<?php include('comments_ram.php'); ?>
<?php view_comments(1); ?>
</td>
</tr>
</table>
</body>
</html>
No se con cual de los dos relacionarlo.