943,514 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1174
  • PHP RSS
Nov 6th, 2008
0

Bad word filter

Expand Post »
I have set up a guestbook on a flash site and the customer has asked for a bad word filter to be incorporated. I have this code for it:
PHP Syntax (Toggle Plain Text)
  1. [php]
  2. $bad_words = explode('|', 'badword1|badword2|badword3|etc|etc');
  3. foreach ($bad_words as $naughty)
  4. {
  5. $comments = eregi_replace($naughty, "#!@%*#", $comments);
  6. }
  7. [/php]

Where in the following code should this be inserted, please:
PHP Syntax (Toggle Plain Text)
  1. // Part Two - Choose what action to perform
  2. $action = $_GET['action'];
  3.  
  4. switch($action) {
  5. case 'read' :
  6. // Fetch all comments from database table
  7. $sql = 'SELECT * FROM `' . $table . '`';
  8. $allComments = mysql_query($sql, $DBConn) or die("Error in GuestBook Application: " . mysql_error());
  9. $numallComments = mysql_num_rows($allComments);
  10. // Fetch page-wise comments from database table
  11. $sql .= ' ORDER BY `time` DESC LIMIT ' . $_GET['NumLow'] . ', ' . $numComments;
  12. $fewComments = mysql_query($sql, $DBConn) or die("Error in GuestBook Application: " . mysql_error());
  13. $numfewComments = mysql_num_rows($fewComments);
  14. // Generate Output for Flash to Read
  15. print '&totalEntries=' . $numallComments . '&';
  16. print "<br>&entries=";
  17.  
  18. if($numallComments == 0) {
  19. print "No entries in the guestbook, as yet..";
  20. } else {
  21. while ($array = mysql_fetch_array($fewComments)) {
  22. $name = mysql_result($fewComments, $i, 'name');
  23. $email = mysql_result($fewComments, $i, 'email');
  24. $comments = mysql_result($fewComments, $i, 'comments');
  25. $time = mysql_result($fewComments, $i, 'time');
  26.  
  27. print '<b>Name: </b>' . $name . '<br><b>Email: </b>' . $email . '<br><b>Comments: </b>' . $comments . '<br><i>Date: ' . $time . '</i><br><br>';
  28. $i++;
  29. }
  30. }
  31. // Print this only when there aren't any more entries..
  32. if($_GET['NumLow'] > $numallComments) {
  33. print 'No More Entries!&';
  34. }
  35. break;
  36.  
  37. case 'write' :
  38. // Recieve Variables From Flash
  39. $name = ereg_replace("&", "%26", $_POST['yourname']);
  40. $email = ereg_replace("&", "%26", $_POST['youremail']);
  41. $comments = ereg_replace("&", "%26", $_POST['yourcomments']);
  42. $submit = $_POST['submit'];
  43.  
  44. // Current system date in yyyy-mm-dd format
  45. $submitted_on = date ("Y-m-d H:i:s",time());
  46.  
  47. // Check if its submitted from Flash
  48. if($submit == 'Yes'){
  49. // Insert the data into the mysql table
  50. $sql = 'INSERT INTO ' . $table .
  51. ' (`ID`,
  52. `name`,
  53. `email`,
  54. `comments`,
  55. `time`
  56. )
  57. VALUES
  58. (\'\','
  59. . '\'' . $name . '\','
  60. . '\'' . $email . '\','
  61. . '\'' . $comments . '\','
  62. . '\'' . $submitted_on . '\'
  63. )';
  64. $insert = mysql_query($sql, $DBConn) or die("Error in GuestBook Application: " . mysql_error());

Is it after the submit POST [submit] section?
Grateful for any help.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bigginge is offline Offline
4 posts
since Nov 2008
Nov 6th, 2008
0

Re: Bad word filter

I would assume you want to put this before for insert the data into the database, so withing the write part of the switch before the SQL query.
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008
Nov 6th, 2008
0

Re: Bad word filter

Thank you so much. I put it here:
PHP Syntax (Toggle Plain Text)
  1. // Print this only when there aren't any more entries..
  2. if($_GET['NumLow'] > $numallComments) {
  3. print 'No More Entries!&';
  4. }
  5. break;
  6.  
  7. case 'write' :
  8. // Recieve Variables From Flash
  9. $name = ereg_replace("&", "%26", $_POST['yourname']);
  10. $email = ereg_replace("&", "%26", $_POST['youremail']);
  11. $comments = ereg_replace("&", "%26", $_POST['yourcomments']);
  12. $submit = $_POST['submit'];
  13. $bad_words = explode('|', 'badword1|badword2|badword3|etc|etc');
  14. foreach ($bad_words as $naughty)
  15. {
  16. $comments = eregi_replace($naughty, "#!@%*#", $comments);
  17. }
  18. // Current system date in yyyy-mm-dd format
  19. $submitted_on = date ("Y-m-d H:i:s",time());
  20.  
  21. // Check if its submitted from Flash
  22. if($submit == 'Yes'){
  23. // Insert the data into the mysql table
  24. $sql = 'INSERT INTO ' . $table .
  25. ' (`ID`,
  26. `name`,
  27. `email`,
  28. `comments`,
  29. `time`
  30. )
  31. VALUES
  32. (\'\','
  33. . '\'' . $name . '\','
  34. . '\'' . $email . '\','
  35. . '\'' . $comments . '\','
  36. . '\'' . $submitted_on . '\'
  37. )';

and it worked a treat. I did alter badword1 etc. with real words. Now just need to sit down and think of the worst words I can.
Marvellous, you're a star.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bigginge is offline Offline
4 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: php validation before file upload
Next Thread in PHP Forum Timeline: Back URL function and keeping search criteria - From Natasha





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC