How to create random alphanumeric password in PHP

At times, you may need to generate and provide your website user with a new or default password. For example, if a user has forgotten his/her password then you may need to generate a new password for him/her.

Or you may want to provide default password to newly registered users after they have verified their e-mail address. It is obvious that password generated in these circumstances should be random in nature.

Here, I will show you an easy way to generate random alphanumeric password in PHP.

We will create a function named generate_random_password() for generating password. It will take an optional parameter, $length, which will define the length of the password. Create a new PHP file and add the following code to it:

<?php
  function generate_random_password($length = 10) {
    $alphabets = range('A','Z');
	$numbers = range('0','9');
	$additional_characters = array('_','.');
	$final_array = array_merge($alphabets,$numbers,$additional_characters);
		
	$password = '';
 
	while($length--) {
	  $key = array_rand($final_array);
	  $password .= $final_array[$key];
	}
 
	return $password;
  }
  echo 'Random password generated is "<b>' . generate_random_password(15) . '</b>".';
?>

In the above code we have used the range() function to automatically generate sequence of alphabets and numbers which are stored in variables $alphabets and $numbers respectively. We have also defined an additional array named $additional_characters which contains any additional character that we may want to use in our password.

After this we merge all the three arrays together with the help of array_merge() function and store the result in the resultant array $final_array. Also, we have declared $password variable which will contain our final generated password.

Next, we use array_rand() function to pick a random key from the resultant array. We append the character corresponding to this random selected key from the resultant array to $password variable. We keep doing this in a while loop until we have created the password of desired length. Finally, we return the randomly generated password. The output is shown below:

Randomly generated alphanumeric password in PHP

Tags:

Comments

Member since:
7 August 2017
Last activity:
6 weeks 1 day

kanye west shoes

michael kors bags

ua shoes

ray ban erika

prada handbags

payless shoes online

official kate spade website

coach outlet online

pandora rings

pandoracharms

pandora rings uk

Coach Sunglasses Outlet

balenciaga outlet

Engagement Rings - Official

Nike Air Max 180

official michael kors

Ray Ban sunglasses

shoes sale

Nike Air Max

Air Max 180

burberry outlet

Dreamland Jewelry - Official

birkenstock sandals

birkenstock

Nike Air Max Couple

coach outlet online

prada outlet online

louis vuitton outlet online

Nike Air Jordan

gel kayano

official michael kors

michael kors outlet bags

Hugo Boss Store

payless shoes

Premier Jewelry - Official

ray ban outlet

Coach Handbags

pandora sale

coach watches

nike store

football jerseys

louis vuitton outlet online

coach outlet online sale

Add Your Link Free

backlink

louis vuitton us

michael kors

Nike Air Jordan Enfants

michael kors

Michael Kors Handbags Outlet Sale

kate spade outlet online

Green Cleaned

ugg boots

sunglasses

Nike Blazer Femmes

evbid

Wine

coach outlet

birkenstock outlet

Burberry Australia

payless near me

Chanel Outlet

north face sale

Nike Air Max

Prada Shoes - Official

michael kors outlet

ray ban wayfarer sunglasses

Oakley Sunglasses

puma sneakers outlet

payless shoes

nike free shoes

diesel watches

burberry outlet canada

prada sale

puma sneakers sale

louis vuitton outlet

Air Max 180 Homme

Jewelry Armoire - Official

coach handbags sale

Hermes Outlet

timberland shoes

Nike Air Max 2017

coachs outlet

louis vuitton outlet online

Nike Jordans

football jerseys

burberry outlet sale

Nike Free 3.0

michael kors outlet online

ray ban wayfarer

ray ban sunglasses

Rolex Watch

michael kors

michael kors

nike huarache shoes

ray ban sale

Adamson

pandora necklaces

kate spade italy

coach australia

pandora uk

pandora uk

swatch watch

michael kors watches

michael kors

rayban prescription sunglasses

Brighton Jewelry - Official

prada outlet online

Nike Air Force

michael kors outlet

fulam shoes

softball jerseys

Nike Air Max

balenciaga us

Air Max 1 Homme

michael kors

Watches

Air Jordan Enfant

chrome store canada

sunglasses store

coach outlet

michael kors outlet

Nike Free Run

mk outlet online

michael kors outlet

the beat

seiko watches

michael kors

Lifestyle

Air Max 90 Femme

hermes outlet

Nike Air Jordan enfants

hermes outlet

prada purses

kate spade outlet

nike jordan shoes

me adc8.7