Note that there are some explanatory texts on larger screens.

plurals
  1. POToken does not work
    primarykey
    data
    text
    <p>I want to use a token to secure my pages which are loaded via an AJAX-request (the basic idea is to prevent direct access of that page). So far so good.</p> <p>The problem is that both tokens do not match. It doesn't work.</p> <p>To create my token I use this function:</p> <pre><code>function generateFormToken($form) { $token = md5(uniqid(microtime(), true)); $_SESSION[$form.'_token'] = $token; return $token; } </code></pre> <p>To verify them I use this function:</p> <pre><code>function verifyFormToken($form) { if (!isset($_SESSION[$form.'_token'])) { return false; } if(!isset($_POST['token'])) { return false; } if ($_SESSION[$form.'_token'] !== $_POST['token']) { return false; } return true; } </code></pre> <p>In my form contact_form.php I create my token:</p> <pre><code>$newToken = generateFormToken('contactForm'); </code></pre> <p>Then I put that generated token into a hidden field:</p> <pre><code>&lt;input type="hidden" name="token" value="&lt;?php echo $newToken; ?&gt;" /&gt; </code></pre> <p>In my header of my website I start a session:</p> <pre><code>if (session_id() === '') { session_start(); } </code></pre> <p>The reason why I'm checking if there's already another session is because on my index.php-page there's a cookie set (without the check I get a PHP-error message saying that there's already a session):</p> <pre><code>if (isset($_COOKIE['ava_lastpage'])) { $prev_page = secure($_COOKIE['ava_lastpage']); } // Set current page if (!isset($_GET['task']) || (isset($_GET['task']) &amp;&amp; $_GET['task'] != 'register' &amp;&amp; $_GET['task'] != 'validate' &amp;&amp; $_GET['task'] != 'login' &amp;&amp; !isset($_GET['status']) || isset($_GET['status']) &amp;&amp; $_GET['status'] != 'reg_complete')) { setcookie('ava_lastpage', curPageUrl(), time()+60*60*24*100, '/'); } </code></pre> <p>This is my AJAX-request (contact.php is the file that shall be protected via a token):</p> <pre><code>$.ajax({ type:'POST', url: '&lt;?php echo $setting['site_url'] .'/';?&gt;includes/misc/contact.php', data:$('#contactForm').serialize(), }); </code></pre> <p>In my file contact.php I try to validate the token (which doesn't work):</p> <pre><code>if (verifyFormToken('contactForm')) { bla bla bla... </code></pre> <p>Please - any help?</p> <p>Update: The functions mentioned above can be found here:<a href="http://css-tricks.com/serious-form-security/" rel="nofollow">Serious Form Security</a></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.
 

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