dpxkkhu1812 2019-06-04 10:25
浏览 73

如何使用strpos将数据库中的常见垃圾邮件字段与表单提交相匹配?

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++;
            }
        }
  • 写回答

1条回答 默认 最新

  • duangaixing1509 2019-06-04 10:39
    关注

    Example code, where you can see your approach vs. the right approach to this problem... https://3v4l.org/Yidr1

    <?php
    
    $spamWords = ['blah', 'nah', 'hah', 'spam', 'spamm'];
    
    $spamMessage = 'Here we go blah blah blah';
    
    // this is the way you do this
    $spam = 0;
    foreach ($spamWords as $spamWord) {
        if (strpos($spamMessage, $spamWord) === false) {
            echo 'This keyword aint here, previous value - ' . $spam . PHP_EOL;
            $spam = 0;
        } else {
            $spam++;
            echo 'I am spam' . PHP_EOL;
        }
    }
    
    if ($spam === 0) {
        echo 'not spam' . PHP_EOL;
    } else {
        echo 'spam' . PHP_EOL;
    }
    // well obviously, you will echo 'spam' only if lastword from your spam-words will be present
    // so you need to keep your variable
    echo 'Right approach' . PHP_EOL;
    $spam = 0;
    foreach ($spamWords as $spamWord) {
        if (strpos($spamMessage, $spamWord) !== false) {
            $spam++;
        }
    }
    // now works as expected
    if ($spam === 0) {
        echo 'not spam';
    } else {
        echo 'spam';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)