-
ereg? Anti-Spam help!
I am using a php form processor and would like to stop web addresses from being input into the text fields.
A couple examples of what I have in place and general syntax:
PHP Code:
if (strlen($_POST['comments']) > 255 )
{
header("Location: http://www.websitename.com/error.php");
exit;
}
if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $_POST['vis_email']))
{
header("Location: http://www.websitename.com/error.php");
exit;
}
I've tried a couple of different things (which I haven't added above) but can't seem to get the right combination of single quotes, quotes, or brackets. :)
Any help would be GREATLY appreciated!!!!!!!!!!
-
I've got this working:
PHP Code:
/*ANTISPAM WEBSITES*/
if (ereg('^www\\.[A-Za-z0-9_-]+\\.[A-Za-z0-9]+', $_POST['comments']))
{
header("Location: http://www.website.com/error.php");
exit;
}
if (ereg('^[A-Za-z0-9_-]+\\.[A-Za-z0-9]+', $_POST['comments']))
{
header("Location: http://www.website.com/error.php");
exit;
}
if (ereg('^http\\:\\/\\/www\\.[A-Za-z0-9_-]+\\.[A-Za-z0-9]+', $_POST['comments']))
{
header("Location: http://www.website.com/error.php");
exit;
}
if (ereg('^http\\:\\/\\/[A-Za-z0-9_-]+\\.[A-Za-z0-9]+', $_POST['comments']))
{
header("Location: http://www.website.com/error.php");
exit;
}
How would I shorten this?
-
PHP Code:
/*ANTISPAM WEBSITES*/
if (ereg('^www\\.[A-Za-z0-9_-]+\\.[A-Za-z0-9]+', $_POST['comments']))
$skipit=1;
if (ereg('^[A-Za-z0-9_-]+\\.[A-Za-z0-9]+', $_POST['comments']))
$skipit=1;
if (ereg('^http\\:\\/\\/www\\.[A-Za-z0-9_-]+\\.[A-Za-z0-9]+', $_POST['comments']))
$skipit=1;
if (ereg('^http\\:\\/\\/[A-Za-z0-9_-]+\\.[A-Za-z0-9]+', $_POST['comments']))
$skipit=1;
if ($skipit) {
header("Location: http://www.website.com/error.php");
exit;
}
The above shortens by using a flag if there is a match and redirecting in one place. This way if you change the error page it only happens in one place.
Note also I am using the short version of IF, where if you only execute ONE command, you dont need the brackets.
Shortening the REGEX -
PHP Code:
if (ereg('^[http\\:\\/\\/]*[www\\.]*[A-Za-z0-9_-]+\\.[A-Za-z0-9]+', $_POST['comments']))
I think this should work. Note the brackets. By putting a * after instead of + you indicate that it is an optional match.
Note, the ^ means search from the beginning of the comments field, so if the URL is not the first thing in comments, it will pass, so I am not sure you want it in there.
-
John-Marc!!!!!!!!!!!! Welcome back!
Thanks for the help - I'm trying this now. :)
Update: Your a gem - it works GREAT! Thank you so much!