清理php中的输入数据

清理php中的输入数据,php,regex,mysqli,Php,Regex,Mysqli,我在我的网站上有一个联系表格,访问者可以在这里与我联系。它只有3个字段:姓名、电话号码和信息 这是我的函数,用于清理将写入数据库的输入数据。我在stackoverflow检查了这个问题和一些问题,得出了这个解决方案。 这是清理数据的安全方法吗? 功能的顺序是否正确? 顺序真的重要吗 spl_autoload_register(function ($class) { include '../classes/' . $class . '.class.php'; }); /* bej

我在我的网站上有一个联系表格,访问者可以在这里与我联系。它只有3个字段:姓名、电话号码和信息

这是我的函数,用于清理将写入数据库的输入数据。我在stackoverflow检查了这个问题和一些问题,得出了这个解决方案。 这是清理数据的安全方法吗? 功能的顺序是否正确? 顺序真的重要吗

spl_autoload_register(function ($class) {
    include '../classes/' . $class . '.class.php';
});

/*
    beje qe nqs ekziston ip bej update vtm time dhe ++$count te ajo ip;
*/

$db = DB::get_instance();   

function sanatize($input) {
    $db = DB::get_instance();
    //mysqli real escape string for all vars.
    //preg_replace for whitespaces, tabs, new lines.
    //strip html tags.
    //convert html tags.
    //strip slashes.
    //htmlentities: htmlentities — Convert all applicable characters to HTML entities.
    //nuk duhet sepse kemi strip tags.

    //trim string
    $trimed_string = trim($input);
    //filter string using php FILTER_SANITIZE_STRING filter.
    $filtered_string = filter_var($trimed_string, FILTER_SANITIZE_STRING);
    //remove slashes
    $no_slash_string = stripslashes($filtered_string);
    //convert special characters to HTML entities
    $conv_string = htmlspecialchars($no_slash_string);
    //strip html tags
    $stripped_tags_string = strip_tags($conv_string);
    //replace whitespaces
    $filtered_string = preg_replace('#[\s]+#', ' ', $stripped_tags_string);
    $safe_string = $mysqli_escaped_string = $db->mysqli->real_escape_string($filtered_string);

    return $safe_string;
}

//send message
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if(isset($_POST["name"]) && isset($_POST["tel"]) && isset($_POST["message"])) {

        $name = sanatize($_POST["name"]);
        $tel = intval(sanatize($_POST["tel"]));

        //sepse intval ja heq zeron.
        $message = trim(sanatize($_POST["message"]));
        $time = time();

        //name validation.      
        //only letter and spaces.
        if(!preg_match('/^[a-zA-Z\s]+$/', $name)) {
            echo "name should contain only letters.";
        } else if(strlen($_POST["name"]) < 3) {
            echo "name should be three chars min.";
        } else if(!preg_match('/^[1-9][0-9]*$/', $tel)) {
            echo "your phone number should contain only numbers.";
        } else if(strlen($tel) != 9) {
            echo "your phone number must be 10 digits.";
        } else if(in_array(substr($tel, 0, 3), array(066, 067, 068, 069))) {
            echo "your phone number must begin with 066, 067, 068 or 069.";
        } else if(strlen($message) == 0) {
            echo "message should be 10 letters min.";
        } else {
            //insert into db.
            $query = "insert into `messages` (name, tel, message, time) VALUES ('$name', '$tel', '$message', '$time')";
            $db->mysqli->query($query);
            echo "sent";
        }
    }
}
spl\u自动加载\u寄存器(函数($class){
包括“../classes/”.$class..class.php”;
});
/*
beje qe nqs ekziston ip bej更新vtm时间dhe++$count te ajo ip;
*/
$db=db::get_instance();
函数sanatize($input){
$db=db::get_instance();
//所有变量的mysqli实际转义字符串。
//preg_替换空白、制表符和新行。
//剥离html标记。
//转换html标记。
//条状斜线。
//htmlentities:htmlentities-将所有适用的字符转换为HTML实体。
//nuk duhet sepse kemi条纹标签。
//修剪线
$trimed_string=trim($input);
//使用php过滤器过滤字符串。
$filtered\u string=filter\u var($trimmed\u string,filter\u SANITIZE\u string);
//删除斜杠
$no\u slash\u string=stripslashes($filtered\u string);
//将特殊字符转换为HTML实体
$conv_string=htmlspecialchars($no_slash_string);
//剥离html标记
$stripped_tags_string=strip_tags($conv_string);
//替换空白
$filtered_string=preg_replace('.[\s]+#','.$stripped_tags_string);
$safe\u string=$mysqli\u转义\u string=$db->mysqli->real\u转义\u string($filtered\u string);
返回$safe_字符串;
}
//发送消息
如果($\服务器[“请求\方法”]=“发布”){
如果(isset($_POST[“name”])和&isset($_POST[“tel”])和&isset($_POST[“message”])){
$name=sanatize($_POST[“name”]);
$tel=intval(sanatize($_POST[“tel”]);
//我想我会的。
$message=trim(sanatize($_POST[“message”]);
$time=time();
//名称验证。
//只有字母和空格。
如果(!preg_match('/^[a-zA-Z\s]+$/',$name)){
echo“名称应仅包含字母。”;
}else if(strlen($_POST[“name”])<3){
echo“名称至少应为三个字符”;
}如果(!preg_match('/^[1-9][0-9]*$/',$tel))匹配,则为else{
回显“您的电话号码应仅包含号码。”;
}否则,如果(斯特伦($tel)!=9){
echo“您的电话号码必须是10位数字。”;
}else if(在数组(substr($tel,0,3),数组(066,067,068,069))中){
echo“您的电话号码必须以066、067、068或069开头。”;
}else if(strlen($message)==0){
回声“信息应至少10个字母”;
}否则{
//插入数据库。
$query=“插入到`消息'(名称、电话、消息、时间)值('$name','$tel','$message','$time')”;
$db->mysqli->query($query);
回音“已发送”;
}
}
}

相关位如下:

filter_var($trimed_string, FILTER_SANITIZE_STRING);
$db->mysqli->real_escape_string($filtered_string);
尽管有人可能会说,准备好的语句比手动转义更容易使用(因此不太可能遗漏字段),但从输入的角度来看,这就足够了。即使缺少
filter\u var()

但是,您还对用户数据执行了大量的编辑,这些编辑被误导了:

stripslashes($filtered_string);
htmlspecialchars($no_slash_string);
strip_tags($conv_string);
preg_replace('#[\s\t\n]+#', ' ', $stripped_tags_string);
如果我无法恢复用户提供的精确输入数据,并且基本上删除任意字符并损坏数据,那么我不会考虑可靠的联系方式。

我认为根本问题在于两个误解:

  • 数据清理不是绝对的。给定的数据是否被认为是安全的完全是上下文相关的。例如,HTML标记在SQL中是不相关的

  • 为了安全起见,不必删除特殊字符。你只需要妥善处理它们


  • \s
    匹配任何空白字符
    [\r\n\t\f]
    所以只需要\s?谢谢。像逃跑一样,消毒应该是特定于上下文的。您试图将您的输入限制为什么?“安全”的具体用途是什么?为什么不使用mysqli绑定参数呢?是的
    \s
    完全填充它。另请参见