- Home /
PHP - Unity Scoreboard
Hello,
So, I'm working on a score board that are stored in an SQL database. All I want to do, is to grab two variables from this database:
Name | Score
So, Unity(JS) > PhP > SQL > PhP > Unity(JS).
This is my script for Unity that connects to my PHP script on my server:
private var highscore_url = "http://MyWebSite.com/game/connent.php";
var playerName = "alex"; // to be an array??
var playerScore = -1;
function Start() {
var form = new WWWForm();
form.AddField( "PHPplayerName", playerName );
var w = WWW(highscore_url, form); //here we create a var called 'w' and we sync with our URL and the form
yield w; //we wait for the form to check the PHP file, so our game dont just hang
if (w.error != null) {
print(w.error); //if there is an error, tell us
} else {
print("Test ok");
formText = w.text; //here we return the data our PHP told us
w.Dispose(); //clear our form in game
}
}
var formText = "";
private var textrect = Rect (10, 150, 500, 500);
function OnGUI(){
GUI.TextArea( textrect, formText );
}
So, what I want my PHP to do (for now) - is to retrieve the names from SQL and give them all to Unity to put in an array of some kind. Here is my PHP script (I know -- Unity forum only!) - connent.php:
<?php
// Connect to database
mysql_connect("localhost", "username", "password") or die(mysql_error());
echo "Connected to MySQL"."\n";
mysql_select_db("database") or die (mysql_error());
echo "Connected to Database\n"."\n";
$playerName = ($_POST["PHPplayerName"]);
$score = ('score');
$query = "SELECT * FROM highscores";
$fetch = "SELECT * FROM highscores ORDER BY score desc LIMIT 10";
$result = mysql_query($fetch) or die (mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['player']." - ".$row['score'];
echo "\n";
$playerName = $row['player'];
}
?>
Answer by Oliver Eberlei · Jun 12, 2011 at 10:32 AM
Since you already get the output from your PHP Script with this code
formText = w.text;
you can split that text into the various components like this
string[] playerEntries = formText.Split( '\n' );
foreach( string entry in playerEntries)
{
string[] playerData = entry.Split( '-' );
string playerName = playerData[ 0 ];
int playerScore = System.Convert.ToInt32( playerData[ 1 ] );
}
You also have to remove the spaces between the "-" in the PHP output so you can split the string properly
echo $row['player']."-".$row['score'];
And remove the previous echo's from the mysql database.
keep in $$anonymous$$d that the splitting doesn't work if the user may enter every kind of characters (including '-').
also, every one who's only a bit familiar with http requests can forge his own by looking at the url you're asking for. an adress like **http://$$anonymous$$yWebSite.com/game/connent.php?PHPPlayerName=me≻ore=5000** is a really easy one to mess with your score board.
there are several solutions for this, one might be adding an additional parameter that's the hash of the other parameters + a secret string in the code.
I know I'm asking for too much here - could you please explain this hash security parameter? Cheers
For this script this is not neccessary, since you are only getting the highscores from the server.
But if you use a similar script to insert scores into your database you would need some protection against cheaters.
You can see an example of it here http://www.unifycommunity.com/wiki/index.php?title=Server_Side_Highscores
Basically what you do is $$anonymous$$D5() the score together with a secret phrase, and then you do the same in your PHP script and check if the two hases match. If not, someone has tampered with the posted scores
Okay - is the code above in JavaScript? And do I put that in an update function? Thanks
It's C# and you put it right after formText = w.text; Never used JavaScript in Unity, but translating from JS is not that hard, shouldn't be too bad to convert it to JS.
Your answer
![](https://koobas.hobune.stream/wayback/20220612103202im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Using Unity to connect to a Database 3 Answers
A node in a childnode? 1 Answer
unity + php get Authentication 0 Answers
Players online counter 1 Answer
Retrieve a value from a PHP file and print it on the Unity3D console. 1 Answer