我从这样的ajax调用php代码:
ajaxRequest.open("GET", "func.php" + queryString, true);
由于这是一个获取请求,任何人都可以通过简单地检查标题来看到它.传递的数据不敏感,但可能会被滥用,因为获取参数名称也很简单.
如何阻止直接访问http://mysite/func.php但允许我的ajax页面访问它?
我试图拒绝直接url访问一个调用ajax进行表单验证的php文件.我目前的代码一直工作,直到表单验证,然后它不起作用.我不想使用.htaccess.
当前代码:
<?php
$url = strtolower(basename($_SERVER['PHP_SELF'])); // Gets url (parent that uses the include)
$fil = strtolower(basename(__FILE__)); // gets filename (the included file)
if ($url == $fil){
    // if they are the same (file is accessed through url
    // redirect to forbidden page
    header("HTTP/1.0 403 Forbidden");
    exit;
}
// require my configuration
require_once("config.php");
// code to be executed for ajax validation
$username = $_POST['username'];
$conn = mysql_connect(DB_HOST,DB_USER,DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME,$conn) or die(mysql_error());
$query = mysql_query("SELECT * FROM " . TB_USER …