On Tue, 2003-06-17 at 02:45, Davy Obdam wrote:
> Hi people,
> 
> I have to make a password generator, but i have a little problem.
> 
> - It needs to generate password 8 characters long, and including 1 or 2 
> special characters(like #$%&*@).
> - Those special characters can never appear as the first or last 
> character in the string... anywhere between is fine.
> 
> I have a password generator script now that does the first thing... but 
> the special character can be in front or back of the string wich it 
> shouldnt.. i have been looking on the web for this but i havent found 
> the answer. Below is my scripts so far.. 
> 
> Any help is appreciated, thanks for your time,
> 
> Best regards,
> 
> Davy Obdam

Please don't crosspost. Pick the suitable list (in this case, it would
have been php-general).

Anyway, just tell it not to use anything beyone the first 26 characters
of your allowable characters string. Below is one way to do it.


Good luck,

Torben


<?php
error_reporting(E_ALL);
ini_set('display_errors', true);

// A function to generate random alphanumeric passwords in PHP
// It expects to be passed a desired password length, but it
// none is passed the default is set to 8 (you can change this)
function generate_password($length = 8) {
   // This variable contains the list of allowable characters
   // for the password.  Note that the number 0 and the letter
   // 'O' have been removed to avoid confusion between the two.
   // The same is true of 'I' and 1
   $allowable_characters =
'abcdefghefghijklmnopqrstuvwxyz0123456789%#*&';
     
   // We see how many characters are in the allowable list
   $ps_len = strlen($allowable_characters);

   // Max index of the characters allowed to stand and end the output.
   $max_endpoint_ind = 25;

   // 0-based index of the last char of the output
   $last_char = $length - 1;

   // Seed the random number generator with the microtime stamp
   // (current UNIX timestamp, but in microseconds)
   mt_srand((double)microtime() * 1000000);

   // Declare the password as a blank string.
   $pass = "";

   // Loop the number of times specified by $length
   for($i = 0; $i < $length; $i++) {
       // Each iteration, pick a random character from the
       // allowable string and append it to the password.
       switch ($i) {
       case 0:
       case $last_char:
           $pass .= $allowable_characters{mt_rand(0,
$max_endpoint_ind)};
           break;
       default:
           $pass .= $allowable_characters{mt_rand(0, $ps_len)};
       }
   }

   // Retun the password we've selected
   return $pass;
}

for ($i = 0; $i < 100; $i++) {
    echo generate_password() . "\n";
}

?>


-- 
 Torben Wilson <[EMAIL PROTECTED]>                        +1.604.709.0506
 http://www.thebuttlesschaps.com          http://www.inflatableeye.com
 http://www.hybrid17.com                  http://www.themainonmain.com
 -----==== Boycott Starbucks!  http://www.haidabuckscafe.com ====-----




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to