Can anyone correct the code modifications I made to Rexy's script? It wont' output a proper HTML page, and I don't really know why.
form_handler.php
CODE
<?php
//Check for input in all areas and if there isn't any, redirect.
if( (empty($_POST['name']) || empty($_POST['content'])) && isset($_SERVER['HTTP_REFERER']) ) {
header("Location: ".$_SERVER['HTTP_REFERER']);
exit();
} else if( (empty($_POST['name']) || empty($_POST['content'])) && !isset($_SERVER['HTTP_REFERER']) ) {
header("Location: ".dirname($_SERVER['PHP_SELF']));
exit();
} else if( !empty($_POST['name']) && !empty($_POST['content']) ) {
// See data_display.php for information on this bit of code.
$filesize = filesize("data.html");
//Clean up inputs
$_POST['name'] = trim($_POST['name']);
$_POST['content'] = trim($_POST['content']);
//HTML is allowed, so check for magic quotes and remove them if they're there.
if( !ini_get("magic_quotes_runtime") )
{
$_POST['name'] = stripslashes($_POST['name']);
$_POST['content'] = stripslashes($_POST['content']);
}
// Replace content not supported by the parser with HTML.
$_POST['content'] = str_replace("\n","<br />",$_POST['content']);
$_POST['content'] = str_replace("\t"," ",$_POST['content']);
$_POST['name'] = str_replace("\t"," ",$_POST['name']);
$_POST['content'] = str_replace("&","&",$_POST['content']);
$_POST['name'] = str_replace("&","&",$_POST['name']);
// Just grabbing the file contents...
$handle = fopen("data.html","r");
$readData = @fread($handle,$filesize);
fclose($handle);
// Opens the file containing the posted information as writeable, script halts on error.
$handle = fopen("data.html","w+") or die("Could not open the target file for writing or the file did not exist.");
if( $filesize > 1 ) {
$data = $_POST['name']."\t".time()."\t".$_POST['content']."&&".$readData;
}
if( $filesize < 1 ) {
$data = $_POST['name']."\t".time()."\t".$_POST['content'];
}
// Write the data and close the handle.
fwrite($handle,$data);
fclose($handle);
unset($handle);
header("Location: ".$_SERVER['HTTP_REFERER']);
}
data_handler.php
CODE
<?php
$output = null;
// Opens the file containing the posted information as readonly, script halts on error.
$handle = fopen("data.html","r") or die("Could not open the source file for reading.");
// Assigns all of the data to a variable, posts are separated by two '&' characters in the file.
// Additonally, if the filesize is blank than it will not be read and a message will be outputted instead.
$filesize = filesize("data.html");
if($filesize < 1) {
die("The source file has no content.");
}
// Read the data from the file and put it into a variable.
$data = fread($handle,$filesize);
// Split up the posts.
$posts = explode("&&",$data);
// For each post, do the following HTML and add the data from the posts to the output variable.
foreach($posts as $tempValue) {
$postData = explode("\t",$tempValue);
//HTML
$output .= <<<OUTPUT
<table border="1">
<tr>
<td>
OUTPUT;
//End HTML
$output .= $postData[0];
//HTML
$output .= <<<OUTPUT
</td>
<td>
OUTPUT;
//End HTML
$output .= date("F nS l g:m a",$postData[1]); //Look at the PHP date function for more info on displaying this
//HTML
$output .= <<<OUTPUT
</td>
</tr>
<tr>
<td colspan="2">
OUTPUT;
//End HTML
$output .= $postData[2];
//HTML
$output .= <<<OUTPUT
</td>
</tr>
</table>
<br />
OUTPUT;
//End HTML
}
// Starts output buffering and compresses the output if the server allows (to save bandwidth).
ob_start("ob_gzhandler");
echo $output;
ob_end_flush();
?>