Jump to content

Fatal Error: cannot redeclare


Bottyz

Recommended Posts

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

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;
    }

?>

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.