php - 我的 android 登录/注册 PHP 服务器出现问题

标签 php android mysql json wamp

我需要以下代码的帮助,我不明白为什么我不能在 db 上注册一个帐户。

在我的 PHP 脚本下面:

更新用户信息.php

<?php

  class update_user_info {

    public function StoreUserInfo($fullname, $matno, $dept, $phone, $email, $password) {
        $hash = $this->hashFunction($password);
        $encrypted_password = $hash["encrypted"]; // encrypted password
        $salt = $hash["salt"]; // salt

        $stmt = $this->conn->prepare("INSERT INTO users(fullname, matno, dept, phone, email, encrypted_password, salt, created_at) VALUES(?, ?, ?, ?, ?, ?, ?, Now())");
        $stmt->bind_param("ssssssss", $fullname, $matno, $dept, $phone, $email, $encrypted_password, $salt, $created_at);
        $result = $stmt->execute();
        $stmt->close();

        // check for successful store
        if ($result) {
            $stmt = $this->conn->prepare("SELECT fullname, matno, dept, phone, email, encrypted_password, salt FROM users WHERE matno = ?");
            $stmt->bind_param("s", $matno);
            $stmt->execute();
            $stmt-> bind_result($token2,$token3,$token4,$token5,$token6,$token7,$token8);

            while ( $stmt-> fetch() ) {
               $user["fullname"] = $token2;
               $user["matno"] = $token3;
               $user["dept"] = $token4;
               $user["phone"] = $token5;
               $user["email"] = $token6;
            }
            $stmt->close();
            return $user;
        } else {
          return false;
        }
    }

    public function hashFunction($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 VerifyUserAuthentication($matno, $password) {

        $stmt = $this->conn->prepare("SELECT fullname, matno, dept, phone, email, encrypted_password, salt FROM users WHERE matno = ?");

        $stmt->bind_param("s", $matno);

        if ($stmt->execute()) {
            $stmt-> bind_result($token2,$token3,$token4,$token5,$token6,$token7,$token8);

            while ( $stmt-> fetch() ) {
               $user["fullname"] = $token2;
               $user["matno"] = $token3;
               $user["dept"] = $token4;
               $user["phone"] = $token5;
               $user["email"] = $token6;
               $user["encrypted_password"] = $token7;
               $user["salt"] = $token8;

            }

            $stmt->close();

            // verifying user password
            $salt = $token8;
            $encrypted_password = $token7;
            $hash = $this->CheckHashFunction($salt, $password);
            // check for password equality
            if ($encrypted_password == $hash) {
                // user authentication details are correct
                return $user;
            }
        } else {
            return NULL;
        }
    }

    public function checkHashFunction($salt, $password) {
        $hash = base64_encode(sha1($password . $salt, true) . $salt);
        return $hash;
    }


    public function CheckExistingUser($matno) {
        $stmt = $this->conn->prepare("SELECT matno from users WHERE matno = ?");

        $stmt->bind_param("s", $matno);

        $stmt->execute();

        $stmt->store_result();

        if ($stmt->num_rows > 0) {
            // user existed
            $stmt->close();
            return true;
        } else {
            // user not existed
            $stmt->close();
            return false;
        }
    }
 }

?>

登录.php

<?php
require_once 'update_user_info.php';
$db = new update_user_info();

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

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

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

    // get the user by email and password
    $user = $db->VerifyUserAuthentication($matno, $password);

    if ($user != false) {
        // user is found
        $response["error"] = FALSE;
        $response["uid"] = $user["unique_id"];
        $response["user"]["fullname"] = $user["fullname"];
        $response["user"]["email"] = $user["email"];
        $response["user"]["matno"] = $user["matno"];
        $response["user"]["dept"] = $user["dept"];
        $response["user"]["phone"] = $user["phone"];
        echo json_encode($response);
    } else {
        // user is not found with the credentials
        $response["error"] = TRUE;
        $response["error_msg"] = "Login credentials are wrong. Please try again!";
        echo json_encode($response);
    }
} else {
    // required post params is missing
    $response["error"] = TRUE;
    $response["error_msg"] = "Required parameters email or password is missing!";
    echo json_encode($response);
}
?>

在 postman 上面运行并输入所有必需的参数显示以下错误:

["error_msg"] = "Required parameters email or password is missing!";

注册.php

<?php

require_once 'update_user_info.php';
$db = new update_user_info();

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

if (isset($_POST['fullname']) && isset($_POST['matnum']) && isset($_POST['depart']) && isset($_POST['phone']) && isset($_POST['email']) && isset($_POST['passworded'])) {

    // receiving the post params
    $fullname = $_POST['fullname'];
    $matno = $_POST['matnum'];
    $email = $_POST['email'];
    $dept = $_POST['depart'];
    $phone = $_POST['phone'];
    $password = $_POST['passworded'];


    // check if user is already existed with the same email
    if ($db->CheckExistingUser($matno)) {
        // user already existed
        $response["error"] = TRUE;
        $response["error_msg"] = "User already existed with " . $matno;
        echo json_encode($response);
    } else {
        // create a new user
        $user = $db->StoreUserInfo($fullname, $matno, $dept, $phone, $email, $password);
        if ($user) {
            // user stored successfully
            $response["error"] = FALSE;
            $response["user"]["fullname"] = $user["fullname"];
            $response["user"]["matno"] = $user["matno"];
            $response["user"]["dept"] = $user["dept"];
            $response["user"]["phone"] = $user["phone"];
            $response["user"]["email"] = $user["email"];

            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 (fullname, email or password) is missing!";
    echo json_encode($response);
}
?>

在 postman 中运行上面的代码并填充所有参数显示以下错误:

$response["error_msg"] = "Required parameters (fullname, email or password) is missing!";

我一定是做错了什么。感谢您的帮助。

最佳答案

问题已解决。在 postman 上,我需要在 body 选项下选择 x-wwww-form-urlencoded 才能使我的脚本正常工作。谢谢

关于php - 我的 android 登录/注册 PHP 服务器出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41348983/

相关文章:

java - Actionbar.removealltabs() 留下标签空间

java - PreferenceFragment 与 AppCompatActivity Android

mysql - Docker:构建依赖于另一个要运行的图像的图像

php - 唯一的表单 token 禁用用户的多任务处理

php - 选择使用双引号字符串时,mysql 上出现错误 1064 <42000>

java - 强制我的 Activity 调用 onSaveInstanceState

php - MySQL 非常偶然 Unable save result set

mysql - 如何提高 MyISAM 中 INSERT/UPDATE 查询的性能

php - 对象序列化/反序列化如何工作?

php - 无法使用 laravel 4 从 mysql 数据库中的列获取完整数据