Creating your own Leaderboard / Highscores Easy and Free (PHP & MySQL) [UPDATED 2020]

33

Index

Attached Files

The following files have been attached to this tutorial:

.zip
.capx

highscore-example.capx

Download now 180.37 KB

Stats

43,670 visits, 115,173 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 26 Sep, 2015. Last updated 18 Dec, 2020

In this tutorial you will learn how to create your own Highscores for your games, using your own free server / database. Let's go!

Here's an example of how the leaderboard with high scores can look on your game. Image: Santa Hop!

What do you need?

- Your Construct 2 game (or C3)

- A server with PHP and MySQL databases (you can get a free server on 000webhost.com)

- Download the PHP7 files (phps.zip is for older versions of php5)

Let's start, step by step...

Registrating Your New Free Server

A server is like a computer on internet where you can save your own data: pictures, files, databases, scores, games and many more things...

000webhost.com offers free web hosting, with PHP and MySQL. It allows you to create 1 free website, host your files and you can create 2 free Databases.

Go to http://www.000webhost.com. Click "Sign Up" and register as a new user.

Fill your name, email and select a new password. When you are done you will receive a confirmation email, open your email account and Confirm your registration.

Back to 000webhost, select Create a New Site. Then type a name for your free domain, ie: duquekarlgames.000webhostapp.com

The next step is to access your dashboard.

Access your Dashboard

If you have already signed up and confirmed your free account, then you can log in to your account. To access your Dashboard, click Manage Your Website.

  • 112 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • how do I show example: Your Position In Rank: 99

    how do I show an error if the player puts a name that has already registered in the database?

    • Check the new attachments in this tutorial. I have uploaded a 'getrank.php' ;)

      To use it you call it like described in page 4 of this tutorial! :)

    • (Continued...)

      Changes in getrank.php:

      1)

      $sql = "SELECT COUNT(*) AS rank FROM scores WHERE score > " . $_GET['score'] . " GROUP BY name";

      2)

      In the while loop remove the "echo part". Outside of the loop, print i:

      echo ($i + 1);

    • There are a few ways to do this.

      Here is one idea:

      Check the current getScores.php. Copy the PHP file and rename to getRank.php

      When you call this PHP, check the DataBase and return a Number (the player RANK).

      How?

      When calling getRank you need to send a parameter. So the PHP code knows what to look for inside the DB.

      One option is to use a UserName or Email, whatever "unique" identifier of the current player. The PHP would be called like this: "www.URL.com/getRank.php?playername=john"

      It must return, for example: "99"

      MY CHOICE. The second option.

      Call the PHP using your best score, and the PHP code will check HOW MANY people have a better score than YOU.

      Example: "www.URL.com/getRank.php?score=75"

      The PHP code checks how many players have a better score than 75 and will return:

      98

      So you know that you are #99 in the rank

      • So if we have more than one person with same score, how will that work? Anyway you can also get sql to check not just score but also name?

        • If you have: Andy 30, Brad 25, Charly 25, David 20, Edgar 15, Frank 15, George 15, Helen 10

          - getrank.php?score=20 (for David) will return Rank #4 ("there are 3 better scores")

          - getrank.php?score=15 (for Frank) will return Rank #5 ("there are 4 better scores." This ignores other people with same score -15-. In other words, it ranks you as the best in your same score. With 15 points your rank could be #5, #6 or #7. You can also make ALL of them with score=15 rank "5th")

          This is like some top scores looking like this:

          1 Andy

          2 Brad

          2 Charly

          4 David

          5 Edgar

          5 Frank

          5 George

          8 Helen

  • [FIXED] The scripts save/get should be updated according to the latest PHP formatting rules. After fiddling after a bit I found that in the save.php you should remove the "id" parts when inserting the score.

  • Great tutorial for a refresher. Ive just finished reading through the tutorial and will soon be implementing the PHP.

  • I may be a bit late to the party, but I just finished this great tutorial by the letter, but the construct 3 part did not work. "work" means, no data was retreived after the ajax request. The phps work just fine, so I went bugfixing. as some users here seem to have similar problems, I want to share my solution:

    Instead of using a constant for the URL as suggested in the tutorial, i hardcoded it in the ajax request. this works like a charm. maybe it helps somebody else :-)

    thank you, dear author of this fine tutorial. well written and a huge help.

    • Hey! Thanks for writing and I am happy that you enjoyed the tutorial and it was helpful!! Go and make some super awesome games and share them here later! ;)

      • Hi, thanks fo the wonderful tutorial but I have an Issue, I hope you can help me by getting block issue in CORS

        how do I deal with it?

        how do I implement the code that allowing me to access in different site address?

  • Thankyou DuqueKarl for putting together this Tutorial. Very helpful

  • Load more comments (98 replies)