Tracking your users with Ajax, PHP and MySQL

  • 16 favourites



12,565 visits, 34,990 views



This tutorial hasn't been translated.


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.

Final Notes

Security - This PHP script takes some precautions to make sure things like SQL injection aren't somehow added to your database (giving remote users access to your data). But with any prevention, there is no absolute guarantee. It also checks to make sure that the two variables PV and N are in the POST command. If either or both are absent, it does nothing. So, even if someone randomly stumbles onto your .php file, they'll have to know how to send a POST command, and know exactly which two variables to send in order to 'spoof' your data. Anyone who has a copy of your game and is determined to find this out, can. My advice is check your data often.

Data Storage - Once you learn a bit more about PHP and MySQL you can create a database and php script that, instead of logging each individual connection, keeps track of how many times a specific IP address has connected and update that record. If you have thousands of connections per day, that would be a better solution as logging each connection would fill up your database quickly.

Advanced ideas - By using Ajax, php and a MySQL database, you can easily track high scores, user names, passwords, registration information, any data your app/game can create, you can stuff it into a 'cloud' database by using Ajax, php and MySQL.

Getting data back - You can even use this method to check a database for information and then, send data back to your game/app. Using the php 'echo' command (which prints things to the browser), the Ajax.LastData will contain the contents of that echo. For that, you'll need to add a Tag to your post command and do an Ajax.onCompleted with the same tag.

How I'll be using this - I sell shareware where users pay via PayPal. PayPal has a feature called Instant Payment Notification (IPN). When someone sends me money, PayPal contacts one of my .php scripts with the payment information. My php script then adds that data to a MySQL database, generates a registration key based on the IPN number and then sends out an automated e-mail to the purchaser thanking them and giving them the registration information and it sends me an e-mail letting me know someone purchased my product.

This is my first tutorial so try not to laugh too hard. Hope you enjoyed it.


  • Order by
Want to leave a comment? Login or Register an account!
  • I have GoDaddy to host my website..

    Is it possible to create a BLANK MySQL database, then just copy it when I have a new user?

    I want each user to have their own MySQL database to save ALOT of data in.

  • Thanks a lot - VERY helpful! :)

    Just to note that lines 2-5 in your MySQL codearen't recognized as correct SQL code.

    Are they important? can you update how should they look now?