php bad word filter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stawkerm0h
Forum Newbie
Posts: 7
Joined: Wed Mar 02, 2011 7:16 pm

php bad word filter

Post by stawkerm0h »

i currently have this codes and it's working........my problem is i want to filter bad word coming from the list of my database which named "bad_replace" and under it there's my tables and list of bad words named "bad_word"........what could be the codes and to where will i enter the codes to filter bad words that will be posted in my forum...because what i created is a forum...once they posted something in my forum and if it was bad word/s it will be filter and be replace by asterisk(*) or something.....hoping someone could help me......thanks(I'm using dreamweaver cs4 for my forum)

this is my create_topic.php

Code: Select all

<?php session_start(); ?>
<?php
if ((!isset($_SESSION['uid'])) || ($_GET['cid'] == "")) {
	header("Location: index.php");
	exit();
}
$cid = $_GET['cid'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Create Forum Topic</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<style type="text/css">
<!--
h2 {
	font-size: 36px;
	color: #6F0000;
}
body,td,th {
	font-size: 18px;
	color: #6F0000;
	background-attachment: fixed;
	background-image: url(assets/image2.jpg);
	background-repeat: no-repeat;
	background-position: 27px 30px;
}
-->
</style></head>

<body>

<div id="wrapper">
<center><h2>Treston Forum Boards
</h2>
</center>

<?php
echo "<p>You are logged is as ".$_SESSION['username']." &bull; <a href='logout_parse.php'>Logout</a>";
?>

<hr />
<div id="content">
<form action="create_topic_parse.php" method="post">
<p>Topic Title</p>
<input type="text" name="topic_title" size="98" maxlength="150" />
<p>Topic Content</p>
<textarea name="topic_content" rows="5" cols="75"></textarea>
<br /><br />
<input type="hidden" name="cid" value="<?php echo $cid; ?>" />
<input type="submit" name="topic_submit" value="Create Your Topic" />
</form>
</div>
</div>

</body>
</html>


this is my create_topic_parse.php

Code: Select all

<?php
session_start();
if ($_SESSION['uid'] == "") {
	header("Location: index.php");
	exit();
}
if (isset($_POST['topic_submit'])) {
	if (($_POST['topic_title'] == "") && ($_POST['topic_content'] == "")) {
		echo "You did not fill in both fields. Please return to the previous page.";
		exit();
	} else {
		include_once("connect.php");
		$cid = $_POST['cid'];
		$title = $_POST['topic_title'];
		$content = $_POST['topic_content'];
		$creator = $_SESSION['uid'];
		$sql = "INSERT INTO topics (category_id, topic_title, topic_creator, topic_date, topic_reply_date) VALUES ('".$cid."', '".$title."', '".$creator."', now(), now())";
		$res = mysql_query($sql) or die(mysql_error());
		$new_topic_id = mysql_insert_id();
		$sql2 = "INSERT INTO posts (category_id, topic_id, post_creator, post_content, post_date) VALUES ('".$cid."', '".$new_topic_id."', '".$creator."', '".$content."', now())";
		$res2 = mysql_query($sql2) or die(mysql_error());
		$sql3 = "UPDATE categories SET last_post_date=now(), last_user_posted='".$creator."' WHERE id='".$cid."' LIMIT 1";
		$res3 = mysql_query($sql3) or die(mysql_error());
		if (($res) && ($res2) && ($res3)) {
			header("Location: view_topic.php?cid=".$cid."&tid=".$new_topic_id);
		} else {
			echo "There was a problem creating your topic. Please try again.";
		}
	}
}
?>


this is my post_reply.php

Code: Select all

<?php session_start(); ?>
<?php
if ((!isset($_SESSION['uid'])) || ($_GET['cid'] == "")) {
	header("Location: index.php");
	exit();
}
$cid = $_GET['cid'];
$tid = $_GET['tid'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Forum Series - Post Forum Reply</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>

<body>

<div id="wrapper">
<h2>TimKippTutorials | Forum Tutorial Series - Part 5</h2>
<p>Posting Replies</p>

<?php
echo "<p>You are logged is as ".$_SESSION['username']." &bull; <a href='logout_parse.php'>Logout</a>";
?>

<hr />
<div id="content">
<form action="post_reply_parse.php" method="post">
<p>Reply Content</p>
<textarea name="reply_content" rows="5" cols="75"></textarea>
<br /><br />
<input type="hidden" name="cid" value="<?php echo $cid; ?>" />
<input type="hidden" name="tid" value="<?php echo $tid; ?>" />
<input type="submit" name="reply_submit" value="Post Your Reply" />
</form>
</div>
</div>

</body>
</html>

and this is my post_reply_parse.php

Code: Select all

<?php
session_start();
if ($_SESSION['uid']) {
	if (isset($_POST['reply_submit'])) {
		include_once("connect.php");
		$creator = $_SESSION['uid'];
		$cid = $_POST['cid'];
		$tid = $_POST['tid'];
		$reply_content = $_POST['reply_content'];
		$sql = "INSERT INTO posts (category_id, topic_id, post_creator, post_content, post_date) VALUES ('".$cid."', '".$tid."', '".$creator."', '".$reply_content."', now())";
		$res = mysql_query($sql) or die(mysql_error());
		$sql2 = "UPDATE categories SET last_post_date=now(), last_user_posted='".$creator."' WHERE id='".$cid."' LIMIT 1";
		$res2 = mysql_query($sql2) or die(mysql_error());
		$sql3 = "UPDATE topics SET topic_reply_date=now(), topic_last_user='".$creator."' WHERE id='".$tid."' LIMIT 1";
		$res3 = mysql_query($sql3) or die(mysql_error());
		
		// Email Sending
		
		if (($res) && ($res2) && ($res3)) {
			echo "<p>Your reply has been successfully posted. <a href='view_topic.php?cid=".$cid."&tid=".$tid."'>Click here to return to the topic.</a></p>";
		} else {
			echo "<p>There was a problem posting your reply. Try again later.</p>";
		}
		
	} else {
		exit();
	}
} else {
	exit();
}
?>
stawkerm0h
Forum Newbie
Posts: 7
Joined: Wed Mar 02, 2011 7:16 pm

Re: php bad word filter

Post by stawkerm0h »

please help......i really need it badly..... =(
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: php bad word filter

Post by Jonah Bron »

Code: Select all

$cid = $_POST['cid'];
$title = $_POST['topic_title'];
$content = $_POST['topic_content'];
$creator = $_SESSION['uid'];
So this is the text that needs to be filtered? You need to clean these values first (replace the above with the following).

Code: Select all

$cid = mysql_real_escape_string($_POST['cid']);
$title = mysql_real_escape_string($_POST['topic_title']);
$content = mysql_real_escape_string($_POST['topic_content']);
$creator = mysql_real_escape_string($_SESSION['uid']);
That's better. Now, we read the bad words from the database (put this after the above).

Code: Select all

$bad_words = array();
$results = mysql_query('SELECT word FROM bad_word');
while ($row = mysql_fetch_object($results)) {
    $bad_words[] = $row->word;
}
Good, that's done. Finally, we remove the offending words from the content/title (put this after the above).

Code: Select all

$content = str_replace($bad_words, '!@%$#%@', $content);
$title = str_replace($bad_words, '!@%$#%@', $title);
http://php.net/mysql-real-escape-string
http://php.net/str-replace
stawkerm0h
Forum Newbie
Posts: 7
Joined: Wed Mar 02, 2011 7:16 pm

Re: php bad word filter

Post by stawkerm0h »

can you show me the total coding of all of it...i'm still confusing.....or where will i change?? is it in:
-create_topic.php
-create_topic_parse.php
-post_reply.php
or in post_reply_parse.php??
which one or all of them that has $cid, $title, $content and $creator??
stawkerm0h
Forum Newbie
Posts: 7
Joined: Wed Mar 02, 2011 7:16 pm

Re: php bad word filter

Post by stawkerm0h »

jonah bron...
i tried what you posted and it's not working...
it did not call and filter any bad words coming from my database named bad_replace and under it there's my table named bad_words...
please help A.S.A.P
badly needed =(
stawkerm0h
Forum Newbie
Posts: 7
Joined: Wed Mar 02, 2011 7:16 pm

Re: php bad word filter

Post by stawkerm0h »

this is my database
Image

bad word list
Image
stawkerm0h
Forum Newbie
Posts: 7
Joined: Wed Mar 02, 2011 7:16 pm

Re: php bad word filter

Post by stawkerm0h »

guys, please help..........
Post Reply