Bottyz Posted February 12, 2010 Share Posted February 12, 2010 hi all, I found an error whilst testing an activate users script i've been testing and debugging, but i can't find a way around this bug? Basically, it only happens when there is more than one user to activate in a list. And on the second, thrid, fourth, etc... in the loop the function to generate a random password cannot be redeclared? the problematic section fo the code is below: // connect to db foreach($_POST['userID'] as $uID){ $productaccess1 = isset($_POST[$uID.'_PA1']) ? '1' : '0'; $productaccess2 = isset($_POST[$uID.'_PA2']) ? '1' : '0'; $productaccess3= isset($_POST[$uID.'_PA3']) ? '1' : '0'; $productaccess4 = isset($_POST[$uID.'_PA4']) ? '1' : '0'; $deleterecord = isset($_POST[$uID.'_DEL']) ? '1' : '0'; $productaccess1a = "No"; $productaccess2a = "No"; $productaccess3a = "No"; $productaccess4a = "No"; //Generate random password function random_password($password) { $vowels = 'aeuy'; $consonants = 'bdghjmnpqrstvz'; $length = 8; $strength = 4; if ($strength & 4) { $vowels .= "AEIOU"; $consonants .= 'BDGHJLMNPQRSTVWXYZ23456789'; } if ($strength & { $consonants .= '@#$%'; } $password = ''; $alt = time() % 2; for ($i = 0; $i < $length; $i++) { if ($alt == 1) { $password .= $consonants[(rand() % strlen($consonants))]; $alt = 0; } else { $password .= $vowels[(rand() % strlen($vowels))]; $alt = 1; } } return $password; } $theirpassword = random_password($password); // encrypts the randomly generated 8 digit password into 32 digit hash $their_password = md5($theirpassword); Is it something that cannot be done in a loop? thanks in advance Link to comment https://forums.phpfreaks.com/topic/191866-fatal-error-cannot-redeclare/ Share on other sites More sharing options...
xjake88x Posted February 12, 2010 Share Posted February 12, 2010 You are declaring a function in a loop.. Meaning every iteration of the loop it tries to redeclare the function. Link to comment https://forums.phpfreaks.com/topic/191866-fatal-error-cannot-redeclare/#findComment-1011302 Share on other sites More sharing options...
jl5501 Posted February 12, 2010 Share Posted February 12, 2010 Yes, move the function declaration out of the loop and it will work you keep the call to the function in the loop Link to comment https://forums.phpfreaks.com/topic/191866-fatal-error-cannot-redeclare/#findComment-1011305 Share on other sites More sharing options...
bugcoder Posted February 12, 2010 Share Posted February 12, 2010 code should be like this <?php foreach($_POST['userID'] as $uID) { $productaccess1 = isset($_POST[$uID.'_PA1']) ? '1' : '0'; $productaccess2 = isset($_POST[$uID.'_PA2']) ? '1' : '0'; $productaccess3= isset($_POST[$uID.'_PA3']) ? '1' : '0'; $productaccess4 = isset($_POST[$uID.'_PA4']) ? '1' : '0'; $deleterecord = isset($_POST[$uID.'_DEL']) ? '1' : '0'; $productaccess1a = "No"; $productaccess2a = "No"; $productaccess3a = "No"; $productaccess4a = "No"; $theirpassword = random_password($password); // encrypts the randomly generated 8 digit password into 32 digit hash $their_password = md5($theirpassword); } function random_password($password) { $vowels = 'aeuy'; $consonants = 'bdghjmnpqrstvz'; $length = 8; $strength = 4; if ($strength & 4) { $vowels .= "AEIOU"; $consonants .= 'BDGHJLMNPQRSTVWXYZ23456789'; } if ($strength & { $consonants .= '@#$%'; } $password = ''; $alt = time() % 2; for ($i = 0; $i < $length; $i++) { if ($alt == 1) { $password .= $consonants[(rand() % strlen($consonants))]; $alt = 0; } else { $password .= $vowels[(rand() % strlen($vowels))]; $alt = 1; } } return $password; } ?> Link to comment https://forums.phpfreaks.com/topic/191866-fatal-error-cannot-redeclare/#findComment-1011306 Share on other sites More sharing options...
jl5501 Posted February 12, 2010 Share Posted February 12, 2010 actually it is better to place the function declaration, before you call it Link to comment https://forums.phpfreaks.com/topic/191866-fatal-error-cannot-redeclare/#findComment-1011311 Share on other sites More sharing options...
bugcoder Posted February 12, 2010 Share Posted February 12, 2010 actually it is better to place the function declaration, before you call it thanks Link to comment https://forums.phpfreaks.com/topic/191866-fatal-error-cannot-redeclare/#findComment-1011314 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.