Advertisement
gitlez

YA: Simple Comment System

Nov 15th, 2011
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.49 KB | None | 0 0
  1. <?php
  2. /*
  3.     Responce to Yahoo Question:
  4.     http://answers.yahoo.com/question/index?qid=20111115110533AAkaHed
  5. */
  6.  
  7. $textFile = 'savedMessages.txt'; // Rename to anything you want;
  8. $output = ''; // Holds any message you want to show the user.
  9. $messages = ''; // Holds the saved messages from the text file.
  10.  
  11. if($_SERVER['REQUEST_METHOD'] === 'POST'){
  12.     $text = trim( strip_tags($_POST['user_input'],'<a><b><i>') ); // Strips any HTML Markup from the input, but allows for tags a, b and i. Allowing for links and minor styling.
  13.     if(strlen($text) > 0){ // Makes sure there's some text input.
  14.         $saveString = "\t\t" . '<div style="margin-bottom: 8px;"><span style="font-variant: italic;font-size: 12px;">' . date("M j Y H:i:s") . '</span><br>' .
  15.                             '<span>' . $text . '</span></div>' . PHP_EOL . PHP_EOL; // Rough template of how the input will be formatted to appear in the page. PHP_EOL is a PHP constant, meaning EndOfLine character, depending on the system.
  16.         $fh = fopen($textFile,'a+') or die('Could not open file: <b>' . $textFile . '</b> to write to.'); // Open the text file to save the new string to. If PHP cannot open the file, then die(message)
  17.         fwrite($fh, $saveString) or die('Could not write new string to file.'); // Write the saveString variable to the file, or die(message);
  18.         fclose($fh) or die('Could not close file.'); // Close the file or die(message);
  19.         $output = 'Thank you, your message has been saved.';
  20.     }else{
  21.         $output = 'Invalid Input to Save';
  22.     }
  23. }
  24. if(file_exists($textFile)){ // Checks to see if the file exists.
  25.     $messages = file_get_contents($textFile);    // Gets the contents of the file
  26. }else{
  27.     $messages = '<span style="font-variant: italic;font-weight: bold;">No Saved Messages</span>'; // Default message to display when there is no Messages file.
  28. }
  29.  
  30. ?>
  31. <html>
  32.     <head>
  33.         <title>Simple Message Saving Script</title>
  34.     </head>
  35. <body>
  36.  
  37.     <div id="output">
  38.         <span><?php echo $output; ?></span>
  39.     </div>
  40.     <div id="formContainer">
  41.         <span>Allowed Tags: &lt;a&gt;,&lt;b&gt; and &lt;i&gt;.</span>
  42.         <br>
  43.         <form method="post" action="">
  44.             <textarea name="user_input" style="width: 500px;height: 300px;"></textarea>
  45.             <br>
  46.             <input type="submit" value="Save" style="position: relative;left: 450px;">
  47.         </form>
  48.     </div>
  49.     <div id="msgsContainer">
  50.         <?php echo $messages; ?>
  51.     </div>
  52. </body>
  53. </html>
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement