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
https://forums.phpfreaks.com/topic/206568-csrf-prevention/
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
https://forums.phpfreaks.com/topic/206568-csrf-prevention/#findComment-1106470
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.