注册android系统错误php
我正在尝试建立一个android登录系统,当我尝试注册时,我发现用户无法存储,我无法找到错误。我正在使用000webhost,我不知道这是否与此有关。请帮忙,谢谢你注册android系统错误php,php,android,mysql,login,web-hosting,Php,Android,Mysql,Login,Web Hosting,我正在尝试建立一个android登录系统,当我尝试注册时,我发现用户无法存储,我无法找到错误。我正在使用000webhost,我不知道这是否与此有关。请帮忙,谢谢你 我有互联网权限,并且登录正常,所以我认为这不是连接或数据库的问题。当我尝试注册时,代码转到“用户未能存储”部分,打印出“注册中发生未知错误!” 这是register.php: <?php require_once 'include/DB_Functions.php'; $db = new DB_Functio
我有互联网权限,并且登录正常,所以我认为这不是连接或数据库的问题。当我尝试注册时,代码转到“用户未能存储”部分,打印出“注册中发生未知错误!” 这是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;
}
}
?>
好的,我通过使用已弃用的mysql版本,而不是使用PDO来实现它。代码和数据库是正确的(我认为),我相信它给了我错误,因为000webhost不支持PDO或mysqli库 您必须更具体地说,当您尝试使用浏览器发布时会发生什么情况?这样行吗?使用postman(chrome/chrome应用程序)将帖子数据发送到该url,如果这样做有效,您在android设备上遇到的错误是什么?你确定它在提出请求吗?你有android.permission.INTERNET权限吗?我有INTERNET权限,并且登录正常,所以我认为连接或数据库没有问题。当我尝试注册时,代码会转到“用户未能存储”部分,打印出“注册时发生未知错误!”。您可以进入storeUser
函数并尝试调试吗?检查程序流是否进入if($result)
块检查执行时实际采用的路径。