Código PHP:
$file_name = "messages.html";
$post_sep = "<hr/>";
$info_sep = "<br/>";
class writePosts {
function inputPost($author, $message) {
global $file_name, $post_sep, $info_sep;
$date = time();
$self = $_SERVER['PHP_SELF'];
$author = urlencode($author);
$message = urlencode($message);
$fh = fopen($file_name, "r+");
fread($fh, filesize($file_name));
fwrite($fh, $author.$info_sep.$date.$info_sep.$message.$post_sep);
fclose($fh);
echo "<script>window.location = '$self'; </script>";
}
function getPost(){
if (isset ($_POST['author']) && isset ($_POST['message'])) {
$this->inputPost($_POST[author], $_POST[message]);
}
}
}
class readPosts{
function readAll(){
global $file_name, $post_sep, $info_sep;
$fh = fopen($file_name, "r");
$all = fread($fh, filesize($file_name));
fclose($fh);
$each = explode($post_sep, $all);
$i = 0;
$counteach = count($each);
while($i < $counteach - 1){
$post_info = explode($info_sep, $each[$i]);
$this->outPost($post_info[0], $post_info[1], $post_info[2]);
$i++;
}
}
function outPost($author, $date, $message){
$date = date("m/d/y", $date);
$author = urlencode($author);
$message = urlencode($message);
echo "<div>";
echo "( $date ) <srtong>$author says:</srtong><br>";
echo "$message </div>";
}
}
$write = new writePosts;
$read = new readPosts;
$write->getPost();
Código HTML:
<html> <head> <!-- el codigo php !--> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input name="author"><br> <textarea name="message"></textarea><br> <input type="submit" value="Submit"> </form> <?php $read->readAll(); ?> </body> </html>