注册android系统错误php

时间:2015-10-17 16:04:52

标签: php android mysql login web-hosting

我正在尝试创建一个Android登录系统,当我尝试注册时,我发现用户无法存储,我无法找到错误。我正在使用000webhost我不知道它是否与此有关。请帮忙,谢谢

我有Internet权限,登录正在运行,所以我认为不是连接或数据库的问题。当我尝试注册时,代码转到"用户无法存储"部分,打印出来"注册时发生了未知错误!"。

这是register.php:

    <?php
    require_once 'include/DB_Functions.php';

$db = new DB_Functions();
// json response array
$response = array("error" => FALSE);

if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['password'])) {

    // receiving the post params
    $name = $_POST['name'];
    $email = $_POST['email'];
    $password = $_POST['password'];

    // check if user is already existed with the same email
    if ($db->isUserExisted($email)) {
        // user already existed
        $response["error"] = TRUE;
        $response["error_msg"] = "User already existed with " . $email;
        echo json_encode($response);
    } else {
        // create a new user
        $user = $db->storeUser($name, $email, $password);
        if ($user) {
            // user stored successfully
            $response["error"] = FALSE;
            $response["uid"] = $user["unique_id"];
            $response["user"]["name"] = $user["name"];
            $response["user"]["email"] = $user["email"];
            $response["user"]["created_at"] = $user["created_at"];
            $response["user"]["updated_at"] = $user["updated_at"];
            echo json_encode($response);
        } else {
            // user failed to store
            $response["error"] = TRUE;
            $response["error_msg"] = "Unknown error occurred in registration!";
            echo json_encode($response);
        }
    }
} else {
    $response["error"] = TRUE;

    $response["error_msg"] = "Required parameters (name, email or password) is missing!";
    echo json_encode($response);
}
?>

这是DB_Functions.php类:

<?php

class DB_Functions {

    private $conn;

    // constructor
    function __construct() {
        require_once 'DB_Connect.php';
        // connecting to database
        $db = new Db_Connect();
        $this->conn = $db->connect();
    }

    // destructor
    function __destruct() {

    }

    public function consigueResultado( $stmt ) {
        $RESULT = array();
        for ( $i = 0; $i < $stmt->num_rows; $i++ ) {
            $Metadata = $stmt->result_metadata();
            $PARAMS = array();
            while ( $Field = $Metadata->fetch_field() ) {
                $PARAMS[] = &$RESULT[ $i ][ $Field->name ];
            }
            call_user_func_array( array( $stmt, 'bind_result' ), $PARAMS );
            $stmt->fetch();
        }
        return $RESULT;
    }

    /**
     * Storing new user
     * returns user details
     */
    public function storeUser($name, $email, $password) {
        $uuid = uniqid('', true);
        $hash = $this->hashSSHA($password);
        $encrypted_password = $hash["encrypted"]; // encrypted password
        $salt = $hash["salt"]; // salt
        $stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES(:uuid, :name, :email, :encrypted_password, :salt, NOW())");
        $stmt->bindParam(':uuid', $uuid);
        $stmt->bindParam(':name', $name);
        $stmt->bindParam(':email', $email);
        $stmt->bindParam(':encrypted_password', $encrypted_password);
        $stmt->bindParam(':salt', $salt);
        $result = $stmt->execute();
        //$this->conn = NULL;
        //$stmt = NULL;

        // check for successful store
        if ($result) {
            $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = :email");
            $stmt->bindParam(':email', $email);
            $stmt->execute();
            $user = $this->consigueResultado( $stmt );
            //$this->conn = NULL;
            $stmt = NULL;

            return $user;
        } else {
            return false;
        }
    }

    /**
     * Get user by email and password
     */
    public function getUserByEmailAndPassword($email, $password) {

        $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = :email");

        $stmt->bindParam(':email', $email);

        if ($stmt->execute()) {
            $user = $this->consigueResultado( $stmt );
            //$this->conn = NULL;
            $stmt = NULL;

            return $user;
        } else {
            return NULL;
        }
    }

    /**
     * Check user is existed or not
     */
    public function isUserExisted($email) {
        $stmt = $this->conn->prepare("SELECT email from users WHERE email = :email");

        $stmt->bindParam(':email', $email);

        $stmt->execute();

        if ($stmt->num_rows > 0) {
            // user existed 
            //$this->conn = NULL;
            $stmt = NULL;

            return true;
        } else {
            // user not existed
            //$this->conn = NULL;
            $stmt = NULL;
            return false;
        }
    }

    /**
     * Encrypting password
     * @param password
     * returns salt and encrypted password
     */
    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;
    }

    /**
     * Decrypting password
     * @param salt, password
     * returns hash string
     */
    public function checkhashSSHA($salt, $password) {

        $hash = base64_encode(sha1($password . $salt, true) . $salt);

        return $hash;
    }

}

?>

1 个答案:

答案 0 :(得分:0)

好的,我使用不推荐使用的版本mysql而不是使用PDO。代码和数据库是正确的(我认为),我认为它给了我错误,因为000webhost不支持PDO或mysqli库。