Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 131,739 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,207 people online right now. Registration is fast and FREE... Join Now!




badword filter

 
Reply to this topicStart new topic

badword filter

ajaycode
post 10 Oct, 2008 - 11:18 PM
Post #1


New D.I.C Head

*
Joined: 9 Aug, 2008
Posts: 40

hi everybody
QUOTE

i wrote a bad word filter everything ok but it not shows output
it display as output is
output is The field must be at least 3 characters long: Go Back


CODE

<?php
//bad word filter program
$_POST['text']='';

$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=\"java script: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 characters:
    <a href=\"java script:history.go(-1)\">Go Back</a></center>";  
//Die();
}
// May not start with a dot
elseif (ereg("^\.",$text))
{
    echo "<center>Invalid Characters:<a href=\"java script:history.go(-1)\">Go Back</a>            </center>";  
//Die();
}
//Text cannot contain a <
elseif (ereg("\<", $text))
{
    echo "<center>Invalid Characters:<a href=\"java script:history.go(-1)\">Go Back</a>          </center>";  
//Die();
}
//Text cannot contain a [
elseif (ereg("\[", $text)) {
    echo "<center>Invalid Characters:<a href=\"java script:history.go(-1)\">Go Back</a></center>";  
//Die();
}
//Text cannot contain a ]
elseif (ereg("\]", $text))
{
    echo "<center>Invalid Characters:<a href=\"java script:history.go(-1)\">Go Back</a></center>";  
//Die();
}
//Text cannot contain "viagra"
elseif (ereg("\viagra", $text))
{
    echo "<center>Invalid Characters:<a href=\"java script:history.go(-1)\">Go Back</a></center>";  
//Die();
}
//Text cannot contain "online"
elseif (ereg("\online", $text))
{
    echo "<center>Invalid Characters:<a href=\"java script:history.go(-1)\">Go Back</a></center>";  
//Die();
}
//Text cannot contain a @
elseif (ereg("\@", $text))
{
    echo "<center>Invalid Characters:<a href=\"java script:history.go(-1)\">Go Back</a></center>";  
//Die();
}
//Text cannot contain "online"
elseif (ereg("\online", $text))
{
    echo "<center>Invalid Characters:<a href=\"java script:history.go(-1)\">Go Back</a></center>";  
//Die();
}
//Text cannot contain a %
elseif (ereg("\%", $text))
{
    echo "<center>Invalid Characters:<a href=\"java script: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
}
?>


many thanks for advance
User is offlineProfile CardPM

Go to the top of the page

DilutedImage
post 11 Oct, 2008 - 01:48 AM
Post #2


D.I.C Addict

Group Icon
Joined: 20 Nov, 2006
Posts: 642



Thanked 6 times

Dream Kudos: 25
My Contributions


That code works. Just get rid of that first line $_POST['text']=''; to check the user's input. And to make the back link work you just need to get rid of the space in java script:history.go(-1).

So, what's not working?
User is offlineProfile CardPM

Go to the top of the page

ajaycode
post 11 Oct, 2008 - 02:19 AM
Post #3


New D.I.C Head

*
Joined: 9 Aug, 2008
Posts: 40

iam entering inpput morethan three characters but its display msg like
QUOTE

The field must be at least 3 characters long: Go Back


thanks for reponse
pls help me
User is offlineProfile CardPM

Go to the top of the page

Moonbat
post 11 Oct, 2008 - 08:40 AM
Post #4


D.I.C Regular

Group Icon
Joined: 30 Jun, 2008
Posts: 373



Thanked 22 times

Dream Kudos: 600
My Contributions


QUOTE(ajaycode @ 11 Oct, 2008 - 03:19 AM) *

iam entering inpput morethan three characters but its display msg like
QUOTE

The field must be at least 3 characters long: Go Back


thanks for reponse
pls help me

Look at this bit of code:

php
$_POST['text']='';

$text = $_POST['text'];


You are pretty much erasing the user's submitted POST data by making $_POST['text'] equal to nothing. Then you are assigning that 'nothing' to $text.

So when the the if-else statement to check for string length runs, the length returns zero, because the $text variable contains nothing.

To remedy the problem, take out this line:

php
$_POST['text']='';

User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 11:01AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month