0

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

  • Thank you so much. The mysql set_charset has fixed my problem. – JoeyPlayzTV Jul 25 '20 at 21:47
  • While you are at it you are probably also missing error reporting. Check out [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Jul 25 '20 at 21:48

0 Answers0