调用Volley时出现PHP 500内部服务器错误

时间:2015-12-19 12:15:30

标签: php android mysql android-volley

我的Android应用程序中出现了一个反复出现的问题。

基本上我使用PHP和MYSQL数据库来注册并将用户登录到我的应用程序中。

注册工作正常。我能够连接到数据库并将新用户插入表中而没有任何问题。

我面临的问题是登录应用时。每当我调用登录URL时,我都会收到以下错误:

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;
    }
}
?>

1 个答案:

答案 0 :(得分:2)

我找到了问题所在。 对于遇到非常讨厌的错误500的所有人,请检查您的日志。对我说,一旦我检查了日志,我发现方法checkhashSSHA()从未被使用过,这导致了以下错误:

PHP Fatal error:  Call to undefined function checkHashSSA() in /xxx/xxx/xxx/xxx/UserFunctions.php on line 54

因此我添加了以下代码来解密密码:

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();
        $salt = $user['salt'];
        $userPassword = $user['UserPassword'];
        $hash = $this->checkhashSSHA($salt, $password);

        if ($userPassword == $hash) {
            return $user;
        }
        $stmt->close();
    } else {
        return NULL;
    }
}

这解决了我的错误。

只是为了记录,这些错误的日志通常位于以下位置:var/log/apache2/error.log您可能需要对php.ini文件进行一些更改以记录这些错误。

希望这可以帮助任何有500错误的人;)