我的Android應用程序中有一個反覆出現的問題。PHP 500調用Volley時出現內部服務器錯誤
基本上我使用PHP和MYSQL數據庫來註冊和登錄用戶到我的應用程序。
註冊工作正常。我能夠連接到數據庫並將新用戶插入到表中而沒有任何問題。
我面臨的問題是登錄到應用程序時。每當我打電話登錄網址,我收到以下錯誤:
BasicNetwork.performRequest: Unexpected response code 500 for URL.
我嘗試使用其他工具來訪問這個網址,並手動發佈的參數,以消除該問題的錯誤可能從我的應用程序代碼中來。其實我有一個Generic 500 Internal Server Error
。使用此工具測試了註冊URL,它的工作完美。
我的PHP類都調用相同的腳本來獲取連接的詳細信息,所以沒有任何問題,因爲註冊工作。
這裏是我下面的代碼:
登錄類:
<?php
require_once 'UserFunctions.php';
$db = new UserFunctions();
$response = array("error" => FALSE);
if (isset($_POST['email']) && isset($_POST['password'])) {
$email = $_POST['email'];
$password = $_POST['password'];
$user = $db->getUserByEmailAndPassword($email, $password);
$count = $db->getUserCount();
if ($user != false) {
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
echo json_encode($response);
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Login credentials are wrong. Please try again!";
echo json_encode($response);
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "One of the required parameters is missing!";
echo json_encode($response);
}
?>
UserFunctions類:
<?php
class UserFunctions {
private $conn;
function __construct() {
require_once 'include/DbConnect.php';
$db = new DbConnect();
$this->conn = $db->connect();
}
function __destruct() {
}
public function storeUser($name, $email, $password) {
$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$password = $hash["encrypted"];
$salt = $hash["salt"];
$stmt = $this->conn->prepare("INSERT INTO users(unique_id, UserName, UserEmail, UserPassword, salt) VALUES(?, ?, ?, ?, ?)");
$stmt->bind_param("sssss", $uuid, $name, $email, $password, $salt);
$result = $stmt->execute();
$stmt->close();
if ($result) {
$stmt = $this->conn->prepare("SELECT * FROM users WHERE UserEmail = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $user;
} else {
return false;
}
}
public function getUserByEmailAndPassword($email, $password) {
$stmt = $this->conn->prepare("SELECT * FROM users WHERE UserEmail = ?");
$stmt->bind_param("s", $email);
if ($stmt->execute()) {
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $user;
} else {
return NULL;
}
}
public function isUserExisted($email) {
$stmt = $this->conn->prepare("SELECT UserEmail FROM users WHERE UserEmail = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
$stmt->close();
return true;
} else {
$stmt->close();
return false;
}
}
public function hashSSHA($password) {
$salt = sha1(rand());
$salt = substr($salt, 0, 10);
$encrypted = base64_encode(sha1($password . $salt, true) . $salt);
$hash = array("salt" => $salt, "encrypted" => $encrypted);
return $hash;
}
public function checkhashSSHA($salt, $password) {
$hash = base64_encode(sha1($password . $salt, true) . $salt);
return $hash;
}
}
?>
500意味着內部服務器error.check您的服務器配置。 –
500內部服務器錯誤也表示「代碼中出現一些錯誤或警告」。有U檢查你的查詢是否正確或連接文件'require_once'include/DbConnect.php'; '沒有錯誤? –
@riazhasan - 服務器沒有錯。如果有的話,那麼註冊函數肯定不起作用。請在評論前閱讀整個問題。 –