| | |
Using preg_match to block spam URLs in PHP forms
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jun 2008
Posts: 1
Reputation:
Solved Threads: 0
Hello
I have a PHP website form that gets filled out with spam - mostly URLs. To try and stop this I have inserted the following code into my form:
However, I have another field in the form that asks the user what their website address is. Of course when they fill that out, the above code blocks it meaning that when they click on 'Send' it tells them that "no URLs are allowed", so they can't submit the form. If I take out the line:
if (preg_match("/http/i", "$website")) {echo "$SpamErrorMessage"; exit();}
the form works - though the person filling it out would have no way of knowing that of course - but on the thank you page gives an error message.
I'm not sure how to escape the above code about the website. If it wasn't for the fact I ask them for their own URL the code would work fine to stop spammers!
Any advice hugely appreciated!
Thanks.
Tig
I have a PHP website form that gets filled out with spam - mostly URLs. To try and stop this I have inserted the following code into my form:
php Syntax (Toggle Plain Text)
$SpamErrorMessage = "No website URLs permitted"; if (preg_match("/http/i", "$telephone")) {echo "$SpamErrorMessage"; exit();} if (preg_match("/http/i", "$website")) {echo "$SpamErrorMessage"; exit();} if (preg_match("/http/i", "$email")) {echo "$SpamErrorMessage"; exit();} /* If e-mail is not valid show error message */ if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) { show_error("E-mail address not valid"); } /* If URL is not valid set $website to empty */ if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website)) { $website = ''; }
if (preg_match("/http/i", "$website")) {echo "$SpamErrorMessage"; exit();}
the form works - though the person filling it out would have no way of knowing that of course - but on the thank you page gives an error message.
I'm not sure how to escape the above code about the website. If it wasn't for the fact I ask them for their own URL the code would work fine to stop spammers!
Any advice hugely appreciated!
Thanks.
Tig
Last edited by peter_budo; Jun 16th, 2008 at 8:55 am. Reason: Code tag correction, closing tag is [code] not <code>
![]() |
Other Threads in the PHP Forum
- Previous Thread: how to delete extra commas on csv file with php
- Next Thread: PHP nested While loop
| Thread Tools | Search this Thread |
# 5.2.10 access action address alexa apache api array auto autoincrement broken cakephp checkbox class classes clean clients cms code cron curl database date dehasher destroy directory dissertation domain dropdown dynamic echo$_get[x]changingitintovariable... email encode error errorlog fairness fatalerror file folder form function functions google href htaccess html image include indentedsubcategory ip javascript joomla legislation limit link load local login masterthesis memberships menu multiple multipletables mysql mysqlquery newsletters oop open passwords paypal pdf persist php popup provider query radio random script search secure server sessions simple sockets source space spam sql system table tutorial upload url user variable voteup web youtube





