Jump to content



Welcome to AstaHost - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!

Replying to Php Random Text Generating


Post Options

    • Can't make it out? Click here to generate a new image

  or Cancel


Topic Summary

Posted 01 June 2010 - 01:13 AM

This is the best i got online, try it..Php Random Text GeneratingGenerate random passwords with PHP In this tutorial I will show you how to generate random passwords that are highly secure and extremely difficult to crack. However you can choose between various complexity/strength and you can set password length as well. Step 1. Let’s go through what we need to generate passwords. First we need a list of words and/or characters what we can use for password generation. I don’t offer using word lists as it is easier to guess and password recovery tools are using such lists as well. So I will focus only on character lists. The idea is to create a string from the characters and than in a loop we select an item from this string (character list) one by one until we reach the requested length. To realize this we will implement a function with 2 parameters. The first is the length of the requested password and the second is the strength/complexity of the password. Step 2. The function use case looks like this: Initializing the PHP random generator using the actual time value. Define 3 variousstrings for the various password complexity. Reset the password andlength counter variables Create a loop until the requested length andappend a random character one by one to the password string. In the loop I made one more check to make all character different in the generatedpassword. Return with the ready password.It is quite simple and you can generate really strong passwords with it. The complete code looks like this:<?phpfunction generatePassword($length=6,$level=2){list($usec, $sec) = explode(' ', microtime());srand((float) $sec + ((float) $usec * 100000));$validchars[1] = "0123456789abcdfghjkmnpqrstvwxyz"; $validchars[2] = "0123456789abcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $validchars[3] = "0123456789_!@#$%&*()-=+/abcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_!@#$%&*()-=+/"; $password = "";$counter = 0; while ($counter < $length) { $actChar = substr($validchars[$level], rand(0, strlen($validchars[$level])-1), 1); // All character must be different if (!strstr($password, $actChar)) {$password .= $actChar;$counter++;} } return $password;}?>Step 3. To make the script more usable let’s create an html page where the visitor can set the requested length and strength of the password. To do this we will create a simple form with 2 drop down box where the visitor can select the password properties. In this example I set the length list from 5 to 10 and the strength to Easy, Normal, and Hard. When the visitor submits the form it will call itself and in this phase not only shows the form again but processes the submitted values and generates the requested password and shows it for the user. The HTML code is quite simple: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.Dtd"><html><body> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <table> <tr><td>Password length: </td><td> <select name="passlength"> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select> </td></tr> <tr><td>Password strength:</td><td> <select name="passstrength"> <option value="1">Easy</option> <option value="2">Normal</option> <option value="3">Hard</option> </select> </td></tr> <tr><td ><br/><input type="submit" name="submitBtn" value="Generate"></td></tr> </table> </form><?php if (isset($_POST['submitBtn'])){ echo '<table><tr><td>Generated password:</td><td>'; echo generatePassword($length,$strength); echo '</td></tr></table>'; }?></body> Step 4. Final words:As you can see generating a random password is not so complex task. You can integrate this script into your registration process to generate a password for the user. The complete script is the following:<?phpfunction generatePassword($length=6,$level=2){ list($usec, $sec) = explode(' ', microtime()); srand((float) $sec + ((float) $usec * 100000)); $validchars[1] = "0123456789abcdfghjkmnpqrstvwxyz"; $validchars[2] = "0123456789abcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $validchars[3] = "0123456789_!@#$%&*()-=+/abcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_!@#$%&*()-=+/"; $password = ""; $counter = 0; while ($counter < $length) { $actChar = substr($validchars[$level], rand(0, strlen($validchars[$level])-1), 1); // All character must be different if (!strstr($password, $actChar)) { $password .= $actChar; $counter++; } } return $password;}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.Dtd"><html><body> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <table> <tr><td>Password length: </td><td> <select name="passlength"> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select> </td></tr> <tr><td>Password strength:</td><td> <select name="passstrength"> <option value="1">Easy</option> <option value="2">Normal</option> <option value="3">Hard</option> </select> </td></tr> <tr><td ><br/><input type="submit" name="submitBtn" value="Generate"></td></tr> </table> </form><?php if (isset($_POST['submitBtn'])){ echo '<table><tr><td>Generated password:</td><td>'; echo generatePassword($length,$strength); echo '</td></tr></table>'; }?></body> -reply by James Bondze

Posted 17 May 2010 - 10:23 AM

Complete SolutionPhp Random Text Generating

<?php/** * @author Praateek * @copyright 2010 */$randcheck = rand(0, 4);   if ($randcheck == 0)   {   $custom = chr(ord("a") + rand(0, 25)) . Rand(10, 99) . Chr(ord("a") + rand(0, 25)) .   rand(10, 99);   } elseif ($randcheck == 1)   {   $custom = rand(10, 99) . Chr(ord("a") + rand(0, 25)) . Rand(10, 99) . Chr(ord("a") +   rand(0, 25));   } elseif ($randcheck == 2)   {   $custom = chr(ord("a") + rand(0, 25)) . Chr(ord("a") + rand(0, 25)) . Rand(10, 99) .   rand(10, 99);   } elseif ($randcheck == 3)   {   $custom = rand(10, 99) . Rand(10, 99) . Chr(ord("a") + rand(0, 25)) . Chr(ord("a") +   rand(0, 25));   } elseif ($randcheck == 4)   {   $custom = rand(10, 99) . Chr(ord("a") + rand(0, 25)) . Chr(ord("a") + rand(0, 25)) .   rand(10, 99);   }   echo $custom;?>


HannahI

Posted 23 February 2010 - 10:22 PM

Your methods could support things like ¥,Ω,≈,ç,√,∫,˜,µ,¬,˚,˙,©,ƒ,∂,ß,å,∑,´,®,†,¥,¨!

Quatrux

Posted 23 February 2010 - 07:13 AM

The iGuest way which you prefer has one bad thing, that it's using md5, which you can substr or something to make it use less symbols, but the problem then is that you're stuck only with these letters:

abcdef1234567890

where with my method and 8ennett method you can include anything you want, even #$%^ symbols, which can be good for a CAPTCHA or for a SMS code or for any code which needs to be short and have a lot of different variations to not generate the same.

Those long codes which are lets say 16 letters are good for generating confirmation codes, where you don't really care about it, those kind of codes usually are copied and pasted or clicked on. ;)

HannahI

Posted 21 February 2010 - 09:40 PM

I kind of like doing the guest's way.

8ennett

Posted 29 January 2010 - 04:20 PM

Really simple one here for ya, works just fine however no letter or number is repeated (will be in the next example):

$alphanum = "abcdefghijkmnpqrstuvwxyz23456789";
   
   $rand = substr(str_shuffle($alphanum), 0, 5);  // 5 Being the amount of letters/numbers are select from the variable alphanum

And if you want letters and numbers to have a chance of being repeated:

$alphanum = "abcdefghijkmnpqrstuvwxyz23456789";
   
   $inc = 1;
   while ($inc < 5){
   $alphanum = $alphanum.'abcdefghijkmnpqrstuvwxyz23456789';
   $inc++;
   }  
   
   $rand = substr(str_shuffle($alphanum), 0, 5);  // 5 Being the amount of letters/numbers are select from the variable alphanum


And finally for a random length text string with possible repeated letters or numbers:

$alphanum = "abcdefghijkmnpqrstuvwxyz23456789";
   
   $inc = 1;
   while ($inc < 5){
   $alphanum = $alphanum.'abcdefghijkmnpqrstuvwxyz23456789';
   $inc++;
   }  
   
   $rand = substr(str_shuffle($alphanum), 0, rand(3,10));  // 3 Being the minimum amound of letters returned and 10 being the maximum

Posted 29 January 2010 - 07:59 AM

How to generate a group of word from CharactersPhp Random Text Generating

I want to a making a word from group of the character.Eg. Opinion is a correct word from the group of character ionnipo(generate a group of the word in php from 7 character)

-reply by musavvir


Quatrux

Posted 30 October 2009 - 01:06 PM

To generate some random characters I use a bit different method, it might be slower, but if it's for a personal site or a site not as big as Wikipedia you can use this code, even though it's not as slow as you can imagine, it works normally and in my opinion is a bit more clear to people who will start editing it:

function gen_chars($length = 6) {
	// Available characters
	$chars = '0123456789abcdefghjkmnoprstvwxyz';

	$Code  = '';
	// Generate code
	for ($i = 0; $i < $length; ++$i)
	{
		$Code .= substr($chars, (((int) mt_rand(0,strlen($chars))) - 1),1);
	}
return $Code;
}

// Usage
$var = gen_chars(12);

echo $var;

In the function variable $chars = '..'; I did not include all the letters, because I use this function for sending SMS code to activate some feature on a site and some letters does not feet, due to they look similar, like l can look like 1 and etc. if they are one to another, of course if you'll add all the alphabet and maybe even other symbols and even uppercase letters you'll get a much bigger probability for it to be different for example looping it million times and of course making more than 6 or 12 characters/length will have more different variations. :D

Posted 08 September 2009 - 08:57 PM

Ya know what you could just do is be sneaky and do this.

$ranNum = rand(100,9999).'-'.Rand(10000,999999);

$hexRan = md5($ranNum );

ThisConverts it to a MD5 Hash and since its random it doesn't reallyMatter. Store it in the DB and you should be good. Of course this codeIs only good if you want to have letters and numbers in the string. IfYou're going for JUST text, well then I would suggest doing somethingLike what is described above.


marijnnn

Posted 18 February 2005 - 09:13 AM

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 :P

Review the complete topic (launches new window)