eclipse android 使用 HTTP 将 java 值传递给 php

eclipse android pass a java value to php by using HTTP

我想将变量 q 从 java 传递给 php。

String q = "select author from books where 1";
try{
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("http://www2.XXXX./XXXX/X.php?qy="+q);
                //"http://10.0.2.2/tut.php", http://www.XXXX/XXXX/tut.php
                HttpResponse response = httpClient.execute(httpPost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
            }catch(Exception e){
                e.printStackTrace(); 
                System.out.println("Exception 1 caught");
            }

但是,php 文件无法从 java 获取值(php 正确连接到 mysql)。

php编码:

<?php
$con=mysql_connect("XXX.XXX","XX","XXX");
mysql_select_db("XX",$con);

$st = $_GET['qy'];
$r = mysql_query("$st");

while($row=mysql_fetch_array($r))
{
$out[]=$row;
}

print(json_encode($out));
mysql_close($con);
?>

我发现如果我只是将 table 名称传递给 php,它就可以工作。但是如果传递的变量变长,它就去抓一个。我怎样才能解决这个问题?将多个变量传递给 php(即 mysql_query("select $_GET['col'] from $_GET['table'] where $_GET['condition']");)怎么样?

使用Post代替Get,更安全。

像这样:

    String col = "author";
    String table = "books";    
    String condition = "1";

    try{
            List<NameValuePair> params = new ArrayList<NameValuePair>(); 
            params.add(new BasicNameValuePair("col", col));
            params.add(new BasicNameValuePair("table", table));
            params.add(new BasicNameValuePair("condition", condition)); 

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://www2.XXXX./XXXX/X.php");
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        }catch(Exception e){
            e.printStackTrace(); 
            System.out.println("Exception 1 caught");
        }

PHP:

    <?php
  if (isset($_POST['col']) && isset($_POST['table']) && isset($_POST['condition'])){
      $columnName= $_POST['col'];
      $tableName = $_POST['table'];
      $condition = $_POST['condition'];


    $dbh=mysql_connect ("localhost", "username", "password") or die('Cannot connect to the database because: ' . mysql_error());
    mysql_select_db ("database_name");

    $sql=mysql_query("select '$columnName' from '$tableName' where '$condition'");

    while($row=mysql_fetch_assoc($sql)) $output[]=$row;
    print(json_encode($output));
    mysql_close();
  }
?>

有效(by mandi yeung)

永远不要向后端发送任何查询。您的方法相当于 EditText,用户可以在其中对您的数据库执行所需的查询。不惜一切代价将您的查询排除在您的请求之外。查询部分(如之前的用户建议的那样)也很重要。请求中的查询部分实际上做同样的事情。 向后端发送 select 查询可能(并且完全会)授予任何攻击者对您的数据库的完全访问权限。这意味着他可能会更改或只是删除数据。 我可以篡改您的网络数据包并改为发送 drop table 语句。

drop table authors;
drop table books;

sBetterapproach 将发送 json 请求,如下所示:

String query = "{
    requestedData: getAllBooks,
    sessionId: 637euegdifoidhrgeydydihvr
}";

new BasicNameValuePair("smth", query);

然后从您的 php 端读取您输入的纯文本 $_POST["smth"].

然后您必须将 json 值解码为对象或数组,并确定您必须 运行 的查询。

if ($_GET["smth"]["requestedData"] === "getBooks") {
    // get books query executed here
}

记住:这种方法仍然不完美,但比你的好