samtwilliams Posted July 26, 2009 Share Posted July 26, 2009 Evening All, I want to add an else statement to my if block 'TEST FOR BLANKS IF BLOCK' within the foreach. As if there arnt any blanks i want to perform another function. if (isset($_SESSION['token']) && $_POST['token'] == $_SESSION['token']) { // TOKEN CHECK foreach($_POST as $allvalues) { // FOREACH BLOCK if($allvalues == "") { // TEST FOR BLANKS IF BLOCK $warning = 'Error: One or more of the fields are blank'; } // TEST FOR BLANKS IF BLOCK } // FOREACH BLOCK } else { $warning = 'Warning: You have either tried to re-submit your last form entry or this is a CSRF attack. This has been logged!'; } // TOKEN CHECK } Hope someone can help. Sam Quote Link to comment https://forums.phpfreaks.com/topic/167517-solved-if-block-with-an-else-problem/ Share on other sites More sharing options...
ignace Posted July 26, 2009 Share Posted July 26, 2009 if (something) { } else { // something else } Quote Link to comment https://forums.phpfreaks.com/topic/167517-solved-if-block-with-an-else-problem/#findComment-883384 Share on other sites More sharing options...
samtwilliams Posted July 26, 2009 Author Share Posted July 26, 2009 I under stand the if statement but when i code it like this, it still executes the 'done' even if some of the post fields are blank. if (isset($_SESSION['token']) && $_POST['token'] == $_SESSION['token']) { // TOKEN CHECK foreach($_POST as $allvalues) { // FOREACH BLOCK if($allvalues == "") { // TEST FOR BLANKS IF BLOCK $warning = 'Error: One or more of the fields are blank'; } else { $warning = "done"; //SCRIPT CONTINUES } // TEST FOR BLANKS IF BLOCK } // FOREACH BLOCK } else { $warning = 'Warning: You have either tried to re-submit your last form entry or this is a CSRF attack. This has been logged!'; } // TOKEN CHECK } // ISSET BLOCK Do i have to break the foreach? Quote Link to comment https://forums.phpfreaks.com/topic/167517-solved-if-block-with-an-else-problem/#findComment-883388 Share on other sites More sharing options...
vineld Posted July 26, 2009 Share Posted July 26, 2009 Yeah, either break or set some variable. The latter is generally a better idea. What you do now is go through this process for each item in the array which means it simply produces the message that corresponds to the last value. Try to follow the logic of your code. Also, it might be a good idea not to print that last error message. Just say "Something went wrong." or something like that. It might confuse at least one attacker. Quote Link to comment https://forums.phpfreaks.com/topic/167517-solved-if-block-with-an-else-problem/#findComment-883400 Share on other sites More sharing options...
samtwilliams Posted July 26, 2009 Author Share Posted July 26, 2009 Thank you, yes i decided against the last error message i might just say Do not resubmit, and i decided to use break;. Quote Link to comment https://forums.phpfreaks.com/topic/167517-solved-if-block-with-an-else-problem/#findComment-883401 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.