Highscore Example
Run Sample Java App | View PHP Source
This is a very simple example of how to set up a highscore table in a game.
Using PHP you can interact with a MySQL database to store and view the
scores.
Settings
Take a look at the script source for the PHP
script that interacts with the MySQL database. There are a few settings
you have to adjust before you can use the script. After you set up
the database on your webserver, change the settings so the PHP script
knows where to go to store data. Here's an explanation of the settings:
$GLOBALS["access_code"] - this is just a string of random numbers
that you choose. It can be as long or short as you want. Your Java game must pass this
code in the script URL every time you query the script. If you choose
not to pass the code to the URL or use the wrong code, the PHP script will
ignore any command you give. This is just used to keep the database secure.
$GLOBALS["db_name"] - this is the name of the database you set up on your server.
Typical servers assign database names with your user prefix. If you login to your site
with the user name bob, then most servers assign the database name to
bob_dbname.
$GLOBALS["table_name"] - this is the name of the table used in your database. The name
doesn't matter as long as it doesn't clash with a table name you already use.
$GLOBALS["view_user"] - view_user is a MySQL user with access to only one
MySQL command: SELECT. This way if a hacker somehow figured out what MySQL username/password
was being used to display the highscore data, they wouldn't be able to change the data.
$GLOBALS["view_pass"] - the password for the view user.
The rest of the $GLOBALS are not settings (so don't touch those).
Connecting to the Script
Because all data passed to the script via GETVARS, we can query the script just by using
a standard URL address. Your Java application only needs to use the URL class to
point to the script. Then just use the openStream() to get
an InputStream object to read the data from the script after the command has been executed.
For example, to receive the list of players and their scores currently in the database:
URL url = new URL("http://yoursite.com/highscore.php?action=list&access_code=1234");
InputStream in = url.openStream();
BufferedReader b = new BufferedReader(new InputStreamReader(in));
// now you can use b.readLine() to read the data from the script until it returns null
Commands
There are only 3 commands you can pass to the script: install, submit, and list.
You specify what command you want by passing action=cmd to the URL of the script.
install - this installs the script. Make sure you have already set up your database on your server
and all of the settings are correct before executing this command. You can also use this command if you want to reset
the highscore table.
Additional variables needed are:
admin_user - the MySQL user who has rights to create and delete databases
admin_pass - the administrator's password
Example: http://yoursite.com/highscore.php?action=install&admin_user=foo&admin_pass=bar&access_code=1234
submit - this submits a new score to the database.
Additional variables needed are:
admin_user - the MySQL user who has rights to insert data into a database. This does not have to be the
same user that has rights to create and delete databases. You can simply use another MySQL user that has permissions
to use the INSERT and SELECT commands.
admin_pass - the password to the MySQL user.
name - the name of the player in the game. Max length is 25 characters.
score - the score of the player.
Example: http://yoursite.com/highscore.php?action=submit&admin_user=foo&admin_pass=bar&name=Bob&score=100&access_code=1234
This will the user "Bob" with a score of 100 to the database.
list - lists the highscore data, line by line. You can use a BufferedReader object to read
the data line by line. The format is the player name and score separated by a space. Be aware that player names may
have a space in them so you need to separate the player name and the score using the last index of space in the
String. No additional variables are needed aside from the required access_code.
Example: http://yoursite.com/highscore.php?action=list&access_code=1234