dra11767 2013-09-10 09:10
浏览 47
已采纳

SQL注入保护动作

I'm using PHP to connect to my db with mysqli. The query is made in Java code and send to my PHP script with JSON.

I have one query which is dynamic and made in my code so I can't use prepared statement.

For example I have in one time: SELECT x FROM y WHERE z = ? AND o =?. And in other time: SELECT x FROM y WHERE z = ? AND o =? and k = ? and j =?

It's by the user choice and I cant know what query will be.

My problem is that I'm exposed to SQL injection.

I thought about one step to protect my db from SQL injection and I want your opinion if it's a good idea or there is a better one. I thought about sending secret key to my PHP script and only if the key is good the SQL action will be.

For example:

if($key == "2fdgfg1sdjsdgj2JDSJDFSG2394KSDJG")
{
 // do SQL
}

This is what I use in my code. The user can select which car types he want to see from my DB.

In the Android application he have dialog in which he can select which car he want. Then I use this code in my java code:

                        int length = usersChecked.length;
                        String sqlQuery = "";
                        for (int i = 0; i < length; i++) {
                            if (usersChecked[i]) {
                                if (sqlQuery.equals(""))
                                    sqlQuery = " = " + list[i];
                                else
                                    sqlQuery = sqlQuery + " OR user = "
                                            + list[i];
                            }
                        }

                        favoritesQuery = "SELECT car, year, gas_type FROM cars_names WHERE user "
                                + sqlQuery
                                + " ORDER BY date DESC LIMIT ? , ?";
  • 写回答

1条回答 默认 最新

  • duanbu9345 2013-09-10 09:14
    关注

    The query is made in java code and send to my php script with JSON.

    This is what you're doing wrong.

    • The query shouldn't be of user choice
    • The query shouldn't be made in java (or rather javascript it is) code
    • The query shouldn't be sent with JSON

    The query have to be hardcoded in PHP script.

    At least you can create your query dynamically, based on the user's choice:

    $sql = "SELECT x FROM y WHERE z = ?";
    if (isset($json['o']))
    {
        $sql .= " AND o =?";
        $values[] = $json['o']
    }
    // and so on
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号