I get a few spam responses to my web contact form and I've used these to build up a list of fields to filter on. My plan was to compare - using strpos - each field against the text field submitted by the user and, if there's a match, redirect that mail response.
For whatever reason, the code I'm trying just doesn't seem to hit a match; what am I missing?
// Query list of spam fields
$sqlSpam= "SELECT * FROM tblspamfilter";
$rstSpam= mysqli_query($db, $sqlSpam);
// Search for key in the notes field passed by the web form
while ($rowSpam=mysqli_fetch_assoc($rstSpam)) {
$Key= $rowSpam['Key'];
if (strpos($notes, $Key) === false) {
$spam=0;
} else {
$spam++;
}
}
if ($spam==0) {
// send the mail...
} else {
// not today Mr spammer
}
It worked OK as a static spam key, but noyt now that I've added teh database element. Nothing I try in my form submission field (exact match, part match etc. ) matches as a spam event and all the mail gets through. I'm quite new to php so I assume I'm missing something in the way it compares the data from the table?
Edit: I have the following fields in my spam table:
| idKey | Key |
| 1 | jnl.io |
| 2 | drive.google.com |
| 3 | casinogorilla.com |
| 4 | www.talkwithcustomer.com |
| 5 | sexy |
| 6 | profitable database of email addresses |
| 7 | My name is Roy |
| 8 | Sexy girls |
And using this code:
$spamWords = mysqli_fetch_assoc($rstSpam);
$spam = 0;
foreach ($spamWords as $spamWord) {
if (strpos($notes, $spamWord) !== false) {
$spam++;
}
}
I get a match for jnl.io, drive.google.com but not for www.talkwithcustomer.com, sexy etc. I don't see a pattern for it at all. Is there a size limit? Is strpos the wrong technique?
Working Code The following is now working; not sure exactly what fixed it, but there was an odd collation for the table which I have replaced with utf8_general_ci and this, amongst the other suggestions has got it testing OK. Many thanks for looking in.
// Check the notes field for any likely spam indicators
$spam=0;
while ($rowSpam=mysqli_fetch_assoc($rstSpam)) {
$Key= $rowSpam['Key'];
if (strpos($notes, $Key) !== false) {
$spam++;
}
}