Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Responce to Yahoo Question:
- http://answers.yahoo.com/question/index?qid=20111115110533AAkaHed
- */
- $textFile = 'savedMessages.txt'; // Rename to anything you want;
- $output = ''; // Holds any message you want to show the user.
- $messages = ''; // Holds the saved messages from the text file.
- if($_SERVER['REQUEST_METHOD'] === 'POST'){
- $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.
- if(strlen($text) > 0){ // Makes sure there's some text input.
- $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>' .
- '<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.
- $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)
- fwrite($fh, $saveString) or die('Could not write new string to file.'); // Write the saveString variable to the file, or die(message);
- fclose($fh) or die('Could not close file.'); // Close the file or die(message);
- $output = 'Thank you, your message has been saved.';
- }else{
- $output = 'Invalid Input to Save';
- }
- }
- if(file_exists($textFile)){ // Checks to see if the file exists.
- $messages = file_get_contents($textFile); // Gets the contents of the file
- }else{
- $messages = '<span style="font-variant: italic;font-weight: bold;">No Saved Messages</span>'; // Default message to display when there is no Messages file.
- }
- ?>
- <html>
- <head>
- <title>Simple Message Saving Script</title>
- </head>
- <body>
- <div id="output">
- <span><?php echo $output; ?></span>
- </div>
- <div id="formContainer">
- <span>Allowed Tags: <a>,<b> and <i>.</span>
- <br>
- <form method="post" action="">
- <textarea name="user_input" style="width: 500px;height: 300px;"></textarea>
- <br>
- <input type="submit" value="Save" style="position: relative;left: 450px;">
- </form>
- </div>
- <div id="msgsContainer">
- <?php echo $messages; ?>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement