It need not be meaningful words - more like random password generation, but the catch is - they should be unique. I will be using this for some kind of package / product cod
Use Kohana text,
http://docs.kohanaphp.com/helpers/text
For example,
$prod_id = text::random('alpha', 7);
If you don't want use the framework, you can simply copy the code. You will find lots of goodies there.
Heres a very simple way
$chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
$temp_pw = substr( str_shuffle( $chars ), 0, 7 );
if ( check_unique( $temp_pw ) ) {
$pw = $temp_pw;
}
You'll have to implement your own check_unique function. That part should be easy.
Here's how I would solve this problem:
Consider that the 7 characters can be one of 26 letters (abc..z), or 10 numbers (01...9). This makes 36 possible characters.
Each time your application generates a new code, have it increment a global variable. You can turn this unique number into a unique string by using a "Hexatridecimal" converter, and by adding filler characters to make up the rest of the string.
Take a look at this link. I think this guy had the same problem as you: http://www.codemaxima.com/2010/04/the-hexatridecimal-numbering-system/
$random = substr(hash('md5',openssl_random_pseudo_bytes(32)),0,7);
Given that you mention passwords here, I'll assume you need a secure method (ie: someone shouldn't be able to guess someone else's password based on knowing any other password). You could use the following:
If security is not an issue, steps 1 and 2 by themselves would be sufficient. If security is an issue, it is vital that no one but yourself knows the value of "MasterPassword".
Galen's answer allows only one use of each character in the password. Not much information in that string. A simple change though:
$chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
$passwordlength = 7;
for ($x = 1; $x <= $passwordlength; $x++) {
$charlist .= $chars;
}
$temp_pw = substr( str_shuffle( $charlist ), 0, $passwordlength );