How to remember user progress through the application?

I ran into an architecture problem that I could not solve. I am developing a small game in PHP and Javascript, and I need to save user progress. Now the problem is that PHP cannot determine when the user wins the level: this is done in Javascript. Is there a way to keep the progress of the user when he wins the level?

For example, when a user wins level 1, he gains access to level 2. If he tries to access level 2 without completing the previous level, he will be redirected to the last completed one. In my controller, I did the following:

if (1 !== $id) { if ($app['session']->get('last_level') !== ($id - 1)) { // redirect the user } } 

Now I need a way to save the last_level value in the session - an operation that cannot be modeled by the user.

Any clues?

-one
source share
3 answers

You will want to perform various last_level calculations on the server. Thus, the user cannot hack JavaScript and send something in a specially designed form. Therefore, depending on what your storage system (KV store, Database, Textfile, etc.), put this value and extract it.

Ajax can help you, but it is not necessary. It depends on how your game is set up. But if they complete the level, the server must be notified.

+1
source

If the user completes the level and then remains on the same page, that does the extra work:

Then you probably want Ajax to send new level information to the server as soon as a new level is reached. In this way:

  • Use cookies to store your php session id

  • From your Javascript client, use Ajax to call the php url on your server whenever the user wins a new level. Send a new level as a parameter. Can use POST or GET, it does not matter.

  • The php program will receive the session identifier in the cookie, and a new level as the parameter. The php program looks up the user id from the session id and then saves the new database level.

  • The next time the php program main URL is called, it will be able to search for the user level.

If the user clicks the Next button to go to the next level: Use Javascript, not Ajax, to change the POST settings of the Next button.

You are using POST, not GET, as browsing the foo.com/game?level=5 URL is too obvious for people to trick your game into. POST does not display the level parameter in the url. Extra security: Add a checksum parameter.

If any of the above questions is not clear, ask in the comments or as a follow-up question.

+1
source

If you are happy to take the word javascript for it, that the level has been completed, just set the cookie from javascript.

operation that cannot be modeled by the user

Sorry, but if it is javascript that obscures when it completes the level, then there is nothing that could prevent the user from falsifying the results.

0
source

Source: https://habr.com/ru/post/1393413/


All Articles