Page 1 of 1

News Posting Script(s) Bug.

Posted: Wed Sep 21, 2005 11:28 am
by ste150
Hi,

I've taking scripts for a "shout box" type thing from a tutorial and customised it for my site, to be a news poster. It is just file based, and takes what ever is in the html post from and places it into a .db file.

When the news page is loaded, it takes each new line from the db file and posts it in a table on the page.

My problem is this:

Because the scripts take each new line in the DB file as a new entry, if the user creates a new line anywhere in the post, it is entered in to the file in several lines, meaning the entries get split up.

One work around I have found is to just use a <br> tag when posting the news, but for the people posting (who aren’t really very computer savvy) this is a big inconvenience.

Has anyone got any off-hand ideas?
Demo of code
User: Steve Pw: Pyramid

Posted: Wed Sep 21, 2005 11:30 am
by Charles256
i just made my learn basic HTML:-D though I am doing volunteer work so ya know..I'm allowed to be a lil men;)

Posted: Wed Sep 21, 2005 11:46 am
by feyd
well.. you could addslashes() the posted text, then stripslashes() prior to display.. that should recombine the post into a single line...

Posted: Wed Sep 21, 2005 12:07 pm
by ste150
add and strip slashes doesnt seem to work.

Ill past the source for the main parts of the script:


Plug.php: Processes Form and Writes to File

Code: Select all

<?php

// Check to see if the user has already plugged...
if($plugged == 2){
echo "<font size='1' face='verdana'><center><br><br>You can't plug more than twice in a day!
</center></font>";
exit();
}else{ $num = $plugged + 1;
setcookie("plugged","$num",time()+86400);
}

// Get required data
$filename = 'plugs.db';
$sitename = stripslashes($_POST['sitename']);

$url = $_POST['url'];

//Set date
$meh = date("l dS of F Y");

echo "<center><font size='1' face='verdana'>";

// Make what is going to be written to the file
$theplug = stripslashes("<table cellpadding=4 cellspacing=0 border=1 width=600 bordercolor=black><tr><td bgcolor=#cccccc><font size=2>News Submited By: <b>$sitename</b> -  on $meh</font></td></tr><tr><td>$url<br></td></tr></table>");

$content = "$theplug \n";

// Write it
if (is_writable($filename)) {

if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit();
}

if (fwrite($handle, $content) === FALSE) {
echo "Cannot write to file ($filename)";
exit();
}

echo "<center>Thanks $sitename! You news has been added.<br>Please wait while we redirect you to the news page....</center><meta http-equiv='refresh' content='1; URL=news.php'>";

fclose($handle);

} else {
echo "The file $filename is not writable";
}

echo "</font></center>";
?>
News.php: Reads file and displays news

Code: Select all

<?php

echo '<font face=verdana size=1>';


$lines = file("plugs.db");

$linesr = array_reverse($lines);
$pamount = '6';

$so = sizeof($linesr);

if ($so > $pamount) {
$so = $pamount;
}

foreach($linesr as $key => $val) {
$data[$key] = $val;
}

for($K = 0; $K < $so; $K++) {
echo '<b>'.$data[$K].'</a></b><br>';
}

?>

Posted: Wed Sep 21, 2005 12:10 pm
by feyd
addslashes is done before data would be stored.. stripslashes would be done on display...

Posted: Wed Sep 21, 2005 12:33 pm
by ste150
Still doesnt seem to work.... even the right way round this time :D

Where in the file would you recomend placing the stripslashes func however?

Posted: Wed Sep 21, 2005 12:42 pm
by feyd
probably at either the echo line with $data[$K] or during the foreach just prior...

personally, I'd toss it through array_map()

Code: Select all

$linesr = array_map('stripslashes',array_reverse($lines));

Posted: Wed Sep 21, 2005 12:57 pm
by ste150
Nope, still the same problem.
It messes up all the tables on the display, as the end tags arnt included when posts are spilt up...