Advertisement
gitlez

YA: Simple Content Form WC

Apr 11th, 2012
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.58 KB | None | 0 0
  1. <?php
  2. /*    In response to a Yahoo Answer's Question    */
  3. // http://pastebin.com/RjEFuzwe  Same script without comments
  4.  
  5. // session_start(); // Depending on whether or not you're using Sessions.
  6. // Using header() --> Headers cannot be sent if data has already been sent to the user, either by echo, print or php error message.
  7.  
  8. /*    Message Functions    */
  9. // Use of tabled messages based on your initial example
  10. function errorMsg($msg){
  11.     // Checks to see if the supplied msg is an array of messages.
  12.     if(is_array($msg)){
  13.         $r = '<table>';
  14.         foreach($msg as $m){
  15.             $r .= '<tr><td style="color: #900;font-weight: bold;">' . $m . '</td></tr>' . PHP_EOL;
  16.         }
  17.         return $r . '</table>';
  18.     }else{
  19.         return '<table><tr><td style="color: #900;font-weight: bold;">' . $msg . '</td></tr></table>'; // #900 is a medium Red
  20.     }
  21. }
  22. function goodMsg($msg){
  23.     return '<table><tr><td style="color: #090;font-weight: bold;">' . $msg . '</td></tr></table>'; // #090 is a medium Green
  24. }
  25.  
  26. /*    Variables    */
  27. $output = ''; // Will hold the desired output;
  28. $title = $_POST['title'];
  29. $content = $_POST['content'];
  30. $category = $_POST['category'];
  31. $form = <<<FORM
  32. <form action="./test.php" method="post" style="background-color: #000;">
  33.     <table>
  34.         <tr>
  35.             <td><font color="white"><b>Title:</b></font></td>
  36.             <td><input type="text" name="title" value="$title"></td>
  37.         </tr>
  38.         <tr>
  39.             <td><font color="white"><b>Content:</b></font></td>
  40.             <td><textarea name="content" rows="6" col="80">$content</textarea></td>
  41.         </tr>
  42.         <tr>
  43.             <td><font color="white"><b>Category</b></font></td>
  44.             <td>
  45.             <select name="category">
  46.                 <option value="General Discussion">General Discussion</option>
  47.                 <option value="Suggestions">Suggestions</option>
  48.                 <option value="Complaints">Complaints</option>
  49.                 <option value="Problem Reporting">Problem Reporting</option>
  50.             </select>
  51.             </td>
  52.         </tr>
  53.         <tr>
  54.             <td colspan="2"><input type="submit" name="postbtn" value="Post"></td>
  55.         </tr>
  56.     </table>
  57. </form>
  58. FORM;
  59.  
  60. /*    Logged In Check    */
  61.     // No Idea where $username and $userid are coming from, I'm assuming a $_SESSION or $_COOKIE variable
  62.     // If it is a Session variable, then you will need to start the session at the beginning of the script with a: session_start();
  63. $username = $_COOKIE['username'];
  64. $userid = $_COOKIE['userid'];
  65. if( !$username || !$userid){
  66.     header('Refresh: 5; url=/login/login.php'); // Header is sent before output of data.
  67.     echo errorMsg('You need to be logged in to access this page. <a href="/login/login.php" >Login</a>');
  68.     exit;
  69. }
  70.  
  71. /*    Form Checking And Processing    */
  72.  
  73. if ($_POST['postbtn']){
  74.     $currenttime = date("h:i A");
  75.     $currentdate = date("F d, Y");
  76.     $errors = Array(); // Will hold the error Messages.
  77.    
  78.     /*    Error Checking    */
  79.     if ((strlen($title) < 10) || (strlen($title) > 50)){
  80.         $errors[] = 'Title must be longer than 10 and less than 50 characters';
  81.     }
  82.     if ((strlen($content) < 50) || (strlen($content) > 1000)){
  83.         $errors[] = 'Content must be longer than 50 and less than 1000 characters';
  84.     }
  85.     if( count($errors) > 0){ // Errors Occurred
  86.         $output = errorMsg($errors) . $form;
  87.     }else{ // No Errors Occurred Continue with post.
  88.         require ("connect.php");
  89.         // Title and Content need to be cleaned from SQL Injection and other harmfull attacks.
  90.         // Although mysql_real_escape_string() is not perfect, it does offer descent protection.
  91.         // Better protection can come from Prepared Statements.
  92.         // mysql_real_escape_string() requires a connection to a mysql database, that is why I
  93.         // didn't suggest using it before on the initial variable declaration.
  94.         // Be aware that the addition of escaping can create entries longer than 50 (title) or 1000 (content).
  95.         // The database column limits should be altered to address these possibilities.
  96.         $title = mysql_real_escape_string( $title );
  97.         $content = mysql_real_escape_string( $content );
  98.         $category = mysql_real_escape_string( $category ); // Although you set these yourself, someone can alter them to be harmful.
  99.         $query = mysql_query("SELECT id FROM posts WHERE title='$title' LIMIT 1"); // I am assuming that there is a column by the name of 'id', but it should be the column with the least amount of data.
  100.         // Returning more data than you need (content), will take up more resources.
  101.         // If all you're doing is checking for the existence, then you simply need to return 1 column Item
  102.         // Limiting it to one, will result in MySQL stopping the search after it locates one, as oppose to
  103.         // continueing to search the whole table for a match.
  104.         if ($query && mysql_num_rows($query) === 0){ // Check to ensure query was successful and then check to see that there is no rows returned.
  105.             if( mysql_query("INSERT INTO posts (title, content, time, date, author, category) VALUES ('$title', '$content', '$currenttime', 'on $currentdate', '$username', '$category')") ){
  106.                 // Success, no need to double check with another query.
  107.                 header('Refresh: 5; url=./posts.php?title=' . $title);
  108.                 $output = goodMsg('Your message was posted successfully.<br>If you are not redirected to the post in 5 seconds. Click the link below.<br> <a href="">' . $title . '</a>');
  109.             }else{
  110.                 $output = errorMsg('There was an Internal Error, while attempting to save your post. Please try again in a few minutes.') . $form; // Output the form
  111.             }
  112.         }else{
  113.             $output = errorMsg('There is already a post with that Title, please select another.') . $form; // Output the form.
  114.         }
  115.         // mysql_close($conn); // Or whatever variable your connection is stored.
  116.     }
  117. }else{
  118.     $output = $form; // Default output;
  119. }
  120. echo $output; // Output the output
  121. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement