gpt4 book ai didi

php - 与用户级错误的共享偏好

转载 作者:行者123 更新时间:2023-11-30 00:59:20 25 4
gpt4 key购买 nike

我的 sharedpreference 有一些错误,即使用户不在用户名和密码上输入值,它仍然会转到 OwnerTabs,我不知道为什么。你们能帮帮我吗?这是我的代码。

验证.php

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

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

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

$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);
$_POST['sess_userrole'] = $row['roles'];

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

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

}
}
?>

主 Activity .java

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

final String TAG = this.getClass().getName();
Button btnLogin;
EditText etUsername, etPassword;
TextView tvRegister;
CheckBox cbRemember;
SharedPreferences pref;
SharedPreferences.Editor editor;
boolean checkFlag;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
etUsername = (EditText) findViewById(R.id.etFirstname);
etPassword = (EditText) findViewById(R.id.etPassword);
btnLogin = (Button) findViewById(R.id.btnLogin);
tvRegister = (TextView) findViewById(R.id.tvRegister);
cbRemember = (CheckBox) findViewById(R.id.cbRemember);
cbRemember.setOnCheckedChangeListener(this);
checkFlag = cbRemember.isChecked();
etUsername.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, etUsername.getText().toString());
}
});

pref = MainActivity.this.getSharedPreferences("Login.conf", Context.MODE_PRIVATE);
editor = pref.edit();


String username = pref.getString("username", "");
String password = pref.getString("password", "");

HashMap postData = new HashMap();
postData.put("username", username);
postData.put("password", password);

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);
Log.d(TAG,pref.getString("username", ""));
Toast.makeText(MainActivity.this, "Renter Login Successful!", Toast.LENGTH_SHORT).show();
Intent in = new Intent(MainActivity.this, RenterTabs.class);
startActivity(in);
MainActivity.this.finish();

} else if (s.contains("owner")) {
Log.d(TAG, s);
Log.d(TAG,pref.getString("username", ""));
Toast.makeText(MainActivity.this, "Owner Login Successful!", Toast.LENGTH_SHORT).show();
Intent in = new Intent(MainActivity.this, OwnerTabs.class);
startActivity(in);
MainActivity.this.finish();
} else {
Toast.makeText(MainActivity.this, "Wrong username or password...", Toast.LENGTH_SHORT).show();
}
}
});
task1.execute("http://carkila.esy.es/carkila/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();
}
Intent in = new Intent(MainActivity.this, RenterTabs.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();

Intent in = new Intent(MainActivity.this, OwnerTabs.class);
startActivity(in);
finish();
} else {
Toast.makeText(MainActivity.this, "Wrong username or password...", Toast.LENGTH_SHORT).show();
}
}
});
task1.execute("http://carkila.esy.es/carkila/authenticate.php");
}
});

tvRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent in = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(in);
}
});

}

@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
checkFlag = b;
Log.d(TAG, "checkflag: " + checkFlag);
}
}

最佳答案

你的 PHP 代码应该是这样的,

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

if(isset($_POST['username']) && isset($_POST['password'])){
$username = $_POST['username'];
$password = $_POST['password'];

$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) {
echo "-1";
} else {
$row = $query->fetch(PDO::FETCH_ASSOC);
$_POST['sess_userrole'] = $row['roles'];
if($_POST['sess_userrole'] == "renter") {
echo "renter";
} else if ($_POST['sess_userrole'] == "owner") {
echo "owner";
}
}
} else {
echo "-1";
}
?>

您没有将用户名密码 保存到SharedPreferences 中。所以你永远不会把它拿回来。登录成功后保存。

你的 Android 代码应该是这样的,

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

final String TAG = this.getClass().getName();
Button btnLogin;
EditText etUsername, etPassword;
TextView tvRegister;
CheckBox cbRemember;
SharedPreferences pref;
SharedPreferences.Editor editor;
boolean checkFlag;
HashMap<String, String> postData = new HashMap<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
etUsername = (EditText) findViewById(R.id.etFirstname);
etPassword = (EditText) findViewById(R.id.etPassword);
btnLogin = (Button) findViewById(R.id.btnLogin);
tvRegister = (TextView) findViewById(R.id.tvRegister);
cbRemember = (CheckBox) findViewById(R.id.cbRemember);
cbRemember.setOnCheckedChangeListener(this);
checkFlag = cbRemember.isChecked();
etUsername.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, etUsername.getText().toString());
}
});

pref = MainActivity.this.getSharedPreferences("Login.conf", Context.MODE_PRIVATE);
String username = pref.getString("username", "");
String password = pref.getString("password", "");

if (!username.equals("") && (!password.equals(""))) {
postData.put("username", username);
postData.put("password", password);
authenticate(postData);
}

btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
postData.put("username", etUsername.getText().toString());
postData.put("password", MD5.encrypt(etPassword.getText().toString()));

authenticate(postData);
}
});

tvRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent in = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(in);
}
});

}

@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
checkFlag = b;
Log.d(TAG, "checkflag: " + checkFlag);
}

private void authenticate(final HashMap<String, String> postData){
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){
// Login success, Save to prefs
editor = pref.edit();
editor.putString("username", postData.get("username"));
editor.putString("password", postData.get("password"));
editor.commit();
}

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

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

if(checkFlag){
// Login success, Save to prefs
editor = pref.edit();
editor.putString("username", postData.get("username"));
editor.putString("password", postData.get("password"));
editor.commit();
}

Toast.makeText(MainActivity.this, "Owner Login Successful!", Toast.LENGTH_SHORT).show();
Intent in = new Intent(MainActivity.this, OwnerTabs.class);
startActivity(in);
MainActivity.this.finish();
} else if (s.equals("-1")) {
Toast.makeText(MainActivity.this, "Wrong username or password...", Toast.LENGTH_SHORT).show();
}
}
});
task1.execute("http://carkila.esy.es/carkila/authenticate.php");
}
}

关于php - 与用户级错误的共享偏好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39673790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com