调用Volley时发生PHP 500 Internal Server Error [英] PHP 500 Internal Server Error when calling Volley

查看:60
本文介绍了调用Volley时发生PHP 500 Internal Server Error的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a recurring problem in my Android application.

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

Basically I am using PHP and a MYSQL database to register and login users into my app.

注册工作正常.我可以连接到数据库并将新用户插入表中,而不会出现任何问题.

Registration works fine. I am able to connect to the database and insert the new user into the table without any problems.

我面临的问题是登录该应用程序时.每当我调用登录URL时,都会出现以下错误:

The issue I am facing is when logging into the app. Whenever I call the login url, I am getting the following error:

BasicNetwork.performRequest: Unexpected response code 500 for URL.

我尝试使用其他工具访问此url并手动发布参数,以消除错误可能来自我的应用程序代码的问题.实际上我得到了Generic 500 Internal Server Error.也使用此工具测试了注册网址,并且效果很好.

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. In fact I got a Generic 500 Internal Server Error. Tested the register URL with this tool too and it worked perfectly.

我的PHP类都调用相同的脚本来获取连接详细信息,因此,由于注册有效,因此也没有问题.

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
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的人,请检查您的日志.对我来说,一旦检查了日志,我发现方法checkhashSSHA()从未被使用过,这导致了以下错误:

I found where my problem was. For all those who encounter the very nasty error 500, check your logs. 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:

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

这解决了我的错误.

为便于记录,通常在以下位置找到此类错误的日志:var/log/apache2/error.log您可能需要对php.ini文件进行一些更改才能记录这些错误.

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.

希望这对500错误有帮助的人;)

Hope this helps anyone with the 500 error ;)

这篇关于调用Volley时发生PHP 500 Internal Server Error的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆