English 中文(简体)
如何使用 strpos 在链接中找到单词
原标题:How to use strpos to find words inside link
  • 时间:2012-05-22 12:36:30
  •  标签:
  • php

我将创建数据库表格badwords ,以储存以下一些不需要的单词[id,word ]。

CREATE TABLE `badwords`(
`id` int(3) NOT NULL auto_increment,
`word` text,
PRIMARY KEY (`id`),
KEY `id` (`id`))

这么说吧,我储存了以下文字:

(1,ugly)
(2,yak)

现在,我的访客可能会张贴一些链接 含有其中的一个坏字, 我愿意使用像这样的东西。

$user = "http://www.this_ugly_site.com"; // visitor post this (ugly) word within

// i m gonna try to find any of bad words stored in my table

$qry="select * from badwords where word= $user "; // how to do it (find)
$result=mysql_query($qry) or die($qry);
if(mysql_num_rows($result)== 0 ){

echo "Good URL";

}else{

while($line=mysql_fetch_array($result)){

echo "Bad URL";

}}

我不知道如何应用 strpos , 以及它是否真的很好, 或者我还能用其他方法!

或我可以使用

$qry="select * from badwords where word LIKE  %$user% ";

但它看起来不安全,因为它在您的 SQL 中嵌入了一个用户提供的价值

任何想法或帮助 如何做到这一点 谢谢

最佳回答

你问了三个不同的问题:

  1. 我如何使用 strpos () strpos ()

    它是一个本地的 PHP 函数, 需要三个参数。 我鼓励您使用和学习 < a href="http://php. net/strpos" rel = "nofollow">PHP Docs 。 不过, 这里举一个例子:

    strpos($string, $bad_word);
    
  2. 有更好的方法吗?

    可能。 但您所拥有的字串并不坏 。 strpos () 是较快的字符串函数之一。 如果您有千个 bad words 和每秒大量请求, 那么您可能想要查封 bad words 而不是每次查询 MySQL 。 但是, 不需要提前优化。 目前, 失败要提前。 也就是说, 您发现一个 bad word , break 时, 就会发现一个 < em > break

  3. 您如何在 MySQL 中逃避字符串?

    我鼓励您使用 < a href=> "http://php.net/mysqli" rel="nofollow" >MySQLi 扩展,然后mysqli->real_escape_string() 。

    例如:

    $qry = "SELECT * FROM badwords WHERE word LIKE  %" . $dbc->real_escape_string($user) . "% ";
    
问题回答

如果您坚持要这样做, 我会让 MySQL 为你做所有的工作。 您需要做的是将歌剧的顺序颠倒到您可能习惯于使用它们的方式, 在 < code> LIKE 条款中使用 :

SELECT `word`
FROM `badwords`
WHERE  <user_input>  LIKE concat( % , `word`,  % )
LIMIT 1

PHP基本代码是:

// User input
$user = "http://www.this_ugly_site.com";

// Find matching words
// Do NOT show mysql_error() or $query in a production environment!
$query = "
  SELECT `word`
  FROM `badwords`
  WHERE  ".mysql_real_escape_string($user)."  LIKE concat( % , `word`, % )
  LIMIT 1
";
$result = mysql_query($query) or die("MySQL Error: ".mysql_error()."
".$query);

// Test for a match
if (mysql_num_rows($result)) {
  $row = mysql_fetch_assoc($result);
  echo "Bad URL (matches {$row[ word ]})";
} else {
  echo "Good URL";
}

您向后重新查找您的字符串。 我不确定我是否使用 MySQL 来这样做; 相反, 我可能会把所有坏字都从桌子上拉出来, 然后搜索我的字符串, 像这样 :

<?
$user_string = "http://www.this_ugly_site.com";

$query = "SELECT word FROM badwords";
$res = mysql_query($query);

$stringOkay = true;

while ($row = mysql_fetch_assoc($res))
{
    //use stripos for case insensitive matching
    if (stripos($user_string, $row[ word ]) !== false)
    {
        //this user string contains a bad word!
        $stringOkay = false;
        break;
    }
}

if ($stringOkay) echo "Good URL";
else echo "Bad URL";

正如DaveRandom在《业务方案》中评论的那样,你将用这种搜索方法,拿出“大量”假正反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反反

例如,您可能想要阻止 yakbot 而不是 boyak 这个词来完成此目的, 您会使用用单词解析符( ) 的常规表达式, 而您会使用这样的表达式 : , 这样您也可以用“ leet speak” 拼写“ ass” 之类的“ ass”, 并使用类似 < codea( 55$) # i < code > /code > 的模式, 但是, 这会有假的负值, 因为有人可以输入 < code> a5$ 或 < codecode> a\ {/ codecode > 。 基本上来说, 这是一个“ 难题 ”, 您需要更复杂的解决方案才能获得完整覆盖/ 保护 。

$user = "http://www.this_ugly_site.com"; 
$qry="select word_column_name from badwords"; 
$badwords=mysql_query($qry);
if(badword($user,$badwords))
    echo "bad url";

function badword($user,$badwords)
{    
   foreach($badwords as $badword)
   {
     if(stristr($text,$badword))
     {
            return true;
     }
   }
   return false;
} 




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...