Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is what I do,</p> <ol> <li>On your website, create a secret string. I use the HMAC($_SERVER['REMOTE_ADDR'], key).</li> <li>Write the secret in a Javascript var.</li> <li>On the AJAX call, pass this string as a parameter.</li> <li>On the AJAX server, do the hash again. If it's matches the parameter, the call is from your page.</li> </ol> <p>EDIT: Code examples,</p> <p>In your website, you do this,</p> <pre><code>$key = 'supersecretkey'; // This is your security, don't expose this $nonce = rand(); $timestamp = time(); $signature = hash_hmac('sha1', $_SERVER['REMOTE_ADDR'] . $nonce . $timestamp, $key); </code></pre> <p>Print out the vars to the page,</p> <pre><code>&lt;script type="text/javascript"&gt; &lt;?php echo " var signature = '" . $signature . "';\n"; echo " var nonce = '" . $nonce . "';\n"; echo " var timestamp = '" . $timestamp . "';\n"; ?&gt; &lt;/script&gt; </code></pre> <p>When you make AJAX call, pass the 3 parameters to the server,</p> <pre><code> http://example.com?signature=...&amp;nonce=...&amp;timestamp=... </code></pre> <p>On the AJAX server, do the calculation again,</p> <pre><code>$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. </code></pre> <p>You can also chech timestamp for currency and nonce for replay (need session or data store).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload