Jump to content

CSRF prevention


kellyjg

Recommended Posts

I have a question about Cross-Site Request Forgeries (CSRF).

 

Somewhere in the processing of my form, I check:

 

if (isset($_SESSION['token']) && $_POST['token'] == $_SESSION['token']) {
     // all other code omitted
} else {
     // no place for bad guys here
}

 

So basically, if the token is good then the form continues to check for errors, valid data, etc...

 

I was wondering; is there a point in checking the token again each time I check something else?

 

For example:

 

// above code omitted
if (isset($_SESSION['token']) && $_POST['token'] == $_SESSION['token']) {
     // all other code omitted
    // check to see if there were any errors
    if  (count($errors) >= 1) {
$valid = false;								
   } else {
       // all other code omitted
       if ($sent == $allowed) {									
            if ($addNew == true) {// Should I be checking the token each time, or am I being redundant??
                // all other code omitted
            }
       }
   }
} else {
     // no place for bad guys here
}

Link to comment
Share on other sites

  • 1 month later...

If they edit the hidden field, it won't pass the validation. It's pretty much what you have:

<?php
//Generate random hash, store in $_SESSION['token'], also place in hidden field

//When the form is being validated, check if the token is correct
if (isset($_SESSION['token']) && $_POST['token'] == $_SESSION['token']) {

//If the token isn't valid, then throw an exception or act accordingly

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.