douju8782 2016-06-14 11:16
浏览 64
已采纳

使用PHP输入数据处理以确保安全性

I am writing a code which will process the user text input in a registration form. I have implemented the following function which make sure that the input data is safe:

function input_check($Indata, $dbc) {       // input_check($Indata, $dbc)
    $Indata = trim($Indata);                // remove white spaces 
    $Indata = stripslashes($Indata);        // remove back slashes
    $Indata = strip_tags($Indata);          // remove html tags
    $Indata = htmlspecialchars($Indata);    // convert html entities
    $Indata = mysql_real_escape_string($Indata,$dbc);
    return $Indata;
}

Is there any other processing that I have to do in order to ensure that the input is safe?

I meant safe from malicious input data

  • 写回答

1条回答 默认 最新

  • doty58493 2016-06-14 11:35
    关注

    Your strategy to use all possible escaping mechanisms may be safe, but will make your application too complex - imagine what you need to do, to use the data (which seems to be stored in a MySQL database later, right?) to print it in a html form later.

    A more wise approach is, to use only the adequate escaping mechanism depending on the use of the data:

    • to store data in a MySQL database, use a database escaping mechanism (btw instead of mysql_real_escape_string() which is deprecated, use PDO::quote() or even better use parameter binding which already does escaping for you)
    • to print stored data in html text use htmlspecialchars(), possibly in conjunction with strip_tags()
    • to print stored data in html attributes use htmlspecialchars() together with urlencode()

    ... and so on. Then you will most likely be safe of SQLInjection, XSS attacks and so on.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler