How to solve Notice: Undefined variable: token in UserSpice 4.4.x in admin dashboard screens
Due to a bug which arose in Userspice 4.4 to 4.4.0.2 there appears a PHP error similar the following:
Notice: Undefined variable: token in htdocs/userspice44/users/views/_admin_settings_general.php on line 315 Call Stack #TimeMemoryFunctionLocation 10.0014410592{main}( ).../admin.php:0
The bug may cause successful processing of HTTP POST requests submitted via the forms on pages where this notice/ error appears.
Cause of the Issue
The issue is raised simply because the $token variable is referred to on line 315 which is 2 lines before it is defined and its value is generated by the invocation of a static method from the Token class.
<input type="hidden" name="csrf" value="<?=$token?>" /> </form> <?php $token = Token::generate(); ?>
Solution
The solution is rather simple. Cut the entire line (317) containing
<?php $token = Token::generate(); ?>
and paste it somewhere above line 315, so that it looks like the following:
<div class="form-group"> <label for="max_pw">Invisible Recaptcha Private (Secret) Key</label> <?php if(in_array($user->data()->id, $master_account)) {?><a href="#" class="nounderline" id="recapatcha_private_show"><span class="fa fa-eye"> </span> show</a><?php } ?> <input type="password" autocomplete="off" class="form-control ajxtxt" data-desc="Recaptcha Private Key" name="recap_private" id="recap_private" value="<?=$settings->recap_private?>"> </div> </div> </div> <?php $token = Token::generate(); ?> <input type="hidden" name="csrf" value="<?=$token?>" /> </form> <?php if(in_array($user->data()->id, $master_account)) {?> <script type="text/javascript"> $(document).ready(function(){ $('#recapatcha_public_show').hover(function () { $('#recap_public').attr('type', 'text');
After saving the file just like this and reloading the page in the browser, the error / notice will go away.
Repeat the same step on any other page and included view (.php) where you encounter the same error.
Comments are always welcome.