Welcome Guest ( Log In | Register )




                Web Hosting

 
Reply to this topicNew 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: 630
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()
{
    define('NUM0', 48);
    define('NUM9', 57);
    define('LETA', 65);
    define('LETZ', 90);
    define('LETa', 97);
    define('LETz', 122);
    $salt = '';
    $passwd = '';

    define('PASSWD_LEN', 10);

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

    $salt = str_shuffle($salt);

    for($gen = 0; $gen < PASSWD_LEN; $gen++)
        $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 topicNew Topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   0 zainboy 95 25th December 2008 - 08:50 PM
Last post by: zainboy
No New Posts   14 Mr 85 1,654 17th December 2008 - 03:44 PM
Last post by: ryantommo
No New Posts   3 nighthawk16 2,261 14th December 2008 - 05:51 PM
Last post by: iG-Kudos
No New Posts   2 denverporia19 1,862 11th December 2008 - 10:36 PM
Last post by: iG-Marble
No New Posts   11 ViRuaL 2,360 10th December 2008 - 10:14 PM
Last post by: iG-nick
No New Posts   13 Scionwest 4,808 6th December 2008 - 11:34 PM
Last post by: iG-Levi
No New Posts   8 Mico 456 2nd December 2008 - 07:14 AM
Last post by: The_Fury
No New Posts   3 Sir Joe 2,023 2nd December 2008 - 03:14 AM
Last post by: iG-Ben and Hank
No New Posts 6 miCRoSCoPiC^eaRthLinG 1,986 29th November 2008 - 02:24 PM
Last post by: yordan
No New Posts 13 FirefoxRocks 462 24th November 2008 - 03:54 AM
Last post by: FirefoxRocks
No New Posts   7 eina 374 20th November 2008 - 11:30 PM
Last post by: tansqrx
No New Posts   6 bob3695 4,979 20th November 2008 - 06:27 AM
Last post by: Guest
No new   24 JohnNitro 9,651 14th November 2008 - 11:54 AM
Last post by: Guest
No New Posts   3 clovis 1,182 13th November 2008 - 05:47 AM
Last post by: Guest
No New Posts   2 merlincsc 229 4th November 2008 - 10:08 PM
Last post by: minimcmonkey