拿起字符串中的网址

时间:2014-05-03 14:01:48

标签: php spam-prevention

大家好我正在尝试一种快速而肮脏的解决方案,用于停止我网站上的评论垃圾邮件发送者,尝试选择此网址" https://www.change.org/en-GB/petitions/rockstar-games-release-pc-games-for-the-linux-operating-system"在一堆文字中如此:

        if (strpos($_POST['text'], "https://www.change.org/en-GB/petitions/rockstar-games-release-games-for-linux") == true)
        {
            header("Location: /home/banned");
        }

可悲的是,这似乎不起作用,有没有好办法呢?

3 个答案:

答案 0 :(得分:1)

来自php

strpos()

Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.

Returns FALSE if the needle was not found.

它不会返回TRUE,所以就像!==FALSE

一样

答案 1 :(得分:0)

strpos返回“返回针存在位置”或“如果未找到针则返回FALSE”。因此,您应该测试它是否返回false:

if (strpos(...) === false) {
    // needle was not found
}

在你的情况下,你需要检查否定(即找到针):

if (strpos($_POST['text'], "https://...") !== false) {
    header("Location: /home/banned");
}

请注意,两个运算符(===!==)中的提取等于符号,即make comparison type strict。如果您不使用类型严格比较并且在位置0处找到针,则会得到假阴性结果(0被解释为等于false)。

答案 2 :(得分:0)

您应该使用!== false而不是== true

   if (strpos($_POST['text'], "https://www.change.org/en-GB/petitions/rockstar-games-release-games-for-linux") !== false)
    {
        header("Location: /home/banned");
        exit;
    }