I am programming a login system now on Unity, but I have this weird bug. For some reason when I get the output of the php file, the if statement in the C# file can't see that 'loginreturn' (= AccountDoesntExist) and the string "AccountDoesntExist" are the same. I don't know why but maybe you smart people see the bug and can help me out.
C# Code:
IEnumerator TryLogin(string username, string password)
{
WWWForm form = new WWWForm();
form.AddField("username", username);
form.AddField("password", password);
WWW loginWWW = new WWW(LoginURL, form);
yield return loginWWW;
if (!string.IsNullOrEmpty(loginWWW.error))
{
Debug.LogError("Cannot connect to LOGIN servers! Error: " + loginWWW.error);
}else
{
string loginreturn = loginWWW.text;
Debug.Log(loginWWW.text);
Debug.Log(loginreturn);
if (loginreturn == "AccountDoesntExist")
Debug.Log("WORKS!");
}
}
PHP Code (which will always return "AccountDoesntExist" because of the way I log in):
<?php
$inputusername = $_REQUEST["username"];
$password = $_REQUEST["password"];
$servername = "localhost";
$username = "chatsystem_accs";
$password = "CENCORED";
$dbname = "chatsystem_accs";
//Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
//Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Connected successfully
$sql = "SELECT `username`, `password` FROM `accounts`";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if ($inputusername == $row["username"]) {
if (password_verify($password, $row["password"])) {
echo "Success";
}else {
echo "UsernameOrPasswordIncorrect";
}
}else {
echo "AccountDoesntExist";
}
}
}else {
echo "AccountDoesntExist";
}
//Close connection
$conn->close();
?>
Very likely a UTF-8 problem. There is an extra data in the received bytes. Convert the received data to UTF-8 before comparing it.
Replace
string loginreturn = loginWWW.text;
with
string loginreturn = System.Text.Encoding.UTF8.GetString(loginWWW.bytes, 3, loginWWW.bytes.Length - 3);
EDIT:
Did debugging like this:
Debug.Log("Received Length: "+ loginreturn.Length);
Debug.Log("Compare Length: " + "AccountDoesntExist".Length);
and the results were:
Received Length: 19
Compare Length: 18
This is wrong. There is an extra character somewhere.
Debugged again with the function below then called it with displayAsChar(loginreturn);
void displayAsChar(string badValue)
{
char[] values = badValue.ToCharArray();
for (int i = 0; i < values.Length; i++)
{
string tempResult = "Value at index " + i + " is: " + values[i];
Debug.Log(tempResult);
}
}
It shows there is an empty character at the end of the character. I thought that was just " " but it wasn't.
I made another function to see what this empty character is:
void showStringAsHex(string badValue)
{
foreach (char c in badValue)
Debug.Log("To Unicode: " + ((int)c).ToString("X2"));
}
Bingo. That last character is 0A (Hex) which is also represented as \n. This is used as a line feed.
FIX:
Before doing the compare action, trim the string. This will remove any escape character and empty strings in the beginning and end of the character.
To trim the character, simply add the code below before comparing the string.
loginreturn = loginreturn.Trim();
Why post the debugging process?
The characters might be different for different servers. Posting this will help others troubleshoot and fix this problem for them-selves in the future.
Related
So I have a list in c# that's being converted to a string, which is then converted to a 2-dimensional array in PHP, which is then sent to MySQL database. Now how do I go about reversing this, whereby I can download from the database to PHP than to c# list.
This is what I have so far, but I'm not sure if I need to download the data back to an array in PHP or if I should download it as a string and break it up into a list in c#?
This is my c# code:
public List<Ship> shipList = new List<Ship>();
string shipListString = "";
WWWForm form = new WWWForm();
form.AddField("username", username);
form.AddField("shipcount", ShipInventory.Count);
for (int i = 0; i < shipList.Count; i++)
{
shipListString = shipListString + " " + shipList[i].id + " '" + shipList[i].username + "' '" + shipList[i].name + "' " + shipList[i].power +
"zzz";// + ShipInventory[i];
}
form.AddField("shipinventory", shipListString);
WWW www = new WWW("x.x.x.x/savedata.php", form);
yield return www;
And this is my php:
$shiparraytobesplit = $_POST["shipinventory"];
$ships = explode("zzz", $shiparraytobesplit);
unset($ships[count($ships)-1]);
$shipinfo = array_map(function($ship) {
$tempshipinfo = explode(" ", $ship);
$ship_assoc = [
"id" => $tempshipinfo[1],
"name" => $tempshipinfo[2],
"username" => $tempshipinfo[3],
"hp" => $tempshipinfo[4]
];
return $ship_assoc;
}, $ships);
$sql = sprintf('INSERT INTO shipinventory (shipid,shipname,username,shippower) VALUES (%s)', implode(',',array_values($shipinfo[$i])));
if(!mysqli_query($con, $sql))
{
echo("error description: " . mysqli_error($con));
}
This is working well to upload my c# list into the database, but I'm not sure what's the best way to download it from the database back to a c# list. Any advice would be awesome! Thanks
Ok so I managed to make it work but I imagine there are better methods so please share! Thanks:
C# Script:
WWW www = new WWW("x.x.x.x/loaddata.php");
yield return www;
stringFromPHP = www.text;
char[] delimiterENTER = new char[] {'\n' };
char[] delimiterSPACE = new char[] { ' ' };
shipStringArray = stringFromPHP.Split(delimiterENTER, StringSplitOptions.RemoveEmptyEntries);
shipList.Clear();
for (int i = 0; i < shipStringArray.Count(); i++)
{
string[] shipInfo = shipStringArray[i].Split(delimiterSPACE);
shipList.Add(new Ship(Int32.Parse(shipInfo[0]), shipInfo[1], shipInfo[2], Int32.Parse(shipInfo[3]), shipStringArray[i]));
}
and php:
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT shipid, username, shipname, shippower FROM shipinventory";
if ($result = mysqli_query($con, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
$stringtoexplodeincsharp printf ("%s %s %s %s\n",(int) $row["shipid"], $row["username"], $row["shipname"],(int) $row["shippower"]);
}
mysqli_free_result($result);
}
mysqli_close($con);
I have the following function which truncates a SQL Server varchar column and adds it to a string:
public void Pp()
{
strSql = #""; //query
using (SqlConnection conn = new SqlConnection(gstr))
{
try
{
SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
myDataSet = new DataSet();
da.Fill(myDataSet);
string specific = "";
string generic = "";
string strTemp = "";
foreach (DataRow r in myDataSet.Tables[0].Rows)
{
if (r["MessageText"].ToString().Length <= 65)
{
strTemp = r["MessageText"].ToString();
}
else
{
strTemp = TruncateLongString(r["MessageText"].ToString(), 65);
}
specific += "</b><span class='hoverText' title='" + r["MessageText"] + "'>" + strTemp + "...</span>";
strTemp = "";
}
lblMessage.Text = "<b>SPECIFIC MESSAGES:</b> <br />" + specific;
}
catch (Exception ce)
{
}
}
}
public string TruncateLongString(string str, int maxLength)
{
return str.Substring(0, maxLength);
}
If the r["MessageText"] contains an appostrophe, it cuts off anything after it. (full text: no way no way HERE we go again but this is not working. Is it? or Is it not? I can't be too sure though. Can someone please check.)
Here is an example of a live preview (the title is shown but gets cut off because of the apostrophe):
Here is the source (the apostrophe is shown in the purple box. Also the color coding gets out of whack due to the apostrophe, which means the code is not correct):
How can I ensure it doesn't escape any escape characters, e.g. ', /, \.
You need to encode the HTML first.
Call this.Server.HtmlEncode( str ). This will also protect against other special characters like & and <.
That said, you're using single-quotes for attribute delimiters but HtmlEncode only encodes double-quotes, so you need to change your code to this:
specific = String.Format( CultureInfo.InvariantCulture, #"</b><span class=""hoverText"" title=""" + this.Server.HtmlEncode( r["MessageText"] ) + """>" + strTemp + #"...</span>";
.Replace(" ' ", "\\' "); You will probably want to do the same with double quote as well.
So i'm using MySQL and C# in order to grab info from a database, but when i try to use that database info again in MySQL doesn't work because it seems to be adding a small space in front of the data. Let me show you what i mean.
php code that displays info on webpage
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$nick = $_POST["myform_nick"];
$sql = "SELECT Lin FROM scores WHERE name='$nick'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo $row["Lin"];
}
}
Then in C# it is like this...
IEnumerator LeagueCheck(){
var form = new WWWForm ();
form.AddField( "myform_nick", formNick );
WWW w = new WWW (URL, form);
yield return w;
if (w.error != null) {
print(w.error); //if there is an error, tell us
}
else {
print("League Check 1");
LinII = w.text; //here we return the data our PHP told us
print (w.text);
Lin = LinI + LinII;
w.Dispose(); //clear our form in game
StartCoroutine (LeagueCheck2 ());
}
}
Inside LeagueCheck2 it uses LinII to grab another thing from the database but it can't find it because it comes back like this
" Dominators"
instead of like this
"Dominators"
So my question is how can i get it to remove that little space in front of Dominators.
What i've tried to fix this
using System;
LinII = String.Trim(w.text);
LinII = w.text;
LinII.Trim();
LinII.Replace(" ","");
void Timer(){
LinII.Trim();
}
//In the IEnumator LeagueCheck();
Lin = LinI + LinII;
w.Dispose(); //clear our form in game
Trimer();
StartCoroutine (LeagueCheck2 ());
This should work if w.text and LinII are a string
if (w.error != null) {
print(w.error); //if there is an error, tell us
}
else {
print("League Check 1");
LinII = w.text.Trim(); //here we return the data our PHP told us
print (LinII );
Lin = LinI + LinII;
w.Dispose(); //clear our form in game
StartCoroutine (LeagueCheck2 ());
}
I'm fairly new to both Unity and PHP, and I am currently working on a project where I can parse data from a MySQL database to Unity, using PHP.
I initially wanted to try and enable a method where the user can perhaps change the php script and enable it to choose a different table of data, however I was advised that it may be safer to list all variables within the php script and call it from Unity accordingly;
Display.php
$table = mysql_real_escape_string($_GET['table'], $db);
if ($table == "shoes") {
$query = "SELECT * FROM `shoes` ORDER by `price` ASC LIMIT 10";
elseif ($table == "sneakers") {
$query = "SELECT * FROM `sneakers` ORDER by `price` ASC LIMIT 10";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$num_results = mysql_num_rows($result);
for($i = 0; $i < $num_results; $i++)
{
$row = mysql_fetch_array($result);
echo $row['shopname'] . "\t" . $row['price'] . "\n";
}
I'm having trouble calling the php and choosing the table that I want to select, I am pretty new to this, so I apologise if this seems completely incompetent to you guys.
Here is the my Unity Script;
HSController.cs
void Start()
{
StartCoroutine(GetScores());
}
// remember to use StartCoroutine when calling this function!
IEnumerator PostScores(string name, int score)
{
string hash = Md5Sum(name + score + secretKey);
string post_url = addScoreURL + "name=" + WWW.EscapeURL(name) + "&score=" + score + "&hash=" + hash;
WWW hs_post = new WWW(post_url);
yield return hs_post; // Wait until the download is done
if (hs_post.error != null)
{
print("There was an error posting the high score: " + hs_post.error);
}
}
IEnumerator GetScores()
{
gameObject.guiText.text = "Loading...";
WWW hs_get = new WWW(highscoreURL);
yield return hs_get;
if (hs_get.error != null)
{
print("There was an error getting the high score: " + hs_get.error);
}
else
{
gameObject.guiText.text = hs_get.text; // this is a GUIText that will display the scores in game.
}
}
Any help or a point in the right direction would be great!
Kind Regards
Let me try to rewrite this into a working example:
C#
void Start() {
StartCoroutine(GetData());
}
IEnumerator GetData() {
gameObject.guiText.text = "Loading...";
WWW www = new WWW("http://yoururl.com/yourphp.php?table=shoes"); //GET data is sent via the URL
while(!www.isDone && string.IsNullOrEmpty(www.error)) {
gameObject.guiText.text = "Loading... " + www.Progress.ToString("0%"); //Show progress
yield return null;
}
if(string.IsNullOrEmpty(www.error)) gameObject.guiText.text = www.text;
else Debug.LogWarning(www.error);
}
PHP
<?php
//DB connection goes here
if ($_REQUEST['table'] === "shoes") { //I'm using REQUEST instead of GET, so it will work with both GET and POST
$query = "SELECT * FROM `shoes` ORDER by `price` ASC LIMIT 10";
} elseif ($_REQUEST['table'] === "sneakers") {
$query = "SELECT * FROM `sneakers` ORDER by `price` ASC LIMIT 10";
}
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo $row['shopname'] . "\t" . $row['price'] . "\n";
}
?>
Hope this helps!
I am fairly new to c# and am working on a little project but got stuck on this. I have a file that contains some assembly code. I want my program to search this file for a string, actually a value right after my string. One of the strings i am searching for is:
setproperty QName(PackageNamespace(""), "font")
getlocal 4
pushint
My search code is this:
private void searchFile(String searchText)
{
System.IO.StreamReader reader = new System.IO.StreamReader(file);
String text = reader.ReadToEnd();
if (Regex.IsMatch(text, searchText))
{
MessageBox.Show(searchText + " was found in the given file", "Finally!!");
}
else
{
MessageBox.Show("Sorry, but " + searchText + " could not be found in the given file", "No Results");
}
}
//when i click a button//
searchFile(#"setproperty QName(PackageNamespace(""""), ""font"")
getlocal 4
pushint ");
I know that the string is in the file but the result comes up with not found. I don't know if it is the quotes or tabs or both that is causing this.
Here is part of the file:
getlocal 4
pushstring "Verdana"
setproperty QName(PackageNamespace(""), "font")
getlocal 4
pushint 16764170
setproperty QName(PackageNamespace(""), "color")
getlocal 4
pushbyte 12
setproperty QName(PackageNamespace(""), "size")
My second question is how can i get the value of the first int after my search result?
Thanks in advance.
-Leen
You should change your method like this:
private static string searchFile(String searchText)
{
System.IO.StreamReader reader = new System.IO.StreamReader("test.txt");
String text = reader.ReadToEnd();
int poz = text.IndexOf(searchText);
if (poz >= 0)
{
int start = poz + searchText.Length;
int end = text.IndexOf("\n", start);
Console.WriteLine(searchText + " was found in the given file", "Finally!!");
return text.Substring(start, end - start);
}
else
{
Console.WriteLine("Sorry, but " + searchText + " could not be found in the given file", "No Results");
return string.Empty;
}
}
The call:
string val = searchFile("setproperty QName(PackageNamespace(\"\"), \"font\")\r\n\r\n getlocal 4\r\n pushint ");
So I think you may be use to VB.net. C-based languages (like c#) used the backslash character "\" as an escape character.
So in a searching for a double-quote in a string you would need to escape it using \".
I believe what you're looking for is:
searchFile(#"setproperty QName(PackageNamespace(\"\"), \"font\")
getlocal 4
pushint ");
But this isn't really a regular expression, which is what the Regex class is meant for. So I would (well not really, I would clean it up a bit, like not mix my UI and bizlogic) do this:
// Added String as the function type so you can return the matched "Integer" as a string, you could always do a Int32.TryParse(...)
private String searchFile(String file, String searchText)
{
System.IO.StreamReader reader = new System.IO.StreamReader(file);
String text = reader.ReadToEnd();
int32 index = text.IndexOf(searchText);
if (index >= 0) //We could find it at the very beginning
{
MessageBox.Show(searchText + " was found in the given file", "Finally!!");
int32 start = index + searchText.Length;
int32 end = Regex.Match(text, "[\n\r\t]", index).Index; // This will search for whitespace
String value = text.Substring(start, end - start);
// Now you can do something with your value, like...
return value;
}
else
{
MessageBox.Show("Sorry, but " + searchText + " could not be found in the given file", "No Results");
return "";
}
}