dongnao6858 2016-03-27 20:45
浏览 42

PHP聊天审查

I have a chat on my website, but it doesn't have accounts, so cursing is rampant. I use PHP to write all of my data to user.txt. Is there a simple filter for mindless cursing in php? Because this js I use doesn't seem to work too well:

$(function() {
  $("input:submit[name='submit']").on("click",function() {
    var name = $("input:text[name='name']").val();
    var message = $("input:text[name='field2']").val();
    var badwords = ["dingbat", "poopy"];

    /* do this */ 

    if($.inArray(name, badwords) !==-1 || $.inArray(message, badwords) !==-1) {
      alert("Your Message Contains Bad Words, Please Remove Them Before Proceeding");
      return false;
    }
  });
});

Here is my full code (minus txt.php which just displays user.txt)

@import url(https://fonts.googleapis.com/css?family=Lato:400,100,300);
 #blu {
 background: #F0FAFF;
 padding: 3;
width: 310;
        }
        b, i, p {
 -webkit-transition: all 0.5s ease;
    font-size: 16px;
    font-size: 1rem;
    line-height: 23px;
    line-height: 1.4375rem;
    font-weight: 400;
    font-style: normal;
    font-family:"Lato";
}
iframe{border: 3px solid white;}

button,input, #rcp_submit, #rcp_update_card {
    display: inline-block;
    font-family: "Lato", sans-serif;
    font-weight: 500;
    outline: 0;
    border-radius: 0;
    color: black;
    background: #FFFFFF;
    white-space: nowrap;
    padding: 9px 16px !important;
    line-height: 1.4;
    border: 0;
    color:black;
    position: relative;
    -webkit-transition: 0.1s;
    transition: 0.1s;
}
,!--this file is called c5.php-->
<?php
if(isset($_POST['field1']) && isset($_POST['field2'])) {
    $data = $_POST['field1'] . '-' . $_POST['field2'] . "<br>";
    $ret = file_put_contents('user.txt', $data, FILE_APPEND | LOCK_EX);
    if($ret === false) {
        die('There was an error writing this file');
    }
    else {
    echo "$ret bytes written to file<a href ='javascript:history.go(-1)'>go back</a>";
    }
}
else {
   die('no post data to process');
}
?>
<html>
<body onload="reloadIFrame();">
<script>
window.setInterval("reloadIFrame();", 3000);
function reloadIFrame() {
 document.getElementById("myIframe").src="txt.php";
}
</script>
<div id ="blu">
<table>
<p>Chat about your code</p>
<td>
<iframe src ="txt.php" id ="myIframe"></iframe><br/>
<button onclick="reloadIFrame();">Refresh feed</button>
<form action="c5.php" method="POST">
<p>Name:</p>
    <input name="field1" type="text" />
    <h3>&nbsp;</h3>
<p>Question:<p>
    <input name="field2" type="text" />
    <input type="submit" name="submit" value="Submit">
</form>
<a href="user.txt" download="chatlogs">download chat logs</a>
</td>
</table>
</div>

</div>
  • 写回答

2条回答 默认 最新

  • dpnof28482 2016-03-27 21:19
    关注

    let's take a look at why your js was not working

    You are matching a whole sentence for a word . here is an example if the var name is BADWORD SOMEONE_BADWORD what you are searching if the array holds that value (BADWORD SOMEONE_BADWORD)

    so what you should do is split the sentence into it's basic component words

    name.split(/[(\s|_)]/) // split the name by white space or underscore 
    

    now you will have an array of

    ["BADWORD", "SOMEONE", "BADWORD"]

    and the code becomes:

    $(function() {
      function containBadWord(words , banlist){
        var bad = false;
        $.each(words , function(k , v){
            if($.inArray(v, banlist) !==-1){
               bad = true;
               return false;
            }
        });
        return bad;
      }
      $("input:submit[name='submit']").on("click",function() {
        var name = $("input:text[name='']").val(); 
        var name_split = name.split(/[(\s|_)]/); 
        var message = $("input:text[name='field2']").val();
        var message_split = message .split(/[(\s|_)]/); 
        var badwords = ["dingbat", "poopy"];
    
        /* do this */ 
        if(containBadWord(name_split , badwords )){
         alert("your name has bad words")
        }
    
        if(containBadWord(message_split , badwords )){
         alert("your messagehas bad words")
        }
      });
    });
    

    edit :

    the code above was not working because your jquery selectors are all wrong as well .

    `input:text[name='']` will select an input with an empty attribute of `name`
    

    here is working code

    https://jsfiddle.net/tq7odeLg/

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测