Wie kann ich die Fehler von PHP einzeln in der Unity-Konsole ausgeben?
Hallo,
wie schaffe ich es, die Fehler zu splitten und wenn es z.B. zwei Fehler sind, beide einzeln in Unity in der Konsole auszugeben?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using TMPro;
using System;
using Newtonsoft.Json;
public class RegisterUser : MonoBehaviour
{
public TMP_InputField EmailInput;
public TMP_InputField UsernameInput;
public TMP_InputField PasswordInput;
private void Update()
{
}
public void RegisterButton()
{
StartCoroutine(Register(EmailInput.text, UsernameInput.text, PasswordInput.text));
}
IEnumerator Register(string email, string username, string password)
{
string url = "geheim";
WWWForm form = new WWWForm();
form.AddField("email", email);
form.AddField("username", username);
form.AddField("password", password);
using (UnityWebRequest www = UnityWebRequest.Post(url, form))
{
yield return www.SendWebRequest();
if www.result != UnityWebRequest.Result.Success)
{
Debug.Log("Error: " + www.error);
}
else
{
// hier Fehler checken
}
}
}
}
Meine PHP if-Abfrage, wo mögliche Fehler ermittelt werden:
if (blablazeug) {
if ($resultemail->num_rows > 0) {
echo "1"; // errorcode for already used email
}
if ($resultuser->num_rows > 0) {
echo "2"; // error code for already used username
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "3"; // error code for wrong email
}
if (strlen($username) < 4 || strlen($username) > 10 || !preg_match('/^[a-zA-Z0-9]+$/', $username)) {
echo "4"; // error code for wrong username conditions
}
if (strlen($password) < 6 || strlen($password) > 20 || !preg_match('/[0-9]+/', $password) || !preg_match('/[A-Z]+/', $password) || !preg_match('/[a-z]+/', $password) || !preg_match('/[.,!@#$%^&*()\-_=+{};:?]+/', $password)) {
echo "5"; // error code for wrong password conditions
}
die();
}
1 Antwort
Von gutefrage auf Grund seines Wissens auf einem Fachgebiet ausgezeichneter Nutzer
PHP, Programmieren & Softwareentwicklung, Software & Apps
Also ich würde in PHP einfach ein JSON ausgeben und das musst du dann in C# entsprechend verarbeiten. In PHP ginge das bspw. so in der Art:
$arrErrors = [];
if ($resultemail->num_rows > 0) {
$arrErrors[] = 1;
}
if ($resultuser->num_rows > 0) {
$arrErrors[] = 2;
}
// …
die(json_encode((object)[
'success' => count($arrErrors) == 0,
'errors' => $arrErrors,
], JSON_UNESCAPED_UNICODE));
Den C# Teil kann ich dir leider nicht abnehmen.
Woher ich das weiß:Berufserfahrung – Entwickle seit > 20 Jahren Anwendungen mit PHP.