【发布时间】:2016-03-26 01:09:35
【问题描述】:
我的 Android 应用程序经常出现问题。
基本上我使用 PHP 和一个 MYSQL 数据库来注册和登录用户到我的应用程序。
注册工作正常。我能够毫无问题地连接到数据库并将新用户插入表中。
我面临的问题是登录应用程序时。每当我调用登录网址时,都会收到以下错误:
BasicNetwork.performRequest: Unexpected response code 500 for URL.
我尝试使用其他工具访问此 url 并手动发布参数以消除错误可能来自我的应用程序代码的问题。事实上我得到了一个Generic 500 Internal Server Error。也用这个工具测试了注册网址,效果很好。
我的 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 表示内部服务器错误。请检查您的服务器配置。
-
500 内部服务器错误也表示“您的代码中有一些错误或警告”。你检查过你的查询是否正确或连接文件'require_once'include/DbConnect.php'; ' 没有错误?
-
@riazhasan - 服务器没有问题。如果有,那么注册功能肯定不会起作用。请在发表评论之前阅读整个问题。
-
@AjeetKumar - 我在用于开发服务器端脚本的 IDE 上没有发现任何错误。
-
将您的代码放入 try catch 语句中,然后发布结果
标签: php android mysql android-volley