Unity
De Login() functie word aangeroepen in de Start() functie (kan overal aangeroepen worden):StartCoroutine(Login()); //call the function with StartCoroutine() this because the function is of 'IEnumerator' type
De login functie:
IEnumerator Login() { //the function has the return type: IEnumerator, this is needed when working with Yield and coroutine's
WWWForm form = new WWWForm(); //form which is handled as a normal html form by the php-page
form.AddField("inlogName", formNick); //add a field to the form in the format: AddField("name","value");
form.AddField("inlogPassword", formPassword);
WWW w = new WWW(URL, form); //a webRequest
yield return w; //yielding a variable will return this function (so other code can run), however intern it is waiting
//till 'w' is finish downloading, when 'w' is finished the code after the return will be called
if (w.error != null) {
Debug.Log(w.error); //log a possible error
} else {
formText = w.data; //here we collect the data from the website (this is the bron-code from the website, incl php echo's)
w.Dispose(); //clear the form in game
}
formNick = ""; //clean the inlog information
formPassword = "";
}
PHP
Een simpel php-mySql/database connectie script:<?php
$inlogName = $_POST["inlogName"];
$inlogPassword = $_POST["inlogPassword"];
$db_server = "*************";
$db_inlogName = "****";
$db_inlogPassword = "****";
$db_name = "SCORES";
echo " connection made ";
echo $inlogName;
echo $inlogPassword;
//make connection
$dbCon = mysql_connect($db_server,$db_inlogName,$db_inlogPassword) or die("Error connection to DB");
mysql_select_db("SCORES");
$query = "SELECT * FROM scores LIMIT 0 , 30";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
//print the information
while ($row = mysql_fetch_assoc($result)) {
echo $row['name']."\n";
echo $row['password']." - ";
}
echo "worked?";
?>
noot1:
Ten behoeve van het testen, stuur ik vanuit unity een aantal waarden mee, deze worden in deze versie nog niet gebruikt (inlogName, inlogPassword).
noot2:
Ik heb ten behoeve van de bescherming van mijn eigen database de inloggegevens hiervan verborgen door er '*' te tekenen.