如何将用户的userID从DB保存到我的共享首选项?

时间:2016-08-08 11:30:53

标签: php android mysql

我正在使用php和mysql登录我想要登录用户的用户ID也将保存在sharedpreference上。我不知道怎么做。 这是我的php登录。

的login.php

    <?php 
require 'database-config.php';

session_start();

$username = "";
$password = "";

if(isset($_POST['username'])){
$username = $_POST['username'];
}
if (isset($_POST['password'])) {
$password = $_POST['password'];
}
if (isset ($_SESSION['userID'])){
$userID = $_SESSION['userID'];
}


$q = 'SELECT * FROM tbl_user WHERE username=:username AND password=:password';

$query = $dbh->prepare($q);

$query->execute(array(':username' => $username, ':password' => $password));


if($query->rowCount() == 0){
header('Location: Login.php?err=1');
}else{

$row = $query->fetch(PDO::FETCH_ASSOC);
session_regenerate_id();
$_SESSION['sess_user_id'] = $row['userID'];
$_SESSION['sess_username'] = $row['username'];
$_SESSION['sess_userrole'] = $row['roles'];

session_write_close();

if( $_SESSION['sess_userrole'] == "renter"){
echo "renter";
echo $_SESSION['sess_user_id'];

}else if ($_SESSION['sess_userrole'] == "owner"){
echo "owner";
echo $_SESSION['sess_user_id'];

}
}
?>      

这就是我将用户名和密码保存到共享偏好的方式。

if (!username.equals("") && (!password.equals(""))) {
        PostResponseAsyncTask task1 = new PostResponseAsyncTask(MainActivity.this, postData,
                new AsyncResponse() {
                    @Override
                    public void processFinish(String s) {

                if (s.contains("renter")) {
                    Log.d(TAG, s);
                    Toast.makeText(MainActivity.this, "Renter Login Successful!", Toast.LENGTH_SHORT).show();
                    Intent in = new Intent(MainActivity.this, ListActivity.class);
                    startActivity(in);

                    finish();

                } else if (s.contains("owner")) {
                    Log.d(TAG, s);
                    Toast.makeText(MainActivity.this, "Owner Login Successful!", Toast.LENGTH_SHORT).show();
                    Intent in = new Intent(MainActivity.this, ownerhome.class);
                    startActivity(in);
                    finish();
                } else {
                    Toast.makeText(MainActivity.this, "Login Failed!", Toast.LENGTH_SHORT).show();
                }

                    }
                });
        task1.execute("http://carkila.esy.es/authenticate.php");

    }

    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            HashMap postData = new HashMap();

            postData.put("username", etUsername.getText().toString());
            postData.put("password", MD5.encrypt(etPassword.getText().toString()));

            PostResponseAsyncTask task1 = new PostResponseAsyncTask(MainActivity.this, postData,
                    new AsyncResponse() {
                        @Override
                        public void processFinish(String s) {
                            Log.d(TAG,s);
                            if (s.contains("renter")) {
                                if (checkFlag) {

                                    editor.putString("username", etUsername.getText().toString());
                                    editor.putString("password", MD5.encrypt(etPassword.getText().toString()));

                                    editor.apply();

                                    Log.d(TAG, pref.getString("password", ""));
                                }

                                Toast.makeText(MainActivity.this, "Renter Login Successful!", Toast.LENGTH_SHORT).show();
                                Intent in = new Intent(MainActivity.this, ListActivity.class);
                                startActivity(in);
                                finish();

                            } else if (s.contains("owner")) {

                                if (checkFlag) {


                                    editor.putString("username", etUsername.getText().toString());
                                    editor.putString("password", MD5.encrypt(etPassword.getText().toString()));

                                    editor.apply();
                                }

                                editor.putString("username", etUsername.getText().toString());
                                editor.putString("password", MD5.encrypt(etPassword.getText().toString()));
                                editor.apply();

                                Toast.makeText(MainActivity.this, "Owner Login Successful!", Toast.LENGTH_SHORT).show();
                                Intent in = new Intent(MainActivity.this, ownerhome.class);
                                startActivity(in);
                                finish();
                            } else {
                                Toast.makeText(MainActivity.this, "Login Failed!", Toast.LENGTH_SHORT).show();
                            }


                        }
                    });
            task1.execute("http://carkila.esy.es/authenticate.php");
        }

这是来自服务器的数据库我的数据库。

enter image description here

2 个答案:

答案 0 :(得分:0)

public static final String pref_name = "your shared preference name";
    SharedPreferences settings = getSharedPreferences(pref_name, MODE_PRIVATE);
    SharedPreferences.Editor prefEditor = settings.edit();
    prefEditor.putString("userId", userId);
    prefEditor.commit();

希望此代码可以帮助您

答案 1 :(得分:0)

同样保存用户ID

String userId = "your user id string from server";

editor.putString("user id", userId);
editor.apply();