Php Random Text Generating - How to Generate Random Text

free web hosting
Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

Php Random Text Generating - How to Generate Random Text

szupie
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.

Reply

mastercomputers
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

 

 

 


Reply

szupie
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

Reply

Xeon
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.

Reply

marijnnn
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

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

Recent Queries:-
  1. random text php - 1.74 hr back. (1)
  2. php generate random name - 8.64 hr back. (1)
  3. php script for genrating random code - 15.09 hr back. (1)
  4. php random text - 16.86 hr back. (1)
  5. php generate random character - 30.99 hr back. (1)
  6. php random text generation - 36.08 hr back. (1)
  7. generate random salt in php - 39.59 hr back. (1)
  8. generate random php key - 51.01 hr back. (1)
  9. php generate random key function - 52.21 hr back. (1)
  10. php generate random key - 52.22 hr back. (1)
  11. selector random text web - 59.30 hr back. (1)
  12. php function for generating random names - 61.88 hr back. (1)
  13. php random text - 36.42 hr back. (3)
  14. for generating random text php - 64.52 hr back. (1)
Similar Topics

Keywords : php, random, text, generating, generate, random, text

  1. Php Random Selector
    whats the code (2)
  2. Random With Functions?
    (4)
    Hey! Is it possible to use a random script on functions. Lets say I have created three functions
    (Items that a user will win if he defeates a monster). One function is a function named shield()
    and another function is named sword() and a third function is named helmet(). Now when a player
    defeates a monster, he must be awarded. So.. How can I randomize what item he should get? something
    like CODE rand(sheild(), sword(), helmet()); or? Thanks //Feelay....
  3. How To Disable The Enter Key Of An Input Text Box
    (7)
    Hi everybody, i don't know if this is the correct place to post this topic so in case it is not
    please mods move it to the correct place, thanks. I have a page with a FORM that contains one or
    more INPUT TEXT boxes, a standard SELECT box, a standard SUBMIT button, a standard BUTTON button,
    and some hidden TEXT boxes, that will be used to Edit or to Add a record from a MySql database as
    usual, in Edit mode this form will get all the information from the database and in Add mode simply
    will fill it with default values, this page will be opened with the window.open() j....
  4. Generate?
    (3)
    How do I make a text generator? Like have a multiple dropdown and text box. Then if someone input
    some info and click summit it generate a text?....
  5. Random Name Generator
    For a fantasy RPG forum (2)
    Well, I did this particular piece of coding a few days back, and I forgot to display it to you all
    here at AstaHost. I have just started a fantasy RPG forum. The naming rules are pretty strict
    because it has to be based on a construted langage called Pasen. No matter how much I explained, I
    thought, it would be hard for a newbie to understand the syllable formation constraints. Hence, I
    wrote a script and put it at my Drupal powered site, that generates random names for the four races
    for both genders. http://www.WiseTome.com/name-generator-for-chaos-and-order I ....
  6. Script For Viewing A Random Image Needed
    (3)
    While browsing the web, I bumped into a script that I could really use, since it's different
    from everything that I saw. Most scripts that view a random image contain a code that chooses an
    image, and then puts the part into the page itself. However, on the www.greenplastic.com web
    site (dedicated to Radiohead), in the top left corner there is a random image module. If you look at
    the code, here's what is used: CODE Now, how can a PHP script be an image, and what
    kind of script that is? If you do visit that URL , you don't get a normal page, b....
  7. Generating A Table Into A File In CSV Format
    and letting user download the file (6)
    I'm working on a project, part of which consists of working with large tables of different
    kinds. Now, I'm using a page seeking technique which allows you to browse through the records
    depending on which page you want to see and how many records you want to see on each page. Now, I
    need a link (or a form button) on each page which, when clicked, will throw a file to the user for
    download (the download window should popup immediatly) which will give the part of the table,
    currently being viewed, in CSV format. One way I can think of to generate the file is manu....
  8. Display Text If Line Not Empty In Config File
    (8)
    I have been working on a new template and I would like it so that if in the global configuration
    file, I have a variable for a global site announcement that would go on every page. The line in the
    global configuration file is this: CODE $announcement = "ANNOUNCEMENT"; In my template file,
    I could easily add CODE but that would leave a blank space and with the announcement style
    (similar to the Invision Power Board error box). Is there some sort of script that could be put in
    the template page so I could have the global config file look something like t....
  9. Php: Write Random Text As Image
    Having problems, help needed! (3)
    I'm trying to create a script that writes text to an image. CODE header("Content-type:
    image/png"); $_phrases = array( "Test 1", "Test 2", "Test 3", "Test 4", "etc." ); $_rand_phrase
    = $_phrases ; $_image = imagecreatefrompng("gmail.png"); $_user_width =
    imaagettfbbox(9,0,"tahoma.ttf",$_rand_phrase); $_x_value = (200-($user_width + 113)); $_color =
    imagecolorallocate($_image, 165, 164, 164); imagettftext($_image, 9, 0, $_x_value, 16, $_color,
    "tahoma.ttf", $_rand_phrase); imagepng($_image); imagedestroy($_image); I can't see what
    I'm doing wron....
  10. Random Banners Rotation...
    set by percentages (2)
    alright, I know a few different ways to make a random banner with php - where a different banner
    would rotate with every refresh/click. But I've had some "customers" complain how they think
    their banner should show up more then others for one reason or another, and I completely agree with
    them. So now I kinda want to have a code (most perferably php or javascript) that would have the
    banner rotation where I could set a peticular banner to show up say 15% of the time and another 5%
    so I could satisfy my more loyal members over the ones simply looking for more hits to ....
  11. Random Image / Random Header
    S.O.S. (5)
    im building a website, and i need some help i got 3 frames, navigation, top and main, but i want
    the top banner to have a new tagline with every refresh, just like the header, it dont really matter
    what coding it is, as long as it's not to weird /tongue.gif' border='0'
    style='vertical-align:middle' alt='tongue.gif' /> ....

    1. Looking for php, random, text, generating, generate, random, text






*SIMILAR VIDEOS*
Searching Video's for php, random, text, generating, generate, random, text
advertisement




Php Random Text Generating - How to Generate Random Text