PHP Generate random unique password

0.28K views
no comments
18 Sep 2014 12:19 am

this function allow you to generate random characters code,function suitable for

Generate activation code for newly user account

Generate shortenURL code,

Generate temporary user password for changing password of account.

function generate_special_pass($pass_len = 8, $allow_special_chrs = true) {
	$allow_chrs = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
	$special_chrs = '!@#$%^&*()_+';
	if($allow_special_chrs) {
		$pass_chrs = $allow_chrs.$special_chrs;
	} else {
		$pass_chrs = $allow_chrs;
	}
	$pass_rand_val = null;
	for($inc =1;$inc<=$pass_len; $inc++ ) {
		$rand_pos = rand(0,strlen($pass_chrs)-1);

		$pass_rand_val .= substr($pass_chrs ,$rand_pos , 1);
	}

	return $pass_rand_val;
}

The 10 is characters limits its generate password with 10 characters and true allow to add special characters these define in $special_vars variable.

echo generate_special_pass(10, true)
//RESULT=Af$@tHiO*q

Now call it as non-special characters with limit of 20

echo generate_special_pass(20, false)
//RESULT=gHoQ0vM5lbX9i1Z70D4B

NOTE:Your Email Address will be not shown and please do not add spamming comments because here is REL="NOFOLLOW" on your links and comments also moderated shown.
<code>Put html css or any language code under this tag</code>