Forum Moderators: coopster

Message Too Old, No Replies

detecting number of characters

         

surrealillusions

8:48 pm on Dec 20, 2007 (gmt 0)

10+ Year Member



Hi,

I have an email form on a few websites i run.

However..we've had a few (well..handful) that is obviously a 'scam' or 'spam' trying to get us to advertise or join this website or whatever. And it usually goes on for several million pages.

Is there anyway, using php, to detect the number of characters in a form field, and if it goes over the limit, it returns you back to the form with an error message above that form field.

I know theres ways using javascript..but like we all know, turn off javascript and you can write however much you like. So i'd like to try and avoid that way if possible.

thanks

:)

mooger35

8:52 pm on Dec 20, 2007 (gmt 0)

10+ Year Member



I think strlen [ca.php.net] is what you are looking for.

Example:
<?php
$str = 'abcdef';
echo strlen($str); // 6

$str = ' ab cd ';
echo strlen($str); // 7
?>

surrealillusions

10:50 pm on Dec 21, 2007 (gmt 0)

10+ Year Member



How would i put that into the email form script?

Sorry..i'm a complete noob to these sorta php type things..so you'll have to explain it in idiots language

:)

PHP_Chimp

10:54 pm on Dec 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are using $_POST['body'] for the body of the email then you could use -

if (strlen($_POST['body']) > 1000) {
// length to long do something
}
else {
// length ok
}

surrealillusions

11:10 pm on Dec 22, 2007 (gmt 0)

10+ Year Member



ok..i'm currently using a script that builds and process the form all in one file.

As it might be tricky (for me anyway) putting this into that form, I'll look into creating a seperate email processing file from scratch so i can teach myself the in's and out's of form processing and all the extra bits.

:)

I'll get back to you when i get stuck..so probably about 10 minutes ;) :p

PHP_Chimp

11:36 pm on Dec 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Creating a script that will do all of the processing for you separately from the rest is a good idea anyway.
It is a lot easier to keep track of your scripts when they are separate from all of the html and other code.
So your rewriting will help in more than one way :)