0

So what I'm building is like a game, with a point system. You create an account and you start with 0 points. Then you can play a snake game(made with JS) to collect points, every time you finish the snake game, depending on your score you get specific amount of points.

Now those "points" are saved in a MYSQL database along with your account information ex. username, password, etc. Now because the snake game is made with JS, how will I be able to update MYSQL database and add points to the "points" variable in the database. Basically how will the server(PHP) know that the JS snake game has finished and that x points should be added in the MYSQL database.

1 Answers1

0

Check out AJAX for sending requests to the server during the game. This will element the need to know when the game ended.

EDIT

Your comment bring a all new issue of security which is not part of your original question and changes the entire scope of the discussion.

To put it simple, JavaScript is an exposed client side code, and the non existing way to 100% protect it has been answered multiple times, here's one for example. Sure, there are ways to make hacking very difficult, so perhaps you should ask yourself what is good enough for your application. If the ability to prevent players from hacking your game is the main goal, then you probably should not use JavaScript.

Nino
  • 702
  • 2
  • 6
  • 12
  • The problem is, if I use ajax with a post request, the same post request could be made with curl or some google addon, to gain free points or even changing the value to earn many points for free. – Kazem Abousetta Jun 18 '21 at 20:17
  • You can set a random Session ID for every new game. At the end of game you can send via Ajax the points gained and the Session ID and then check if the send ID is the same of Session ID before save the points in your db. After save the points you have to delete the session ID. If anyone tries to send extra points by curl or Ajax without an ID or using an old ID, return an error – Stefino76 Jun 18 '21 at 20:45