简体   繁体   English

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

[英]PHP 500 Internal Server Error when calling Volley

I have a recurring problem in my Android application. 我的Android应用程序中经常出现问题。

Basically I am using PHP and a MYSQL database to register and login users into my app. 基本上,我使用PHP和MYSQL数据库将用户注册并登录到我的应用程序。

Registration works fine. 注册工作正常。 I am able to connect to the database and insert the new user into the table without any problems. 我能够连接到数据库并将新用户插入表中而没有任何问题。

The issue I am facing is when logging into the app. 我面临的问题是登录应用程序时。 Whenever I call the login url, I am getting the following error: 每当我调用登录URL时,都会出现以下错误:

BasicNetwork.performRequest: Unexpected response code 500 for URL.

I tried using other tools to access this url and posting the parameters manually to eliminate the issue that the error might be coming from my app code. 我尝试使用其他工具访问此url并手动发布参数,以消除错误可能来自我的应用程序代码的问题。 In fact I got a Generic 500 Internal Server Error . 实际上,我收到了“ Generic 500 Internal Server Error Tested the register URL with this tool too and it worked perfectly. 也使用此工具测试了注册网址,并且效果很好。

My PHP classes all call the same script to get the connection details, so there is no problem with that either since registration works. 我的PHP类都调用相同的脚本来获取连接详细信息,因此,由于注册有效,因此也没有问题。

Here is my code below: 这是我的代码如下:

Login class: 登录类别:

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

I found where my problem was. 我找到了问题所在。 For all those who encounter the very nasty error 500, check your logs. 对于所有遇到非常讨厌的错误500的人,请检查您的日志。 Occured to me that once I checked the logs, I found that the method checkhashSSHA() was never being used, and this was causing the following error: 对我来说,一旦检查了日志,便发现从未使用过方法checkhashSSHA() ,这将导致以下错误:

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

Hence I added the following code to decrypt the password: 因此,我添加了以下代码来解密密码:

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

This solved my error. 这解决了我的错误。

Just for the record, logs for such errors are usually found in the following location: var/log/apache2/error.log You may need to make some change to the php.ini file to log these errors. 仅作记录,通常会在以下位置找到此类错误的日志: var/log/apache2/error.log您可能需要对php.ini文件进行一些更改才能记录这些错误。

Hope this helps anyone with the 500 error ;) 希望这对500错误有帮助的人;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM