[snippet] Create Random Strings, PHP

Brandon

Former Senior Account Rep
Community Support
Messages
19,181
Reaction score
28
Points
48
Ok, to create a random string in php use this great code snippet I made

PHP:
<?php

// Brandon <brandon@phpriots.com>
// Support: http://www.phpriots.com/forums/
// Rev 1

// Edit Below
$min = "5"; // Minimum Password Length
$max = "15"; // Maximun Password Length

################## Don't edit bellow unless you know what you are doing ##################

// Letter/number Array
$useable_characters = array_merge(range('A','Z'),range('a','z'),range(0,9));

// Make the length
$length = rand($min,$max);

$password = "";

for ($i = 0; $i < $length; $i++)
{
  $newrand = rand(0,61);
  $password .= $useable_characters[$newrand];
}

################## Echo's the password out, or do something else with it ##################

echo $password;
?>

Any support questions can be asked here or at the phpRiots forums.
 
Last edited:

YamiKaitou

Member
Messages
636
Reaction score
0
Points
16
I have always hated typing out all of those letters/numbers.

But what is the difference from that and using this? Anything?
PHP:
$useable_characters = array_merge(range('A','Z'),range('a','z'),range(0,9));

I use something similar to this with my captcha image
 
Last edited:

Brandon

Former Senior Account Rep
Community Support
Messages
19,181
Reaction score
28
Points
48
The fact that I never knew about that:drool:

That does seem more handy though, mind if I use it?
 

YamiKaitou

Member
Messages
636
Reaction score
0
Points
16
Not at all, I found out about it from someplace else. Can't remember where
 

Brandon

Former Senior Account Rep
Community Support
Messages
19,181
Reaction score
28
Points
48
Thanks, I updated the initial post to include that, seems to work so thanks.
 
Top