Javascript 如何添加过滤器以避免使用坏词?

Javascript 如何添加过滤器以避免使用坏词?,javascript,php,html,mysql,css,Javascript,Php,Html,Mysql,Css,我想添加到这个聊天系统的功能,应该避免粗话。我已尝试实现以下代码: 但它对我不起作用,也许我没有把它放在正确的位置。 在将消息放入数据库之前,坏字应该在消息之前更改为星号 有人能帮我写代码并告诉我我能做什么吗 聊天室 函数submitChat(){ 如果(form1.msg.value=''){ 警报(“输入您的消息!”); 返回; } $('#imageload').show(); var msg=form1.msg.value; var xmlhttp=new XMLHttpReques

我想添加到这个聊天系统的功能,应该避免粗话。我已尝试实现以下代码:

但它对我不起作用,也许我没有把它放在正确的位置。 在将消息放入数据库之前,坏字应该在消息之前更改为星号

有人能帮我写代码并告诉我我能做什么吗


聊天室
函数submitChat(){
如果(form1.msg.value=''){
警报(“输入您的消息!”);
返回;
}
$('#imageload').show();
var msg=form1.msg.value;
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById('chatlogs').innerHTML=xmlhttp.responseText;
$('#imageload').hide();
}
}
open('GET','insert.php?msg='+msg,true);
xmlhttp.send();
}
$(文档).ready(函数(e){
$.ajaxSetup({cache:false});
setInterval(function(){$('#chatlogs').load('logs.php');},2000);
});
函数pageScroll(){
$(“html,body”).animate({scrollTop:$(document.height()},“fast”);
}
window.onload=函数(){
setTimeout(函数(){
$(“html,body”).animate({scrollTop:$(document.height()},“fast”);
}, 4000);
};
全球帽子
以以下身份登录:
正在加载聊天日志,请稍候。。。

这是新的
insert.php
。我添加了坏字过滤器,将其切换到使用(这将阻止sql注入),并对其进行了一些保护

<?php
session_start();
$uname = $_SESSION['username'];
$msg = $_REQUEST['msg'];

$db = new mysqli("localhost", "user", "pass", "chat");
if($db->connect_errno > 0){
    die("Unable to connect to database: " . $db->connect_error);
}

//the list of words to check.
$badWordList = array(
    'test1',
    'test2'
    );

//loop over each bad word
foreach($badWordList as $k=>$bad){
    //clean up the bad word for use in a regex
    $pattern = '/\b'.preg_quote($bad).'\b/i';

    //replace each bad word
    $msg = preg_replace($pattern, str_repeat('*', strlen($bad)), $msg);
}

//call html entities to prevent html from being entered
$msg = htmlentities($msg, ENT_QUOTES);

//create a prepared statement
$stmt = $db->prepare("INSERT INTO logs (`username` , `msg`) VALUES (?,?)");

//bind the username and message
$stmt->bind_param('ss', $uname, $msg);

//run the query to insert the row
$stmt->execute();

//get all the entries from `logs`
if($result = $db->query("SELECT `username`,`msg` FROM `logs` ORDER by `id` ASC")){
    //loop over the result and echo out the chat
    while($row = $result->fetch_assoc()){
        echo "<span class='uname'>" . $row['username'] . "</span>: <span class='msg'>" . $row['msg'] . "</span><br>";
    }
} else {
    die("There was an error retrieving the chat records: " . $db->error);
}

你哪里有那些坏的单词星码?因为我根本没看到。另外,我会避免在javascript中这样做,并在php中检查它的服务器端(
insert.php
),否则它很容易被绕过
@Jonathan Kuhn如何在php中实现这一点?我应该发布insert.php脚本吗?既然您正在加载jquery,为什么要手动执行ajax请求?所有xmlhttprequest的东西本质上都是毫无意义的。@Intera请注意,php mysql扩展是非常不推荐的。请使用mysqli或PDO。另外,看一看mysql注入。我把坏词的列表放在哪里?我应该把这段代码放在哪里呢?在.open()之前,将其替换为“请帮我把坏单词”改为“badowrd1 | badowrd2 |…”,但还有更好的方法可以做到这一点,在PHP上使用应该这样做,这样就没有人可以从页面源代码中读取坏单词列表,使用preg|replace(),就像上面mario所说的,$msg ist然后$REQUEST['msg'$_请求['msg'];在insert.phpI中,您必须更深入地了解这一点,因为在消息中可能使用类似于t*ht的东西。。。。或者,在php或js中查找api或阻止所有非0-9和a-zA-ZWell:)首先非常感谢您的帮助!!但似乎有个问题。。。它说它无法连接到数据库,当然我更改了密码和用户名。我还有logs.php,它还没有使用mysqli,也许这就是问题所在?您知道为什么它可能无法连接到数据库吗?/:顺便说一句,聊天中会有mod。如果它们是单独的脚本,在其中一个脚本中使用mysql,在另一个脚本中使用mysqli不会有什么区别。不过,您确实应该将另一个脚本切换到mysqli,并准备/绑定您的查询(除非您喜欢被黑客攻击)。它是否显示错误消息?是。”无法连接到数据库:'Oops,找到了它。将
$db->connect\u errno>=0
更改为仅
。如果没有错误,它将返回
0
,因此我们要检查它是否大于零。我没有一个系统来测试这个,所以我没有测试就写了它,可能在其他地方也犯了这样的小错误。更新了代码。好的,现在没有更多的连接问题:)但是坏字不会被星星取代。。。我能做些什么来解决这个问题?
<?php
session_start();
$uname = $_SESSION['username'];
$msg = $_REQUEST['msg'];

$db = new mysqli("localhost", "user", "pass", "chat");
if($db->connect_errno > 0){
    die("Unable to connect to database: " . $db->connect_error);
}

//the list of words to check.
$badWordList = array(
    'test1',
    'test2'
    );

//loop over each bad word
foreach($badWordList as $k=>$bad){
    //clean up the bad word for use in a regex
    $pattern = '/\b'.preg_quote($bad).'\b/i';

    //replace each bad word
    $msg = preg_replace($pattern, str_repeat('*', strlen($bad)), $msg);
}

//call html entities to prevent html from being entered
$msg = htmlentities($msg, ENT_QUOTES);

//create a prepared statement
$stmt = $db->prepare("INSERT INTO logs (`username` , `msg`) VALUES (?,?)");

//bind the username and message
$stmt->bind_param('ss', $uname, $msg);

//run the query to insert the row
$stmt->execute();

//get all the entries from `logs`
if($result = $db->query("SELECT `username`,`msg` FROM `logs` ORDER by `id` ASC")){
    //loop over the result and echo out the chat
    while($row = $result->fetch_assoc()){
        echo "<span class='uname'>" . $row['username'] . "</span>: <span class='msg'>" . $row['msg'] . "</span><br>";
    }
} else {
    die("There was an error retrieving the chat records: " . $db->error);
}