I have in my website a PHP page which retrieves data from my database to be presented in my website. This page is called via AJAX. How can I restrict the access to it only f
This is what I do,
EDIT: Code examples,
In your website, you do this,
$key = 'supersecretkey'; // This is your security, don't expose this
$nonce = rand();
$timestamp = time();
$signature = hash_hmac('sha1', $_SERVER['REMOTE_ADDR'] . $nonce . $timestamp, $key);
Print out the vars to the page,
When you make AJAX call, pass the 3 parameters to the server,
http://example.com?signature=...&nonce=...×tamp=...
On the AJAX server, do the calculation again,
$key = 'supersecretkey'; // This is your security, don't expose this
$nonce = $_REQUEST['nonce'];
$timestamp = $_REQUEST['timestamp'];
$signature = hash_hmac('sha1', $_SERVER['REMOTE_ADDR'] . $nonce . $timestamp, $key);
if ($signature == $_REQUEST['signature'])
// the call if from my page.
You can also chech timestamp for currency and nonce for replay (need session or data store).