PHP Project-6 : Creating a basic bad word filter in PHP

Posted: November 9, 2010 in HTML, PHP
Tags: ,

This tutorial will show you how to create a basic bad word filter in PHP.

Code:

<?php
$text = $_POST['text'];
if(isset($_POST['submit']))
{
//Must be at least 3 characters and not more than 20
//The field must be at least 3 characters long
if (strlen($text) < 3) {
echo "<center>The field must be at least 3 characters long:
<a href=\"javascript:history.go(-1)\">
Go Back</a></center>";
Die();
}
//The field cannot contain more than 20 characters
elseif (strlen($text) > 20) {
echo "<center>The field cannot contain more than 20 char:
<a href=\"javascript:history.go(-1)\">
Go Back</a></center>";
Die();
}
// May not start with a dot
elseif (ereg("^\.",$text)) {
echo "<center>Invalid Characters:
<a href=\"javascript:history.go(-1)\">
Go Back</a></center>";
Die();
}
//Text cannot contain a <
elseif (ereg("\<", $text)) {
echo "<center>Invalid Characters:
<a href=\"javascript:history.go(-1)\">
Go Back</a></center>";
Die();
}
//Text cannot contain a [
elseif (ereg("\[", $text)) {
echo "<center>Invalid Characters:
<a href=\"javascript:history.go(-1)\">
Go Back</a></center>";
Die();
}
//Text cannot contain a ]
elseif (ereg("\]", $text)) {
echo "<center>Invalid Characters:
<a href=\"javascript:history.go(-1)\">
Go Back</a></center>";
Die();
}
//Text cannot contain "viagra"
elseif (ereg("\viagra", $text)) {
echo "<center>Invalid Characters:
<a href=\"javascript:history.go(-1)\">
Go Back</a></center>";
Die();
}
//Text cannot contain "online"
elseif (ereg("\online", $text)) {
echo "<center>Invalid Characters:
<a href=\"javascript:history.go(-1)\">
Go Back</a></center>";
Die();
}
//Text cannot contain a @
elseif (ereg("\@", $text)) {
echo "<center>Invalid Characters:
<a href=\"javascript:history.go(-1)\">
Go Back</a></center>";
Die();
}
//Text cannot contain "online"
elseif (ereg("\online", $text)) {
echo "<center>Invalid Characters:
<a href=\"javascript:history.go(-1)\">
Go Back</a></center>";
Die();
}
//Text cannot contain a %
elseif (ereg("\%", $text)) {
echo "<center>Invalid Characters:
<a href=\"javascript:history.go(-1)\">
Go Back</a></center>";
Die();
}

echo $text;
}else{

?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="text"  name="text"><br>

<input type="submit" name="submit" value="demo"><br>
</form>
<?php
}
?>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s