【问题标题】:Recognize android httpclient with php sessions on web service通过 web 服务上的 php 会话识别 android httpclient
【发布时间】:2012-12-20 10:04:10
【问题描述】:

我正在构建一个使用 Web 服务(只是 php 脚本)来查询 mysql 数据库的 android 应用程序。目前没有任何身份验证系统,但我想在用户使用用户名和密码登录 mysqldatabase 后创建 php 会话......问题是目前我与脚本的所有连接都像这样工作:

public static long getOnlineAlarm(long taskID) {

    long alarm = -1;

    String result = null;
    InputStream is = null;

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

    nameValuePairs.add(new BasicNameValuePair("taskID", String.valueOf(taskID)));

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                "http://192.168.1.13/spotnshare/getOnlineAlarm.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);

        HttpEntity entity = response.getEntity();
        is = entity.getContent();

    } catch (Exception e) {
        Log.i("taghttppost", "" + e.toString());

    }

    // conversion de la réponse en chaine de caractère
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "UTF-8"));

        StringBuilder sb = new StringBuilder();

        String line = null;

        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }

        is.close();

        result = sb.toString();
    } catch (Exception e) {
        Log.i("tagconvertstr", "" + e.toString());
    }
    // recuperation des donnees json
    try {
        Log.i("tagconvertstr", "[" + result + "]");

        JSONObject jObj = new JSONObject(result);

        alarm = jObj.getLong("alarm");
        return alarm;

    } catch (JSONException e) {
        Log.i("tagjsonexp", "" + e.toString());
    } catch (ParseException e) {
        Log.i("tagjsonpars", "" + e.toString());
    }

    return alarm;
}

所以我创建了一个新的 DefaultHttpClient();每次我使用脚本时,这会成为 php 会话的问题吗?还是服务器可以识别客户端?

【问题讨论】:

    标签: php android mysql session


    【解决方案1】:

    您可以考虑为每个会话使用某种形式的唯一标识符。

    基本上,当您登录时对数据库的第一次调用将返回一个 sessionID。然后每次调用都将 sessionID 传递给 php 脚本(在每个 php 脚本上)。这是我见过的用于此类事情的最常见的方法。

    【讨论】:

    • 是的,但是我们如何在不访问数据库的情况下将 sessionID 保留在服务器端?有 cookie 吗?
    • 要么使用会话,要么有一个跟踪会话的数据库表 - 基本上保留 sessionID 并具有到期日期/时间,以便 sessionID 在一段时间后不再有效。例如带有 sessionID、userID、timeout 的表。如果您需要额外的安全性,您可能还需要包含 IP 地址或类似的标识值,以防止从不同位置传递 sessionID。
    猜你喜欢
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 2014-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多