A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: PHP/MySQL Highscore Tutorial

  1. #1
    Flash Developer on a break
    Join Date
    Jun 2001
    Location
    The Netherlands
    Posts
    1,370

    PHP/MySQL Highscore Tutorial

    Changes March 26, 2007: Reposting my old Highscore Tutorial. Because my original one is pretty old, but still useful, apparently I cant edit my original thread anymore as it seems to be resolved/archived. I get asked about my tutorial now and then by email so I decided I should just repost this.
    I also updated the zip link. Please note that the original Highscore Tutorial in the tutorials section on the website has been outdated since 2002. Here's the original thread:
    http://board.flashkit.com/board/show...hreadid=297011

    v1.3 changes November 26, 2002: _root.add_pass in the AS that inputs new data into the database changed to _root.pass.

    __________________________________________________
    Ok... Lets get started. This is my first tutorial, so enjoy!

    http://rapidshare.com/files/22827601/highscore.zip.html

    Highscore Tutorial

    One of the little things for a nice Flash game is a highscore list. It's making people to go back to your game and play it again to win from the competition. It's a way of competition and interactivity between the players to do their best and win as much points as they can.

    Things you need
    1. A webserver enabled with PHP and MySQL.
    Almost all unix/linux webservers have these two, because they are free for everyone. If you don't know if you have PHP4/MySQL enabled, contact your Hosting Company.
    2. Notepad.
    Or a similar text editing program.
    3. A FTP program to upload your files.
    CuteFTP or WS_FTP are two examples.
    4. The files that come with this tutorial. (highscore_tut.zip)
    5. Your nice Flash game.
    Duh..

    The first few things.
    Open '_data.php'.
    This is rather important. You need to change a few things here. You need to input the right usernames/passwords/game name/other things.
    You will have to change at least numbers 1 to 6. The numbers 1-3 are the few things you need to enter to make contact with the MySQL database. You've got those from your Hosting Company. If not, ask them.
    7-14 are optional, they don't need to be changed to get it all working. All things are rather straigtforward.

    Upload _data.php/get_score.php/install.php/new_score.php to your website's root directory.
    Run the install.php script. When it sais 'Done..', you can delete the install.php file from your website.
    Done.

    Why PHP/MySQL?
    PHP, PHP Hypertext Processing is a free server-based scripting language that is free and provided by a lot webserver/hosting companies. Other than that, it's a very fast language, faster then ASP(from Micro$oft). MySQL is the perfect database partner for PHP. Together they can be fast and above all secure.

    Caching
    You will notice that sometimes I have added a random variable. This is to prevent caching. It's a common problem of using PHP in combination of flash. To add a random number to the url, you make sure the output of the script is retrieved from the server instead of your hard-drive.

    Adding new people to the scores.
    Ok, now we are going to start with the Flash part.
    When you want to send your highscores from Flash to the MySQL server, you'll need this Actionscript:

    Code:
    	_root.pass = "{same as $add_pass in _data.php}";
    	_root.name = _root.variableYouUsedInTheGameForThePlayersName;
    	_root.score = _root.variableYouUsedInTheGameForThePlayersScore;
    	somevar = random(9999);
    	loadVariablesNum("new_score.php?" add random(999), 0, "POST");
    That's it! The PHP script itself makes sure the player gets added to the database and all other things you don't need to worry about.

    Showing the Highscore in Flash.
    This is rather easy. Make 10 variable text boxes in a column.(Or the number you used in the _data.php file as $st_size.) These dynamical text boxed are named 'name1' to 'name10'. (Or different, again corresponding to the Standard Size inputted in _data.php.)
    Then make 10(Or blab, bla, you know..) dynamical variable text fields next to each corresponding text box called 'score1' to 'score10'.

    I am not taking this anyfurther, but you can understand that you can make the looks of your Highscore list somewhat nicer than standard..

    Now use the following Actionscript code to load the variables from the database back into Flash:
    somevar = random(9999);
    loadVariablesNum("get_score.php?flash=1&" add random(999), 0);

    Now the variables get loaded into the _root. But if you have your Highscore list in a movieclip, you should use this code:
    somevar = random(9999);
    loadVariables("get_score.php?flash=1&" add random(999), "_root.scoretableOrAnyOtherMCname");


    The location of the Flash game's SWF file and the location of the Highscore scipts must be at the same domain. (And in these Actionscript code, it must be in the same folder as well.) This is a standard security feature from Macromedia Flash. It can't be changed, live with it.

    Including the Highscore list on your webpage.
    It is possible to include a HTML highscore list on your webpage. This is realy nice for your site's visitors.

    Your page where you want it to show must be a PHP page as well. We take 'index.php' as an example. The $dis_file variable in _data.php must be the same as this page. Now, where ever you want the Highscore table to appear on your page, you include this:


    Code:
    	<?
    		include("http://www.your-webserver.com/get_score.php";);
    	?>
    You can change some graphics from the HTML table in _data.php if you want, but look out to only use ' in the code instead of ". That is VERY important.

    Ending
    Well, that's about it. You can use this to make your own nice Highscore for every game you want.

    I am reposting my old Highscore Tutorial. Because my original one is pretty old, but still useful, apparently I cant edit my original thread anymore as it seems to be resolved/archived. I get asked about my tutorial now and then by email so I decided I should just repost this.
    I also updated the zip link. Please note that the original Highscore Tutorial in the tutorials section on the website has been outdated since 2002.

    That's it. If you encounter any problems or so, you can discuss them here

    Jeroen

    | "Keep Flashing!"-Jeroen
    PHP multiplayer? Check this thread: PHPgateway

  2. #2
    Who needs pants? hooligan2001's Avatar
    Join Date
    Apr 2001
    Location
    Somewhere
    Posts
    1,976
    You must have seen all the posts about your high score system recently

    [h]ooligan

  3. #3
    Flash Developer on a break
    Join Date
    Jun 2001
    Location
    The Netherlands
    Posts
    1,370
    Nope actually ^^;;

    I havent had much time lately to work with Flash lately or check these boards. :/

    Edit: I'll see if I can find the time to update it to the latest AS version later.
    Last edited by jeroen84; 03-27-2007 at 04:18 AM.

    | "Keep Flashing!"-Jeroen
    PHP multiplayer? Check this thread: PHPgateway

  4. #4
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,379
    Caching
    You will notice that sometimes I have added a random variable. This is to prevent caching. It's a common problem of using PHP in combination of flash. To add a random number to the url, you make sure the output of the script is retrieved from the server instead of your hard-drive.
    I do not actually use your tutorials, but could the lack of using this in my games because the cause for some people not seeing the updated hi-score list right away? The solution was always to "play again" and it would then show the last score added, but not the new score.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  5. #5
    Flash Developer on a break
    Join Date
    Jun 2001
    Location
    The Netherlands
    Posts
    1,370
    Yes, that's correct. Sometimes Flash just uses the page that is in your Temporarily Internet Files instead of retreiving it from the server again.
    By adding a random variable to the url, you can be sure that a new request is send to the server and updated data is send back.

    | "Keep Flashing!"-Jeroen
    PHP multiplayer? Check this thread: PHPgateway

  6. #6
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,379
    So the random range should be pretty high then? If a game has the potential to get popular, it could mean that a person still might not get an updated score list if they play often enough and do not clear their cache, correct?
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  7. #7
    Who needs pants? hooligan2001's Avatar
    Join Date
    Apr 2001
    Location
    Somewhere
    Posts
    1,976
    You could use new Date().getTime() instead which will pretty much ensure no duplicate will be made in cache seeing that no date and time combination happens twice.

  8. #8
    Senior Member webgeek's Avatar
    Join Date
    Sep 2000
    Posts
    1,356
    Thanks for posting this. It will help a lot of people and maybe aleviate a lot of the questions on the board. One suggestion though, please correct the SQL Injection vulnerabilities in the script to make it more secure. Specifically, things like:
    PHP Code:
    $result5 mysql_db_query($db_name,"SELECT score from $db_table WHERE name=\"$name\""); 
    Thanks!

  9. #9
    Junior Member
    Join Date
    Mar 2007
    Posts
    3
    hello,
    im having trouble for some reason. My game is wings.swf and its in the root folder of my website Should it be The $game_name = "wings"; or The $game_name = "wings.swf"; Ive tried both and it still doesnt work but i guess it would be good to get this one right first. thanks

  10. #10
    wow, real cool. But it's a little aged, ur AS is in AS1? add has a total new function now. You should probably test your tutorials before posting.
    PS! Even if I fixed the add, it still didn't work on my case.

  11. #11
    Quote Originally Posted by lennu
    wow, real cool. But it's a little aged, ur AS is in AS1? add has a total new function now. You should probably test your tutorials before posting.
    PS! Even if I fixed the add, it still didn't work on my case.

    PPS! mention in the _data.php that $game_name = "must be with out spaces"

  12. #12
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,379
    Quote Originally Posted by lennu
    PPS! mention in the _data.php that $game_name = "must be with out spaces"
    That might be a good point, but I'm pretty sure that about 99% of people type everything code related in camel-casing.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  13. #13
    I tried to do all by tis tutorial and it didn't work can I get the flash source files?

  14. #14
    Junior Member
    Join Date
    Apr 2007
    Posts
    5
    This is not really a tutorial, but more like documention for your code. But anyway, thanks, I learned some stuff from the source. My favorite way of learning is from an example. I am wondering though if there isn't a better way to remove the highscores that fall off the list? Like if you only want a 100 highscores on your list, you need to keep only a 100, so the database doesn't just keep growing. It seems that your code goes through the records one by one and deletes the ones, that are after the limit.

    Is there a more efficient way in mySql to truncate a table to only a certain number of records, sorted by eg. score?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center