确保仅通过 AJAX 表单访问页面以便对其进行处理的最佳方法是什么?

What is the best way to ensure a page was only accessed through the AJAX form in order to process it?

我在 index.php 上有一个 AJAX 表单,它通过 order.php 处理。

如何确保在处理之前通过 AJAX 表单访问 order.php,以便我可以忽略对 order.php 的直接访问请求?

我尝试使用会话变量并确保在处理 order.php 之前设置 index.php 中设置的会话变量,但是如果有人先转到 index.php 然后再转到 order.php,它仍然会处理 order.php,即使它不是通过 AJAX 表单。

if (isset(($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') && $_SERVER['HTTP_X_REQUESTED_WITH']))
{
  // Code that will run if this file called via AJAX request
} 
else 
{
  // Code that will run when accessing this file directly
}