I am working on an Auth/Login system, that does the following. JavaFX-App sends POST-Request to an "auth.php", auth.php check if the user exists and returns a JSON with userdata.
If my Database contains a character like "Ü,Ä,ä,ö etc" the method json_encode($data)
fails. it returns nothing. Without one of those chars all works so fine. How I can get my code to working?
<?php
require "config.php";
$username = "";
$password = "";
foreach($_POST as $keyname => $value){
switch($keyname){
case 'un':
$username = $value;
break;
case 'pw':
$password = $value;
break;
default:
break;
}
}
$authuser = new AuthUser(false, -1, "dummy", "dummy", true);
$sql = "SELECT `id`, `username`, `authkey`, `banned` FROM `users` WHERE `username`= ? AND `password` = ? LIMIT 1;";
$statement = $conn->prepare($sql);
$statement->bind_param('ss', $username, $password);
$statement->execute();
$result = $statement->get_result();
if($result->num_rows > 0){
$data = $result->fetch_assoc();
$authuser = new AuthUser(true, $data['id'], $data['username'], $data['authkey'], $data['banned'] ? true : false);
}
//Here it returns nothing if the data contains an "ü,ö,Ä etc..." it is like the json_encode cannot convert the user object containing special chars to an JSON String
echo json_encode($authuser);
class AuthUser{
var $auth = false;
var $id;
var $username;
var $authkey;
var $banned;
function __construct($auth, $id, $username, $authkey, $banned){
$this->auth = $auth;
$this->id = $id;
$this->username = $username;
$this->authkey = $authkey;
$this->banned = $banned;
}
}
?>
Fixed by setting the mysqli connection encoding to UTF-8