0

Currently when I want to get data from my database through ajax I use an external php file which just grabs the data from the database with mysqli and echoes the results and sends the data to ajax.

I now want to change that into a more effective solution. I thought I would first change it so that the php file outputs json data for future projects and easier data managament. But I don't want everyone else to be able to access this data directly other than through my website. And since you can see the ajax connection by inspecting the website it's really easy to find out.

I don't want it to become a place where everyone can just access the raw json data and build their own applications/websites from.

What other solutions are there? Only my web server should be able to access it.

Oskar Persson
  • 6,605
  • 15
  • 63
  • 124

2 Answers2

1
  1. Estabilish a session when the user access your site
  2. Check the session before answering any AJAX call

Note, this isn't bombproof, but only a starting point.

STT LCU
  • 4,348
  • 4
  • 29
  • 47
0

There's no way to hide json data from a user who is allowed to use the document which requesting it. In other words: you cant really deny the inspection of the data if arrived to browser by an allowed way (using your site normal way). However, you can make leeching difficult. If you make the leeching the process longer than time needed to build a self-made database, you win.

  1. Reduce the amount of data you sending in one request: don't send more data than you actually displaying.
  2. Split data to paged list and detail views. So for accessing the complete data, the leecher has to make lot-lot requests. You can then limit calls/period/user (using session).
  3. Identify users, if your project allows it. There are many methods to reduce the unwanted registrations. Also, you can make the data access limiting (above) more efficient (you can connect the counters to the user).
  4. You can use key-based encrypting. You can find a possible solution here: Simple Javascript encrypt, PHP decrypt with shared secret key Obviously, the browser will decrypt the data, therefore you need send the key to it. That means: the process will be reproducible, however takes much more time than a simple inspection. You can variate the parameters of encryption to make it even more difficult.

You can do some additional steps to make scripted leeching a bit more difficult. However, these will not make the browser-inspection harder:

  1. Check referrer you got from request header. It should match to your document's url.
  2. Don't send anything if the X-Requested-Width header doesn't set to 'XMLHttpRequest' ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'). This is safe, all browser sending it on ajax requests.
  3. You can add an unique, one-time parameter to the ajax url. You can pass the parameter (or the whole url) by PHP, using simple "script" tags (var param='uniqparam123';). You will need a fast solution to temporary store the sent keys for a while. Once an ajax request incoming, you can match the key, remove from list, then send the data out.

Hope you can cherry-pick some of these.

Community
  • 1
  • 1
GT.
  • 91
  • 6