connect to mySQL via php

hey folks!

after reading the tutorial of xadeck and the mandownlogin system i started to create my own in c#.
so the annoying thing is to become the user valid. i´m not sure what i miss because after testing and playing around it was finally working well, but just for one user…
after deleting the valid user and insert the same username/pass to the db again, it didn´t work anymore…im not sure why. the md5 key is ok, the username is ok…
well, here are the scripts:

using UnityEngine;
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;


public class Login : MonoBehaviour {
	private string console = "";  /// for debug
	private string createuserUrl = "http://www.xxxxxxxxxxxxxx.xxx/login/cuser.php";
	private string getuserUrl = "http://www.xxxxxxxxxxxxxxxx.xxx/login/getuser.php";
	string getuser_url = "";
	public string account = "";
	public string passwordToEdit = "";
	private bool userValid = false;
	private bool success = false;
	
	IEnumerator checkAccount(){

		getuser_url = getuserUrl + "?account=" + account + "&password=" + Md5Sum(passwordToEdit);
		Debug.Log("used md5 "+ Md5Sum(passwordToEdit));

		WWW cu_get = new WWW(getuser_url); ///call php
		yield return cu_get; /// wait for response
		
		
		if(cu_get.error != null) {
			console = "There was an error checking your account and password: " + cu_get.error;
		}
		else{
			console = "connected - verifying...";
			if(cu_get.text == "valid"){
				console = "Welcome back, " + account;
				Debug.Log(console);
				yield return new WaitForSeconds(2.0f);
				userValid=true;
				success = true;
			}
			else if(cu_get.text == "invalid"){
				console = "wrong username or password";
				userValid = false;
				success = false;
			}
			else{
				console = "ZONK...";
				userValid = false;
				success = false;
			}
		}
	}

	void OnGUI() {
		
		account = GUI.TextField(new Rect(0, 20, 200, 20), account, 25);
		passwordToEdit = GUI.PasswordField(new Rect(0, 45, 200, 20), passwordToEdit, "*"[0], 25);
		
		if(GUI.Button(new Rect(0, 70, 100, 20), "Login")){
			StartCoroutine("checkAccount");
		}
		GUI.Label(new Rect(0,0,200,20), console.ToString());
		
	}	
	void CantLogin(){
		success = false;
	
	}
	public  string Md5Sum(string strToEncrypt){
		
		System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
		byte[] bytes = ue.GetBytes(strToEncrypt);

		// encrypt bytes
		System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
		byte[] hashBytes = md5.ComputeHash(bytes);

		// Convert the encrypted bytes back to a string (base 16)
		string hashString = "";

		for (int i = 0; i < hashBytes.Length; i++)
		{
			hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
		}

		return hashString.PadLeft(32, '0');
	}

}

the PHP

<?php

	include("db_config.php");

    $account = $_REQUEST['account'];
    $password = $_REQUEST['password'];
    $qry="SELECT * FROM Accounts WHERE account ='".mysql_real_escape_string($account)."'
   AND password = ' " . $password . " ' "; 
	$result=mysql_query($qry);
	//var_dump($result);
 	if (mysql_num_rows($result)>0) {echo "valid";}
 	else { echo "invalid";}
?>

the db_config works as intend…i already had have a valid user and use this script often

the SQL

CREATE TABLE IF NOT EXISTS `Accounts` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `account` varchar(30) NOT NULL,
  `password` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;


INSERT INTO `Accounts` ( `id` , `account` , `password` )
VALUES (
1, 'dev', MD5( '1234' )
)

this is stored as

(1, 'dev', '81dc9bdb52d04dc20036dbd8313ed055');

unity tells me, that the md5 key im generating in unity is: 81dc9bdb52d04dc20036dbd8313ed055
line 20 : Debug.Log("used md5 "+ Md5Sum(passwordToEdit));
that´s also correct…

so…what could be wrong?

What do you receive in the www ? Can you debug that and post it…

Debug.Log(cu_get); →
UnityEngine.WWW
UnityEngine.Debug:Log(Object)
c__Iterator2:MoveNext() (at Assets/002_Scripts/login/Login.cs:25)

Debug.Log(cu_get.text); →
invalid

Debug.Log(getuser_url); →
http://www.xxxxxxxxxxxxxx.xxx/login/getuser.php?account=dev&password=81dc9bdb52d04dc20036dbd8313ed055
UnityEngine.Debug:Log(Object)
c__Iterator2:MoveNext() (at Assets/002_Scripts/login/Login.cs:26)

(the url is correct)

try and debug the 2 hashes in the PHP, check if they are the same.
the hash received from the client in the php with the one in the DB.
i’m guessing it’s a space added somewhere…

omfg… not really…i´m searching the error now for such a long time…and this was the last fcking idea i had think of^^…and finally you are right…it was a fcking space…wtf^^
thx for helping me out :slight_smile:

§$%@! :wink:

yeah, i had my share with PHP lol
pain in the butt