tell me what you think of this:

<?php
function head(){
	$header = "header.php";
	include($header);
	return true;
}
function foot(){
	$footer = "footer.php";
	$inc = include($footer);
	return true;
}
function arr(){
	$array = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'0','1','2','3','4','5','6','7','8','9');
	$text = $array[rand(0,35)];
	return $text;
}
head();
$a = arr(); $b = arr(); $c = arr(); $d = arr(); $e = arr(); $f = arr(); $g = arr(); $h = arr(); $i = arr(); $j = arr(); $k = arr(); $l = arr(); $m = arr();
$n = arr(); $o = arr(); $p = arr(); $q = arr(); $r = arr(); $s = arr(); $t = arr(); $u = arr(); $v = arr(); $w = arr(); $x = arr(); $y = arr(); $z = arr();
echo $a.$b.$c.$d.$e.$f.$g.$h.$i.$j.$k.$l.$m.$n.$o.$p.$q.$r.$s.$t.$u.$v.$w.$x.$y.$z;
$pass = md5($a.$b.$c.$d.$e.$f.$g.$h.$i.$j.$k.$l.$m.$n.$o.$p.$q.$r.$s.$t.$u.$v.$w.$x.$y.$z);
echo $pass;
foot();
?>

    I would have done it like this.
    (Not tested)

    HalfaBee

    <?php
    function head(){
        $header = "header.php";
        include($header);
        return true;
    }
    function foot(){
        $footer = "footer.php";
        $inc = include($footer);
        return true;
    }
    
    $array =  array('a','b','c','d','e','f','g','h','i','j','k',
     'l','m','n','o','p','q','r','s','t','u','v','w','x
    ','y','z',
    '0','1','2','3','4','5','6','7','8','9');
    
    head();
    for( $i=0;$i<26;$i++ )
      $pass .= $array[rand(0,count($array))];
    echo $pass;
    $pass = md5( $pass )
    echo $pass;
    foot();
    ?>
    

      Cool but instead of

      $pass = md5($a.$b.$c.$d.$e.$f.$g.$h.$i.$j.$k.$l.$m.$n.$o.$p.$q.$r.$s.$t.$u.$v.$w.$x.$y.$z);

      You should maybe try:

      $pass = $a.$b.$c.$d.$e.$f.$g.$h.$i.$j.$k.$l.$m.$n.$o.$p.$q.$r.$s.$t.$u.$v.$w.$x.$y.$z;
      $i=0;
      while ($i<20){
      $pass=crypt($pass,CRYPT_BLOWFISH);
      ++$i;
      }

        Now a tid bit more compact

        <?php
        $header = "header.php";
        include($header);
        $passlength = 25;
        for(;strlen($ultimatepass)<=$passlength;$ultimatepass.=chr(rand($a=ord('A'),ord('Z'))+ rand()%2*(ord('a')-$a)));
        echo $ultimatepass;
        $ultimatepass = md5($ultimatepass);
        echo $ultimatepass;
        $footer = "footer.php";
        include($footer);
        ?>

        Doesn't include numbers, but includes upper and lower case letters

          Why would you bother encrypting the password 20 times.
          what ever happend to for loops?

          It would take hundreds of years to crack a 26 char password encrypted with MD5.

          HalfaBee

            Originally posted by tasistro
            Now a tid bit more compact

            <?php
            $header = "header.php";
            include($header);
            $passlength = 25;
            for(;strlen($ultimatepass)<=$passlength;$ultimatepass.=chr(rand($a=ord('A'),ord('Z'))+ rand()%2*(ord('a')-$a)));
            echo $ultimatepass;
            $ultimatepass = md5($ultimatepass);
            echo $ultimatepass;
            $footer = "footer.php";
            include($footer);
            ?>

            Doesn't include numbers, but includes upper and lower case letters

            A bit more compact, but hard to read and a lot slower. ( about 50% slower than my option.)

            HalfaBee

              My rand_pass function...

              <?php
              function rand_pass() {
                 $letters = array (1 => "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
                 $password_size = rand(6,15);
                 $rand_password = " ";
                 for ($counter=0; $counter<$password_size; $counter++) {
                    $number = rand(0,9);
                    $letornum = rand(1,100);
                    $letter_number = rand(1,26);
                    if ($letornum <= 50) {
                       $rand_password .= "$letters[$letter_number]";
                    } elseif ($letornum > 50) {
                       $rand_password .= "$number";
                    } else {
                    $rand_password .= "$letters[$letter_number]";
                    }
                 }
                 return $rand_password;
              }
              echo (rand_pass());
              ?>
              

              🙂

                A bit more compact, but hard to read and a lot slower. ( about 50% slower than my option.)

                Matter depends on the reader and how often you change passwords.

                  jebster try this
                  <?php
                  function rand_pass() {
                  $letters = array (1 => "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
                  $password_size = rand(6,15);
                  $rand_password = " ";
                  for ($counter=0; $counter<$password_size; $counter++) {
                  $number = rand(0,9);
                  $letter_number = rand(1,26);
                  switch (rand(1,3)) }
                  case 1:
                  $rand_password .= $letters[$letter_number];
                  break;
                  case 2:
                  $rand_password .= strtoupper($letters[$letter_number]);
                  break;
                  case 3:
                  $rand_password .= $number;
                  break;
                  }
                  }
                  return $rand_password;
                  }
                  echo (rand_pass());
                  ?>

                    Originally posted by tasistro
                    jebster try this
                    <?php
                    function rand_pass() {
                    $letters = array (1 => "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
                    $password_size = rand(6,15);
                    $rand_password = " ";
                    for ($counter=0; $counter<$password_size; $counter++) {
                    $number = rand(0,9);
                    $letter_number = rand(1,26);
                    switch (rand(1,3)) }
                    case 1:
                    $rand_password .= $letters[$letter_number];
                    break;
                    case 2:
                    $rand_password .= strtoupper($letters[$letter_number]);
                    break;
                    case 3:
                    $rand_password .= $number;
                    break;
                    }
                    }
                    return $rand_password;
                    }
                    echo (rand_pass());
                    ?>

                    The reason I did it like I did with the rand(1,100); is cause when I used smaller number like you have(1,3) it wasn't very random! It would keep picking 1 like a dozon times in a row and such, seemed to work better with the larger numbers! 🙂

                      <?php 
                      include("header.php");
                      srand ((float) microtime() * 10000000);
                      $array = array_merge(range("a", "z"), range ("0", "9")); 
                      $plain = "";
                      for ($i=1; $i <= 26; $i++)
                      	$plain .= $array[rand(0, 35)];
                      $pass = md5($plain); 
                      echo $plain."<br>".$pass; 
                      include("footer.php"); 
                      ?> 
                      

                      I guess we're tied at 9 lines of code, but this script includes numbers.
                      It should be faster, too.
                      If you unroll the for loop, you could probably squeeze a bit more performance out of it.
                      For that matter, it would probably be a little quicker if I set the array manually, too, but I didn't want to type (or copy and paste) that much.

                      This was fun.

                      (EDIT: fixed poor grammar. :rolleyes: )

                        That is wierd jebster I just ran 900 rand(1,3) and it came out pretty even. Here is the dump in 30 run increments

                        8 8 14
                        17 20 23
                        28 34 28
                        41 46 33
                        52 56 42
                        62 66 52
                        72 79 59
                        85 85 70
                        98 94 78
                        112 101 87
                        123 109 98
                        131 119 110
                        143 128 119
                        156 139 125
                        165 148 137
                        179 154 147
                        188 168 154
                        196 183 161
                        206 189 175
                        217 199 184
                        226 209 195
                        235 220 205
                        246 230 214
                        256 237 227
                        264 249 237
                        277 257 246
                        284 269 257
                        293 281 266
                        300 296 274
                        311 302 287

                          Originally posted by tasistro
                          That is wierd jebster I just ran 900 rand(1,3) and it came out pretty even. Here is the dump in 30 run increments

                          8 8 14
                          17 20 23
                          28 34 28
                          41 46 33
                          52 56 42
                          62 66 52
                          72 79 59
                          85 85 70
                          98 94 78
                          112 101 87
                          123 109 98
                          131 119 110
                          143 128 119
                          156 139 125
                          165 148 137
                          179 154 147
                          188 168 154
                          196 183 161
                          206 189 175
                          217 199 184
                          226 209 195
                          235 220 205
                          246 230 214
                          256 237 227
                          264 249 237
                          277 257 246
                          284 269 257
                          293 281 266
                          300 296 274
                          311 302 287

                          Weird! Guess my computer is just screwed up LOL

                            You win coreyp_1

                            You don't need the srand anymore.

                            In older versions of PHP, you had to seed the random number generator before use with srand(). Since 4.2.0 this is no longer necessary.

                            I ran some tests using vars against numbers and it makes very little difference. The count function adds quite a bit to the loop.
                            0.00085 using count()
                            0.00063 using a number.

                            HalfaBee

                              I'd do it like this:

                              <?
                              
                              $chars="abcdefghijklmnopqrstuvwxyz0123456789";
                              $charslen=strlen($chars)-1;
                              for ($i = 1; $i <= 26; $i++) {
                                    $pass .= $chars{mt_rand(0,$charslen)};
                              }
                              
                              ?>
                              

                                No srand anymore?!?!?

                                What's the world coming to?

                                I haven't programmed in 6 years, and started using PHP 3 days ago. I figure that trying to solve problems in a forum like this would teach me more practicle things than just working on my own few projects. Please pardon my behind-the-times-essedness.

                                If it tells you anything, my last language was Pascal. Never even used C++! :rolleyes:

                                BTW, Halfabee, I didn't see your solution before I posted.

                                  Interesting.

                                  It is marinally slower using strings rather than arrays. ( I would have thought otherwise )

                                  It is quicker to use a var. for the strlen() as this is slower when executed 26 times.

                                  HalfaBee

                                  $len=strlen($chars)-1;
                                  for ($i = 1; $i <= 26; $i++) {
                                        $pass .= $chars{mt_rand(0,$len)};
                                  }
                                  

                                    Welcome to PHP coreyp_1

                                    Pascal, wow thats a language I haven't touched in 18 years.

                                    Lucily php is similar to pascal / c++ and it is easy to fall in the deep end and still survive.

                                    We both came up with a very similar solution, I just couldn't be bothered changing the array declaration.

                                    HalfaBee

                                      Write a Reply...