简体   繁体   English

我的android登录/注册PHP服务器的问题

[英]Issues with my android login/registration PHP server

I need help with below codes,I can't figure out why I can't register an account on db. 我需要以下代码的帮助,我无法弄清楚为什么我不能在db上注册帐户。

Below my PHP scripts: 在我的PHP脚本下面:

update_user_info.php update_user_info.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;
        }
    }
 }

?>

login.php 的login.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);
}
?>

running above on postman and puttin all the required parameters shows below error: 在postman和puttin上面运行所有必需的参数显示如下错误:

["error_msg"] = "Required parameters email or password is missing!"; [“error_msg”] =“缺少必需参数的电子邮件或密码!”;

register.php register.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);
}
?>

Running above code in postman with all params filled shows below error: 在邮递员的代码上运行所有参数填充显示以下错误:

$response["error_msg"] = "Required parameters (fullname, email or password) is missing!"; $ response [“error_msg”] =“缺少所需参数(全名,电子邮件或密码)!”;

I must be doing something wrong. 我一定做错了什么。 Thank you for your help. 谢谢您的帮助。

issues resolved. 问题已解决。 on postman i needed to select x-wwww-form-urlencoded under body option for my script to work. 在邮递员我需要选择x-wwww-form-urlencoded在body选项下让我的脚本工作。 thanks 谢谢

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

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