我正在为一个网站开发一个Android应用程序,我在它里面做了一个功能,用户可以在应用程序中发送数据到网站数据库.我使用PHP Web服务和Android代码中的URL连接到Web服务. 我怎样才能保证我的应用程序(和php web服务)的安全,不会有人通过调试apk找不到web服务url,并将大量数据发送到站点并使站点宕机.

任何让软件系统免受这类危险的提示都会对我有很大的帮助,谢谢.

编辑: 例如,现在我使用AsyncTask将edittext中的数据发送到WebService,如下所示. 我在oncreate中使用此代码将数据发送到名为AskServer()的AsyncTask类:

link = "http://mywebservice-adrress/name.php";
new AskServer().execute(link, edttext.getText().toString());

下面是我的AsyncTask类的doInBackground:

@Override
    protected Object doInBackground(String... params) {
        BufferedReader buf = null;

        try {
            URL link = new URL(params[0]);
            URLConnection connect = link.openConnection();

            connect.setDoOutput(true);
            OutputStreamWriter osw = new OutputStreamWriter(connect.getOutputStream());
            String data = URLEncoder.encode("field","UTF8") +"="+ URLEncoder.encode(params[1], "UTF8");
            sw.write(data);
            osw.flush();
        } catch (Exception e) {e.printStackTrace();}

        return null;
    }// end doInbackground()

以下是我的web服务中的php代码:

<?php

include 'config.php';
$field=$_POST['field'];


mysql_query("insert into `wp_comments` (`comment_content`)
                    values('$field')");
?>

这是一个例子,我的真实代码向服务器发送了更多的数据,也许上面的代码不起作用,我只是想让我的问题更准确、更具体.

推荐答案

过了这段时间,我带着我自己的问题来了一些答案.这些技巧是针对Android应用客户端的php服务器端的,但其逻辑可以在服务器端的其他语言中使用.

Security :
    1- Validate input data
    2- Filter input data
    3- Bind parameters to refuse query injection
    4- Give least access to user
    5- Refuse Leakage info by disableing log and httpd.conf


    6- Output Escaping : 
        escape html in output for sites and useless for webservices
        Functions like : htmlspecialchars()


    Filter      : diagnose XSS characters and filter them
    Validate    : check if input data is like the data type we expect

    Filter functions    :
        strip_tags()
        str_replace()
        preg_replace()
        Force Change DataType by placeing datatype before them : $input = (int) $input;


    Validat functions   :
        stripos()
        preg_match()
        "ctype" functions like : ctype_alnum(text), ctype_alpha(text), ctype_digit(text) : if input is ctype validate type return true else return false

        isset()
        filter_var()


    Access to user :
        initialize all variables before use
        use if and else statements to check everything is ok to prevent showing errors
        try catch for PDO SQL errors
        HASH the password and important data in database
        set hard names for database tables and columns
        make index.php in the folder and use this code :
            <?php
                header('location',HOME_URL);
                exit;
            ?>
        in .htdocs paste this code : Options -Indexes



    Database security :
        $mysqli->real_escape_string($input); // for scape chars like ' " 
        use stored procedures for refuse query injection.
        filter inputs for : stripos($input, 'UNION')

我希望这些小贴士能帮上忙.

Database相关问答推荐

华为Appcube中的对象字段类型

获取总和列的比率

实体关系图. IS A 关系如何转换为table表?

数据库设计 - 类别(categories)和子类别(sub-categories)

Objective-C中是否有类似于LINQ的东西?

repeating groups是什么意思?

Spring Framework 中的默认隔离级别

如何设计 SaaS 数据库?

MySQL workbench:如何将 mysql 数据库导出到 .sql 文件?

寻找基于磁盘的类 redis 数据库

是否有任何用于 NoSQL 数据库架构迁移的工具?

什么是数据仓库?

寻找示例数据库设计的好方法

使用 PHP/PDO 判断数据库表是否存在

Sqlite 判断表是否为空

postgreSQL 同时将列类型从 int 更改为 bigint

如何将特定的、可变的 order订单保存到数据库中

对于 N:M 关系,在 MongoDB 中推荐的级联删除等效项是什么?

与 IntelliJ IDEA 相比,DataGrip 附加值

数据库效率 - 每个用户的表与用户表