2015-12-19 103 views
1

我的Android應用程序中有一個反覆出現的問題。PHP 500調用Volley時出現內部服務器錯誤

基本上我使用PHP和MYSQL數據庫來註冊和登錄用戶到我的應用程序。

註冊工作正常。我能夠連接到數據庫並將新用戶插入到表中而沒有任何問題。

我面臨的問題是登錄到應用程序時。每當我打電話登錄網址,我收到以下錯誤:

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

500意味着內部服務器error.check您的服務器配置。 –

+0

500內部服務器錯誤也表示「代碼中出現一些錯誤或警告」。有U檢查你的查詢是否正確或連接文件'require_once'include/DbConnect.php'; '沒有錯誤? –

+0

@riazhasan - 服務器沒有錯。如果有的話,那麼註冊函數肯定不起作用。請在評論前閱讀整個問題。 –

回答

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錯誤;)

+0

提到日誌文件指出我正確的方向。 – CHarris

相關問題