Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Php Random Text Generating, How to Generate Random Text
szupie
post Feb 4 2005, 11:04 PM
Post #1


S.P.A.M.S.W.A.T.
Group Icon

Group: Members
Posts: 814
Joined: 22-January 05
From: San Antonio, Texas (No, I'm not dumb. I just moved here...)
Member No.: 2,284



I was trying to figure out how to make random texts for random passwords and stuff, and I found someone who created this code.

QUOTE
<?

//author: polmme

$codelenght = 10;
while($newcode_length < $codelenght) {
$x=1;
$y=3;
$part = rand($x,$y);
if($part==1){$a=48;$b=57;}  // Numbers
if($part==2){$a=65;$b=90;}  // UpperCase
if($part==3){$a=97;$b=122;} // LowerCase
$code_part=chr(rand($a,$b));
$newcode_length = $newcode_length + 1;
$newcode = $newcode.$code_part;
}
echo $newcode;
?>


I think it's pretty good. If anyone has a better one or suggestions, please tell me.
Go to the top of the page
 
+Quote Post
mastercomputers
post Feb 5 2005, 07:39 AM
Post #2


PESTICIDAL MANIAC
Group Icon

Group: Members
Posts: 626
Joined: 1-September 04
From: Auckland, New Zealand
Member No.: 27



Well the only real way to work out whether it's good or not is to work out it's logic, yet I'm not in the mood for testing my brain, so I'll just rewrite this example in a different way but still same ending results, oh and I'll fix those notice errors for those undeclared variables.

Sorry to the author polmme it was a nice example indeed and a lot simpler to understand for beginners than my rewrite I admit.


<?php
for($code_length = 10, $newcode = ''; strlen($newcode) < $code_length; $newcode .= chr(!rand(0, 2) ? rand(48, 57) : (!rand(0, 1) ? rand(65, 90) : rand(97, 122))));

echo $newcode;
?>


I also have one I made based off someone elses code but I lost who that was, anyways here it is. I should have turned it into a function (just did), I know there was more I was going to do, which was randomise the characters within $salt string (just did) as well but didn't continue on this (just did), I might add to it later (just did), I got to be somewhere 5 minutes ago (just got back) tongue.gif What my intensions were, was to actually take a section from the string (just did) and use that as the password, as there really is no need to keep randomising it and then randomise again, and then do another randomise, although we could keep this going (stopped before this).


<?php
function mkRandPasswd()
{
    [/tab]define('NUM0', 48);
[tab]define('NUM9', 57);
    [/tab]define('LETA', 65);
[tab]define('LETZ', 90);
    [/tab]define('LETa', 97);
[tab]define('LETz', 122);
    [/tab]$salt = '';
[tab]$passwd = '';

    [/tab]define('PASSWD_LEN', 10);

[tab]for($nLoop = 0; $nLoop < NUM9 - NUM0 + 1; $nLoop++)
    [/tab][tab]$salt .= chr(mt_rand(NUM0, NUM9));
    for($ucLoop = 0; $ucLoop < LETZ - LETA + 1; $ucLoop++)
    [/tab][tab]$salt .= chr(mt_rand(LETA, LETZ));
    for($lcLoop = 0; $lcLoop < LETz - LETa + 1; $lcLoop++)
    [/tab][tab]$salt .= chr(mt_rand(LETa, LETz));

    [/tab]$salt = str_shuffle($salt);

[tab]for($gen = 0; $gen < PASSWD_LEN; $gen++)
    [/tab][tab]$passwd = $passwd . substr($salt, mt_rand() % strlen($salt), 1);

    return array($salt, $passwd);
}

list($salt, $passwd) = mkRandPasswd();

$str_passwd = substr($salt, strlen($salt) / 4, PASSWD_LEN);

echo 'Characters to select for password: ' . $salt . '<br /> The generated password: ' . $passwd . '<br /> The stringed password: ' . $str_passwd;

?>


Remember not much can be said about the script unless the logic is worked out, I still haven't worked mine out, but I can say it's safe enough for password generating, you still would require the user to change the password and not keep it.


Cheers,


MC
Go to the top of the page
 
+Quote Post
szupie
post Feb 5 2005, 11:15 AM
Post #3


S.P.A.M.S.W.A.T.
Group Icon

Group: Members
Posts: 814
Joined: 22-January 05
From: San Antonio, Texas (No, I'm not dumb. I just moved here...)
Member No.: 2,284



Yeah, those 2 codes and the Polmme did were all very nice. I wish that PHP would make its own function for generating that, though. :P
Go to the top of the page
 
+Quote Post
Xeon
post Feb 18 2005, 08:33 AM
Post #4


Member [ Level 2 ]
Group Icon

Group: Members
Posts: 66
Joined: 28-December 04
From: Los Angeles, CA
Member No.: 1,896



Interesting, I just made a random text generator, its located on my site. This code however generates only random letters between 4-7 characters long. The code is pretty simple:

CODE

for($j = 1; $j <= 30; $j++)
 {
   $lim = rand(4,7);
   for ($i = 1; $i <= $lim; $i++)
   {
     echo chr(ord('a') + rand(0,25));
   }
   echo "<BR>\n";
 }


Hope this is useful.
Go to the top of the page
 
+Quote Post
marijnnn
post Feb 18 2005, 09:13 AM
Post #5


Premium Member
Group Icon

Group: [HOSTED]
Posts: 336
Joined: 22-September 04
Member No.: 798



another way would to make a table with all the allowed signs
a,b,c,d,... ,A,B,C,D,... 1,2,3,...
let's say you get a table of 80 fields.
then you could just pick a random field
you could do this in various way: just rand(0,80)
if you have doubts about the randomness of rand, you could rand a random times and then start picking your letters
$i = rand(0,20);
for ($j=0; $j<$i;$j++) rand(0,80);
for ($i=0; $i<$passlenght;$i++)
$pw =$pw.$array[rand(0,80)];

or you could use srand

or you could go like rand(0,846854354)%80

anything that makes you feel safer smile.gif
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Php: Write Random Text As Image(3)
  2. Display Text If Line Not Empty In Config File(8)
  3. Generating A Table Into A File In CSV Format(6)
  4. Script For Viewing A Random Image Needed(3)
  5. Random Name Generator(2)
  6. Generate?(3)
  7. How To Disable The Enter Key Of An Input Text Box(7)
  8. Random With Functions?(4)
  9. Php Random Selector(2)


 



- Lo-Fi Version Time is now: 22nd November 2008 - 04:17 PM