<p>I'm using jquery ajax to post updates back to my server. I'm concerned about making sure I have put in place appropriate measures so that only my AJAX calls can post data.</p>

<p>My stack is PHP on Apache against a MySQL backend.</p>

<p>Advice greatly appreciated!</p>

[{'answer_id': 38444, 'author': 'conmulligan', 'author_id': 1467, 'author_profile': 'https://Stackoverflow.com/users/1467', 'pm_score': 3, 'selected': False, 'text': '<p>Use sessions to ensure that any Ajax posts are done in an authenticated context. Think of your Ajax code as just another client to your server, it becomes easier to tackle authentication issues that way.</p>\n'}, {'answer_id': 38446, 'author': 'John', 'author_id': 2168, 'author_profile': 'https://Stackoverflow.com/users/2168', 'pm_score': 6, 'selected': True, 'text': '<p>Any request that the AJAX calls in your pages can make can also be made by someone outside of the application.  If done right, you will not be able to tell if they were made as part of an AJAX call from your webapp or by hand/other means.</p>\n\n<p>There are two scenarios I can think of which you might be talking about when you say you want to make sure that only your AJAX calls can post data: either you don\'t want a malicious user to be able to post data that interferes with another user\'s data or you actually want to restrict the posts to being in the "flow" of a multi-request operation.  </p>\n\n<p>If you are concerned with the first case (someone posting malicious data to/as another user) the solution is the same whether you are using AJAX or not -- you just have to authenticate the user through whatever means is necessary -- usually via session cookie.</p>\n\n<p>If you are concerned with the second case, then you are going to have to do something like issue a unique token at each step of the process, and store the expected token on the server side.  Then when a request is made, check that there is a corresponding entry on the server side for the action that is being taken and that the expected tokens match and that that token has not been used yet.  If there is no, you reject the request, if there is, then you mark that token as used and process the request.</p>\n\n<p>If what you are concerned about is something other than one of these two scenarios then the answer will depend on more specifics than you have provided.</p>\n'}]
['https://Stackoverflow.com/questions/38421', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2362/']