<p>What's the best practice for making sure that certain ajax calls to certain pages are only accepted from authenticated users?</p>

<p>For example:</p>

<p>Let's say that I have a main page called <strong>blog.php</strong> (I know, creativity abounds).  Let's also say that there is a page called <strong>delete.php</strong> which looks for the parameter <strong>post_id</strong> and then deletes some entry from a database.</p>

<p>In this very contrived example, there's some mechanism on blog.php which sends a request via ajax to delete.php to delete an entry.  </p>

<p>Now this mechanism is only going to be available to authenticated users on blog.php.  But what's to stop someone from just calling delete.php with a bunch of random numbers and deleting everything in site?</p>

<p>I did a quick test where I set a session variable in blog.php and then did an ajax call to delete.php to return if the session variable was set or not <strong><em>(it wasn't)</em></strong>.</p>

<p>What's the accepted way to handle this sort of thing?</p>

<hr>

<p>OK.  I must have been crazy the first time I tried this.</p>

<p>I just did another test like the one I described above and it worked perfectly.</p>

[{'answer_id': 15368, 'author': 'pix0r', 'author_id': 72, 'author_profile': 'https://Stackoverflow.com/users/72', 'pm_score': 4, 'selected': True, 'text': '<p>You were correct in trying to use session variables.  Once your user authenticates, you should store that information in their session so that each subsequent page view will see that.  Make sure you are calling <code>session_start()</code> on both pages (blog.php and delete.php) before accessing $_SESSION.  Also make sure you have cookies enabled -- and if not, you should pass an additional parameter in the query string, usually PHPSESSID=&lt;<code>session_id()</code>>.</p>\n'}, {'answer_id': 16962, 'author': 'Andy', 'author_id': 1993, 'author_profile': 'https://Stackoverflow.com/users/1993', 'pm_score': 2, 'selected': False, 'text': '<p>It is not recommended that you rely on sessions for authentication without taking additional actions. \n<a href="http://shiflett.org/articles/the-truth-about-sessions" rel="nofollow noreferrer">Read more on</a>.</p>\n'}]
['https://Stackoverflow.com/questions/15366', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/305/']