Asked 7 years ago
22 Dec 2016
Views 577
web-api

web-api posted

function to generate good secure password

i am seeking good function to generate secure password , i gonna use it to suggest password

function generate_password(){
return rand(1000,100000);
}


made some silly code . it will give a random number from 1000 , 100000 to user for suggestion of password.

but need to generate more secure and more difficult password.
shyam

shyam
answered Nov 30 '-1 00:00


function generate_random_password($length){
	$length=10;
	$password='';
	while($length>0){
		$password.=chr(mt_rand(33, 125));
		$length--;
	}
	return $password;
	}

echo generate_random_password(10);

it will generate password for 33 , 125 ASCII value .
it will give you random password with more secure and in less code
you can also put mt_rand(0,100) . you can check ASCII table to predict how it make password .
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

below function provide secure password and difficult to guess because it give you mix of A-Z , a-z , 0-9 , special character

	function generate_random_password($length = 10)
	{
		$alphabets = range('A','Z');
		$Smallalphabets = range('a','z');    
		$numbers = range('0','9');
		$additional_characters = array('_','.'); 
		$final_array = array_merge($alphabets,$Smallalphabets,$numbers,$additional_characters);
		
		$password = '';
		
		while($length--)
		{
			$key = array_rand($final_array);
			$password .= $final_array[$key];
		}
		return $password;
	}    		
	


call it to generate random password

$random_secure_password=generate_random_password(9);


you can make more complex by adding more special character like below code.
i add more special character at $additional_characters

      function generate_random_password($length = 10)
	{
		$alphabets = range('A','Z');
		$Smallalphabets = range('a','z');    
		$numbers = range('0','9');
		$additional_characters = array('_','.',"#","$","%","@","!"); 
 		$final_array = array_merge($alphabets,$Smallalphabets,$numbers,$additional_characters);
		
		$password = '';
		
		while($length--)
		{
			$key = array_rand($final_array);
			$password .= $final_array[$key];
		}
		return $password;
	}    

it make good random generated password which more secure and not easy to guess
Post Answer