4

Androidでセッション処理プロセスを実行しようとしています。ここで、私は Android 経由で正常にログインしましたが、ログインしているユーザーのセッションを処理したいと考えています。これは私のlogin_suer.java(アンドロイド部分)です

package com.iwantnew.www;



import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class login_user extends Activity{
    private ProgressDialog pDialog;
    JSONParser jsonParser = new JSONParser();
    EditText login_email;
    EditText login_password;
    Button signin;
    TextView error_msg;

    private static String url_create_signin= "http://10.0.2.2/android_iwant/login_user.php";
    // JSON Node names
            private static final String TAG_SUCCESS = "success";
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.user_form);

                // Edit Text
                login_email = (EditText) findViewById(R.id.login_email);
                login_password = (EditText) findViewById(R.id.login_password);              
                signin = (Button) findViewById(R.id.signin);
                error_msg = (TextView) findViewById(R.id.error_msg);

                signin.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        // creating new product in background thread
                        new CheckLogin().execute();
                    }
                });
            }

            class CheckLogin extends AsyncTask<String, String, String> {
                /**
                 * Before starting background thread Show Progress Dialog
                 * */
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    pDialog = new ProgressDialog(login_user.this);
                    pDialog.setMessage("Signing in..");
                    pDialog.setIndeterminate(false);
                    pDialog.setCancelable(true);
                    pDialog.show();
                }
                /**
                 * Creating product
                 * */
                protected String doInBackground(String... args) {

                    //Building Parameters
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("email",login_email.getText().toString()));
                    params.add(new BasicNameValuePair("password", login_password.getText().toString()));

                    // getting JSON Object
                    // Note that create product url accepts POST method
                    JSONObject json = jsonParser.makeHttpRequest(url_create_signin,
                            "POST", params);

                    // check log cat fro response
                    Log.d("Create Response", json.toString());

                    // check for success tag
                    try {
                        int success = json.getInt(TAG_SUCCESS);

                        if (success == 1) {
                            // successfully created users
                            Intent i = new Intent(getApplicationContext(), post_item.class);
                            startActivity(i);

                            // closing this screen
                            finish();
                        } else {
                            // failed to sign in
                            error_msg.setText("Incorrect username/password");

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    return null;
                }

                /**
                 * After completing background task Dismiss the progress dialog
                 * **/
                protected void onPostExecute(String file_url) {
                    // dismiss the dialog once done
                    pDialog.dismiss();
                }

            }


}

ここで、この Java ファイルでセッション処理を開始するというアイデアが必要です。サーバー側のコードは次のとおりです。つまり、login_user.php

<?php
session_start();
// array for JSON response
$response = array();
if(isset($_POST['email']) && isset($_POST['password'])){
    $email = $_POST['email'];
    $password = $_POST['password'];

// include db handler
    require_once 'DB_Functions.php';
    $db = new DB_Functions();

     $user = $db->getUesrByEmailAndPassword($email, $password);
      if ($user != false) {
            // user found
            // echo json with success = 1
            $response["success"] = 1;
            $response["uid"] = $user["unique_id"];
            $response["user"]["name"] = $user["name"];
            $response["user"]["email"] = $user["email"];
            $response["user"]["created_at"] = $user["created_at"];
            $response["user"]["updated_at"] = $user["updated_at"];
            echo json_encode($response);
        } else {
            // user not found
            // echo json with error = 1
            $response["error"] = 1;
            $response["error_msg"] = "Incorrect email or password!";
            echo json_encode($response);
        }

}

?>

上記の php ファイルで使用されている関数は、つまり getUesrByEmailAndPassword($email, $password) です。

public function getUserByEmailAndPassword($email, $password) {
        $result = mysql_query("SELECT * FROM users WHERE email = '$email'") or die(mysql_error());
        // check for result 
        $no_of_rows = mysql_num_rows($result);
        if ($no_of_rows > 0) {
            $result = mysql_fetch_array($result);
            $salt = $result['salt'];
            $encrypted_password = $result['encrypted_password'];
            $hash = $this->checkhashSSHA($salt, $password);
            // check for password equality
            if ($encrypted_password == $hash) {
                // user authentication details are correct
                //return $result;
                session_start();
                $_SESSION['clientId'] = $result[0];
                $_SESSION['logged_in'] = TRUE;
            }
        } else {
            // user not found
            return false;
        }
    }

私のコードを機能させるのを手伝ってください。どんな助けでも大歓迎です。そのような問題の解決策を含むリンクは、私にとって役に立ちます。ありがとう!

4

1 に答える 1

2

私が見る限り、getUserByEmailAndPassword()パスワードチェックが成功した後、実際のユーザーデータを返すことはありません. //return $result;コメントアウトされています。$userしたがってnull、クライアントは「電子メールまたはパスワードが正しくありません!」というメッセージを受け取ります。メッセージ。

別物。PHP セッションが機能するには、クライアントは session_id を受信して​​記憶し、すべてのリクエストで GET または COOKIE パラメータとして送信する必要があります。あなたのコードを見ると、android が session_id を受け取っていることがわかりません。参照: http://www.php.net/manual/en/session.idpassing.php

ところで、$emailPOST から直接 SQL クエリで unscaped を使用するのは悪い考えです。参照: PHP で SQL インジェクションを防ぐにはどうすればよいですか?

于 2013-09-22T13:57:14.177 に答える