我正在练习使用Java,PHP和SQL创建身份验证系统。我在MySQL数据库中有一个条目,但是我的应用程序拒绝登录。我还创建了该应用程序的注册端,但是它也不起作用。所以我想知道没有远程登录访问权限是否会影响它。
if ($_SERVER['REQUEST_METHOD']=='POST') {
$matriculation_number = $_POST['matriculation_number'];
$password = $_POST['password'];
$conn = mysqli_connect("localhost", "id4922470_public", "password", "id4922470_cuc_general");
if ($conn = mysqli_connect("localhost", "id4922470_public", "password", "id4922470_cuc_general")) {
$sql = "SELECT * FROM users_general WHERE matriculation_number='$matriculation_number' ";
$response = mysqli_query($conn, $sql);
$check = array();
$result = array();
$check['connection'] = array();
$result['login'] = array();
if ( mysqli_num_rows($response) === 1 ) {
$row = mysqli_fetch_assoc($response);
if ( password_verify($password, $row['password']) ) {
$index['matriculation_number'] = $row['matriculation_number'];
$index['first_name'] = $row['first_name'];
$index['middle_name'] = $row['middle_name'];
$index['last_name'] = $row['last_name'];
$index['gender'] = $row['gender'];
$index['level'] = $row['level'];
$index['programme'] = $row['programme'];
$index['department'] = $row['department'];
$index['college'] = $row['college'];
array_push($result['login'], $index);
$result['success'] = "1";
$result['message'] = "success";
echo json_encode($result);
mysqli_close($conn);
} else {
$result['success'] = "0";
$result['message'] = "error";
echo json_encode($result);
mysqli_close($conn);
}
} else {
$check['Connected'] = "Connection failed";
echo json_encode($check);
}
}
}
齐射代码:
StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN_REQUEST_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
System.err.println("__________________________________________LOG________________________________________");
Log.i("tagconvertstr", "["+response+"]");
System.err.println("__________________________________________LOG________________________________________");
JSONObject jsonObject = new JSONObject(response);
String success = jsonObject.getString("success");
JSONArray jsonArray = jsonObject.getJSONArray("login");
if (success.equals("1")) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
String matriculation_number = object.getString("matriculation_number").trim();
String first_name = object.getString("first_name").trim();
String middle_name = object.getString("middle_name").trim();
String last_name = object.getString("last_name").trim();
String gender = object.getString("gender").trim();
int level = object.getInt("level");
String programme = object.getString("programme").trim();
String department = object.getString("department").trim();
String college = object.getString("college").trim();
System.out.println(matriculation_number + "\n" + first_name + "\n" + middle_name + "\n" +
last_name + "\n" + gender + "\n" + level + "\n" + programme + "\n" +department + "\n" + college);
System.out.println("___________________________CHECK___________________________________");
String message = "Welcome " + matriculation_number;
Toast.makeText(LoginActivity.this, message, Toast.LENGTH_SHORT).show();
Intent l = new Intent(LoginActivity.this, DashboardActivity.class);
startActivity(l);
LoginActivity.this.finish();
}
}
} catch (JSONException e) {
e.printStackTrace();
String message = "Error " + e.toString();
Toast.makeText(LoginActivity.this, message, Toast.LENGTH_SHORT).show();
pWord.setText("");
pWord.clearFocus();
uName.clearFocus();
login.setVisibility(View.VISIBLE);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
String message = "Error " + error.toString();
Toast.makeText(LoginActivity.this, message, Toast.LENGTH_SHORT).show();
pWord.setText("");
pWord.clearFocus();
uName.clearFocus();
login.setVisibility(View.VISIBLE);
}
}
) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("matriculation_number", matriculation_number);
params.put("password", password);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
我被要求发布脚本...我已经完成了。我还添加了一项检查,以确保脚本已连接到数据库。
答案 0 :(得分:0)
因为php文件负责连接到数据库并查找使用验证,所以Java不必具有访问数据库的权限即可工作。您需要做的就是从Java调用php端点。使用一些http库将内容发布到php文件。
首先查看php文件是否可以正确手动运行。您可以使用邮递员将帖子发送到php文件。 https://www.getpostman.com/docs/v6/postman/sending_api_requests/requests
然后查看输出。如果还可以,则您的Java代码可能有错误。
我认为这行代码行不通
if ($conn = mysqli_connect("localhost", "id4922470_public", "password","id4922470_cuc_general")) {
}
应替换为
if($conn){
}