- Home /
Adding extra information to Server Side Highscores
Good Evening :-)
I have been struggling with the Server Side Highscores example on the unify community wiki for a good two weeks now, and I really, really need to get this working. The script works excellent, everything is fine and I'm loving it. The problems starts when I try to add additional information to the database.
The thing is, I would like to add two things to the database; the users email address and the date when the score was posted. But whenever I try to update the scripts to support this it wont upload to the database anymore. I have update the HSController, the addscores.php and the MySQL tables but I must be doing something wrong somewhere.
Could you maybe help me out in adding these fields to the script? It would be very much appreciated and you will of course be credited for helping :-)
http://www.unifycommunity.com/wiki/index.php?title=Server_Side_Highscores
Answer by DaveA · Jan 29, 2011 at 07:10 PM
You will of course add your extra info in the client script, on this line:
var highscore_url = addScoreUrl + "name=" + WWW.EscapeURL(name) + "&score=" + score + "&hash=" + hash + "&extra_stuff="+extra_stuff
Then you need to add to the PHP script:
$extra_stuff = mysql_real_escape_string($_GET['extra_stuff'], $db);
$query = "insert into scores values (NULL, '$name', '$score', $extra_stuff);";
And make sure you add fields to your database too.
You can test your PHP by just hitting it with a web browser.
Thanks for the answer. That's pretty much what I tried. I tried your code aswell just in case, and I'm having the exact same problem. I'm starting to wonder if it's the hash that fails?
$$anonymous$$ySQL
CREATE TABLE scores
( id
int(10) UNSIGNED NOT NULL AUTO_INCRE$$anonymous$$ENT PRI$$anonymous$$ARY $$anonymous$$EY, name
varchar(15) NOT NULL DEFAULT 'anonymous', extra_stuff
varchar(255) NOT NULL DEFAULT 'anonymous', score
int(10) UNSIGNED NOT NULL DEFAULT '0' )
Ok, I think I figured it out. It seems that if you add the fields in the wrong order in the database it will not work, once I had the exact same order in both url and the database then it worked.
It's still not accepting when I do it with the hash though. I commented out the hash stuff and got it to work, any ideas?
Answer by zbegra · Jan 29, 2011 at 11:54 PM
Allright, I finally got this working. I'll post the full code here so that others can benefit from this in the future
PHP:
<?php $db = mysql_connect('mysql.host.com', 'username', 'password') or die('Could not connect: ' . mysql_error()); mysql_select_db('database') or die('Could not select database');
// Strings must be escaped to prevent SQL injection attack.
$name = mysql_real_escape_string($_GET['name'], $db);
$score = mysql_real_escape_string($_GET['score'], $db);
$email = mysql_real_escape_string($_GET['email'], $db);
$date = date("d/m/Y");
$hash = $_GET['hash'];
$secretKey="replacewithyourown"; # Change this value to match the value stored in the client javascript below
$real_hash = md5($name . $score . $secretKey);
if ($real_hash != $hash) {
die('haha, try again sucker');
}
if($real_hash == $hash) {
// Send variables for the MySQL database class.
$query = "insert into scores values (NULL, '$name', '$score', '$email', '$date');";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
}
?>
Javascript:
private var secretKey="replacewithyourown"; // Edit this value and make sure it's the same as the one stored on the server var addScoreUrl="http://replacewithyourown.com/addscore.php?"; //be sure to add a ? to your url var highscoreUrl="http://replacewithyourown.com/display.php";
function postScore(name, score, email) { //This connects to a server side php script that will add the name and score to a MySQL DB. // Supply it with a string representing the players name and the players score. var hash=md5functions.Md5Sum(name + score + secretKey);
var highscore_url = addScoreUrl + "name=" + WWW.EscapeURL(name) + "&score=" + score + "&email=" + WWW.EscapeURL(email) + "&hash=" + hash ;
// Post the URL to the site and create a download object to get the result.
hs_post = WWW(highscore_url);
yield hs_post; // Wait until the download is done
if(hs_post.error) {
print("There was an error posting the high score: " + hs_post.error);
}
}
// Get the scores from the MySQL DB to display in a GUIText. function getScores() { gameObject.guiText.text = "Loading Scores"; hs_get = WWW(highscoreUrl); yield hs_get;
if(hs_get.error) {
print("There was an error getting the high score: " + hs_get.error);
} else {
gameObject.guiText.text = hs_get.data; // this is a GUIText that will display the scores in game.
}
}
MySQL Table:
CREATE TABLE `scores` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(15) NOT NULL DEFAULT 'anonymous',
`score` int(10) UNSIGNED NOT NULL DEFAULT '0' ,
`email` varchar(255) NOT NULL DEFAULT 'anonymous',
`date` varchar(15) NOT NULL DEFAULT 'anonymous'
Answer by lucas 1 · May 09, 2011 at 06:24 PM
Assets/all/score/score.js(36,13): BCE0005: Unknown identifier: 'Md5'. error how can souve?
Answer by n4zg · Jun 23, 2012 at 04:33 PM
I have set up the db and its working with Server Side Highscores script. i am getting stuck in adding data automatically eg when a coin is collected in a game a score is added live onto the sql . any suggestions ?
Your answer
Follow this Question
Related Questions
Android Mysql Database online 3 Answers
PHP HighScore Position 1 Answer
Unify "serverside highscores" - reading in a webpage 1 Answer
Using PHP to generate objects or list in Unity with C# 2 Answers