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

调用Volley时PHP 500内部服务器错误,php,android,mysql,android-volley,Php,Android,Mysql,Android Volley,我的Android应用程序中经常出现问题 基本上,我使用PHP和MYSQL数据库将用户注册并登录到我的应用程序中 注册很好。我能够连接到数据库并将新用户插入到表中,没有任何问题 我面临的问题是登录应用程序时。无论何时调用登录url,都会出现以下错误: BasicNetwork.performRequest: Unexpected response code 500 for URL. PHP Fatal error: Call to undefined function checkHashSS

我的Android应用程序中经常出现问题

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

注册很好。我能够连接到数据库并将新用户插入到表中,没有任何问题

我面临的问题是登录应用程序时。无论何时调用登录url,都会出现以下错误:

BasicNetwork.performRequest: Unexpected response code 500 for URL.
PHP Fatal error:  Call to undefined function checkHashSSA() in /xxx/xxx/xxx/xxx/UserFunctions.php on line 54
我尝试使用其他工具访问此url并手动发布参数,以消除错误可能来自我的应用程序代码的问题。事实上,我得到了一个
通用500内部服务器错误
。用这个工具也测试了注册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);
}
?>
<?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;
    }
}
?>

用户函数类:

<?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);
}
?>
<?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的人,请检查您的日志。我突然想到,在检查日志后,我发现从未使用过方法
checkhashSSHA()
,这导致了以下错误:

BasicNetwork.performRequest: Unexpected response code 500 for URL.
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个错误的人;)

500表示内部服务器错误。请检查服务器配置。500内部服务器错误还表示“代码中的某些错误或警告”。您是否检查过您的查询是否正确或连接文件“require_once”是否包含/DbConnect.php“;”没有错误吗?@riazhasan-服务器中没有任何错误。如果有,那么寄存器函数肯定不起作用。在评论之前,请阅读整个问题。@AjeetKumar-我在用于开发服务器端脚本的IDE上没有发现任何错误。请将代码放入try-catch语句中,然后发布结果